22
33namespace Aslnbxrz \SimpleOTP ;
44
5- use Illuminate \Support \Facades \Cache ;
65use Illuminate \Support \Facades \RateLimiter ;
7- use Aslnbxrz \SimpleOTP \Contracts \ OTPDeliveryContract ;
6+ use Aslnbxrz \SimpleOTP \Exceptions \ OTPException ;
87use Aslnbxrz \SimpleOTP \Contracts \OTPServiceContract ;
98use Aslnbxrz \SimpleOTP \Contracts \OTPStorageContract ;
10- use Aslnbxrz \SimpleOTP \Exceptions \ OTPException ;
9+ use Aslnbxrz \SimpleOTP \Contracts \ OTPDeliveryContract ;
1110use Aslnbxrz \SimpleOTP \Exceptions \OTPDeliveryException ;
1211
1312class OTPService implements OTPServiceContract
@@ -18,9 +17,9 @@ class OTPService implements OTPServiceContract
1817
1918 public function __construct (OTPStorageContract $ storage , OTPDeliveryContract $ delivery , array $ config )
2019 {
21- $ this ->storage = $ storage ;
20+ $ this ->storage = $ storage ;
2221 $ this ->delivery = $ delivery ;
23- $ this ->config = $ config ;
22+ $ this ->config = $ config ;
2423 }
2524
2625 public function generate (string $ identifier , string $ recipient , array $ options = []): array
@@ -35,12 +34,12 @@ public function generate(string $identifier, string $recipient, array $options =
3534
3635 // Store OTP
3736 $ metadata = [
38- 'recipient ' => $ recipient ,
37+ 'recipient ' => $ recipient ,
3938 'delivery_driver ' => $ this ->delivery ->getDriver (),
40- 'options ' => $ options ,
39+ 'options ' => $ options ,
4140 ];
4241
43- $ ttlMinutes = $ this ->config ['otp ' ]['ttl ' ] ?? 5 ;
42+ $ ttlMinutes = $ this ->config ['otp ' ]['ttl ' ] ?? 5 ;
4443 $ storeResult = $ this ->storage ->store ($ identifier , $ code , $ ttlMinutes , $ metadata );
4544
4645 if (!$ storeResult ) {
@@ -53,15 +52,16 @@ public function generate(string $identifier, string $recipient, array $options =
5352 } catch (OTPDeliveryException $ e ) {
5453 // Clean up stored OTP if delivery fails
5554 $ this ->storage ->delete ($ identifier );
55+
5656 throw $ e ;
5757 }
5858
5959 // Increment rate limiting counter
6060 $ this ->incrementRateLimit ($ identifier );
6161
6262 return [
63- 'success ' => true ,
64- 'message ' => $ this ->getMessage ('sent ' ),
63+ 'success ' => true ,
64+ 'message ' => $ this ->getMessage ('sent ' ),
6565 'identifier ' => $ identifier ,
6666 'expires_at ' => now ()->addMinutes ($ ttlMinutes ),
6767 ];
@@ -84,6 +84,7 @@ public function verify(string $identifier, string $code): bool
8484 $ maxAttempts = $ this ->config ['otp ' ]['max_attempts ' ] ?? 3 ;
8585 if ($ otpData ['attempts ' ] >= $ maxAttempts ) {
8686 $ this ->storage ->delete ($ identifier );
87+
8788 throw new OTPException ($ this ->getMessage ('max_attempts ' ));
8889 }
8990
@@ -105,7 +106,7 @@ public function resend(string $identifier, string $recipient, array $options = [
105106 {
106107 // Check if OTP exists
107108 $ existingOtp = $ this ->storage ->get ($ identifier );
108-
109+
109110 if ($ existingOtp && $ existingOtp ['verified ' ]) {
110111 throw new OTPException ($ this ->getMessage ('verified ' ));
111112 }
@@ -120,25 +121,26 @@ public function resend(string $identifier, string $recipient, array $options = [
120121 public function exists (string $ identifier ): bool
121122 {
122123 $ otpData = $ this ->storage ->get ($ identifier );
124+
123125 return $ otpData !== null && !$ otpData ['verified ' ];
124126 }
125127
126128 public function info (string $ identifier ): ?array
127129 {
128130 $ otpData = $ this ->storage ->get ($ identifier );
129-
131+
130132 if (!$ otpData ) {
131133 return null ;
132134 }
133135
134136 return [
135137 'identifier ' => $ identifier ,
136- 'recipient ' => $ otpData ['recipient ' ] ?? null ,
138+ 'recipient ' => $ otpData ['recipient ' ] ?? null ,
137139 'created_at ' => $ otpData ['created_at ' ],
138140 'expires_at ' => $ otpData ['expires_at ' ],
139- 'attempts ' => $ otpData ['attempts ' ],
140- 'verified ' => $ otpData ['verified ' ],
141- 'expired ' => now ()->timestamp > $ otpData ['expires_at ' ],
141+ 'attempts ' => $ otpData ['attempts ' ],
142+ 'verified ' => $ otpData ['verified ' ],
143+ 'expired ' => now ()->timestamp > $ otpData ['expires_at ' ],
142144 ];
143145 }
144146
@@ -149,7 +151,7 @@ public function delete(string $identifier): bool
149151
150152 protected function generateCode (): string
151153 {
152- $ type = $ this ->config ['otp ' ]['type ' ] ?? 'numeric ' ;
154+ $ type = $ this ->config ['otp ' ]['type ' ] ?? 'numeric ' ;
153155 $ length = $ this ->config ['otp ' ]['length ' ] ?? 6 ;
154156
155157 switch ($ type ) {
@@ -168,30 +170,31 @@ protected function generateNumericCode(int $length): string
168170 {
169171 $ min = pow (10 , $ length - 1 );
170172 $ max = pow (10 , $ length ) - 1 ;
173+
171174 return (string ) random_int ($ min , $ max );
172175 }
173176
174177 protected function generateAlphanumericCode (int $ length ): string
175178 {
176179 $ characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ' ;
177- $ code = '' ;
178-
180+ $ code = '' ;
181+
179182 for ($ i = 0 ; $ i < $ length ; $ i ++) {
180183 $ code .= $ characters [random_int (0 , strlen ($ characters ) - 1 )];
181184 }
182-
185+
183186 return $ code ;
184187 }
185188
186189 protected function generateAlphaCode (int $ length ): string
187190 {
188191 $ characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' ;
189- $ code = '' ;
190-
192+ $ code = '' ;
193+
191194 for ($ i = 0 ; $ i < $ length ; $ i ++) {
192195 $ code .= $ characters [random_int (0 , strlen ($ characters ) - 1 )];
193196 }
194-
197+
195198 return $ code ;
196199 }
197200
@@ -204,6 +207,7 @@ protected function isRateLimited(string $identifier): bool
204207
205208 $ key = "simple_otp_rate_limit: {$ identifier }" ;
206209 $ max = $ rateLimit ['max_attempts ' ] ?? 5 ;
210+
207211 return RateLimiter::tooManyAttempts ($ key , $ max );
208212 }
209213
@@ -214,9 +218,9 @@ protected function incrementRateLimit(string $identifier): void
214218 return ;
215219 }
216220
217- $ key = "simple_otp_rate_limit: {$ identifier }" ;
221+ $ key = "simple_otp_rate_limit: {$ identifier }" ;
218222 $ decayMinutes = $ rateLimit ['decay_minutes ' ] ?? 60 ;
219-
223+
220224 RateLimiter::hit ($ key , $ decayMinutes * 60 );
221225 }
222226
0 commit comments