@@ -40,7 +40,10 @@ public function generate(string $identifier, string $recipient, array $options =
4040 'options ' => $ options ,
4141 ];
4242
43- if (!$ this ->storage ->store ($ identifier , $ code , $ this ->config ['ttl ' ], $ metadata )) {
43+ $ ttlMinutes = $ this ->config ['otp ' ]['ttl ' ] ?? 5 ;
44+ $ storeResult = $ this ->storage ->store ($ identifier , $ code , $ ttlMinutes , $ metadata );
45+
46+ if (!$ storeResult ) {
4447 throw new OTPException ('Failed to store OTP code ' );
4548 }
4649
@@ -60,7 +63,7 @@ public function generate(string $identifier, string $recipient, array $options =
6063 'success ' => true ,
6164 'message ' => $ this ->getMessage ('sent ' ),
6265 'identifier ' => $ identifier ,
63- 'expires_at ' => now ()->addMinutes ($ this -> config [ ' ttl ' ] ),
66+ 'expires_at ' => now ()->addMinutes ($ ttlMinutes ),
6467 ];
6568 }
6669
@@ -78,7 +81,8 @@ public function verify(string $identifier, string $code): bool
7881 }
7982
8083 // Check max attempts
81- if ($ otpData ['attempts ' ] >= $ this ->config ['max_attempts ' ]) {
84+ $ maxAttempts = $ this ->config ['otp ' ]['max_attempts ' ] ?? 3 ;
85+ if ($ otpData ['attempts ' ] >= $ maxAttempts ) {
8286 $ this ->storage ->delete ($ identifier );
8387 throw new OTPException ($ this ->getMessage ('max_attempts ' ));
8488 }
@@ -145,8 +149,8 @@ public function delete(string $identifier): bool
145149
146150 protected function generateCode (): string
147151 {
148- $ type = $ this ->config ['type ' ];
149- $ length = $ this ->config ['length ' ];
152+ $ type = $ this ->config ['otp ' ][ ' type ' ] ?? ' numeric ' ;
153+ $ length = $ this ->config ['otp ' ][ ' length ' ] ?? 6 ;
150154
151155 switch ($ type ) {
152156 case 'numeric ' :
@@ -193,23 +197,25 @@ protected function generateAlphaCode(int $length): string
193197
194198 protected function isRateLimited (string $ identifier ): bool
195199 {
196- if (!$ this ->config ['rate_limit ' ]['enabled ' ]) {
200+ $ rateLimit = $ this ->config ['otp ' ]['rate_limit ' ] ?? ['enabled ' => false ];
201+ if (!($ rateLimit ['enabled ' ] ?? false )) {
197202 return false ;
198203 }
199204
200205 $ key = "simple_otp_rate_limit: {$ identifier }" ;
201-
202- return RateLimiter::tooManyAttempts ($ key , $ this -> config [ ' rate_limit ' ][ ' max_attempts ' ] );
206+ $ max = $ rateLimit [ ' max_attempts ' ] ?? 5 ;
207+ return RateLimiter::tooManyAttempts ($ key , $ max );
203208 }
204209
205210 protected function incrementRateLimit (string $ identifier ): void
206211 {
207- if (!$ this ->config ['rate_limit ' ]['enabled ' ]) {
212+ $ rateLimit = $ this ->config ['otp ' ]['rate_limit ' ] ?? ['enabled ' => false ];
213+ if (!($ rateLimit ['enabled ' ] ?? false )) {
208214 return ;
209215 }
210216
211217 $ key = "simple_otp_rate_limit: {$ identifier }" ;
212- $ decayMinutes = $ this -> config [ ' rate_limit ' ][ ' decay_minutes ' ];
218+ $ decayMinutes = $ rateLimit [ ' decay_minutes ' ] ?? 60 ;
213219
214220 RateLimiter::hit ($ key , $ decayMinutes * 60 );
215221 }
0 commit comments