From a14d961355c8726cf0582c80df29b17356cdec8d Mon Sep 17 00:00:00 2001 From: Bart Kuenen Date: Wed, 23 Aug 2023 14:15:20 +0200 Subject: [PATCH 1/2] verification code model can be customized --- README.md | 3 ++ config/verification-code.php | 13 ++++++ src/Console/PruneCommand.php | 4 +- src/VerificationCodeManager.php | 21 +++++++-- .../CustomVerificationCodeClassTest.php | 44 +++++++++++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 tests/Facade/CustomVerificationCodeClassTest.php diff --git a/README.md b/README.md index 319523e..cd908fb 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,9 @@ change this setting to a different number (or to `null` if you want unlimited co ### Custom Notification If you want to use a custom notification to send the verification code, you can create your own notification class which should extend the `VerificationCodeCreatedInterface`. Make sure you don't forget to pass the verification code to the mail. +### Custom Model +If you want to use a custom verification code model, you can create your own verification code class which should extend the `VerificationCode`. + ### Queue If your notification is queueable, you can define the queue that will be used for the notification. diff --git a/config/verification-code.php b/config/verification-code.php index b95ffc0..5fb88cc 100644 --- a/config/verification-code.php +++ b/config/verification-code.php @@ -61,6 +61,19 @@ */ 'notification' => \NextApps\VerificationCode\Notifications\VerificationCodeCreated::class, + /* + |-------------------------------------------------------------------------- + | Custom Model + |-------------------------------------------------------------------------- + | + | Here you can customize the VerificationCode class that will be used + | + | It should extend the package class: + | - \NextApps\VerificationCode\Models\VerificationCode + | + */ + 'model' => \NextApps\VerificationCode\Models\VerificationCode::class, + /* |-------------------------------------------------------------------------- | Queue diff --git a/src/Console/PruneCommand.php b/src/Console/PruneCommand.php index 74deb8f..521ebbb 100644 --- a/src/Console/PruneCommand.php +++ b/src/Console/PruneCommand.php @@ -3,7 +3,7 @@ namespace NextApps\VerificationCode\Console; use Illuminate\Console\Command; -use NextApps\VerificationCode\Models\VerificationCode; +use NextApps\VerificationCode\VerificationCode; class PruneCommand extends Command { @@ -13,7 +13,7 @@ class PruneCommand extends Command public function handle() : void { - VerificationCode::query() + VerificationCode::getModelClass()::query() ->where('created_at', '<=', now()->subHours($this->option('hours'))) ->where('expires_at', '<', now()) ->delete(); diff --git a/src/VerificationCodeManager.php b/src/VerificationCodeManager.php index 6c07ca1..2f45dac 100644 --- a/src/VerificationCodeManager.php +++ b/src/VerificationCodeManager.php @@ -21,7 +21,7 @@ public function send(string $verifiable, string $channel = 'mail') : void return; } - $code = VerificationCode::createFor($verifiable); + $code = $this->getModelClass()::createFor($verifiable); $notificationClass = $this->getNotificationClass(); $notification = new $notificationClass($code); @@ -39,7 +39,9 @@ public function verify(string $code, string $verifiable, bool $deleteAfterVerifi return $this->isTestCode($code); } - $codeIsValid = VerificationCode::query() + $modelClass = $this->getModelClass(); + + $codeIsValid = $modelClass::query() ->for($verifiable) ->notExpired() ->cursor() @@ -52,12 +54,25 @@ public function verify(string $code, string $verifiable, bool $deleteAfterVerifi } if ($deleteAfterVerification) { - VerificationCode::for($verifiable)->delete(); + $modelClass::for($verifiable)->delete(); } return true; } + public function getModelClass() : string + { + $modelClass = config('verification-code.model', VerificationCode::class); + + if (! is_a($modelClass, VerificationCode::class, true)) { + $model = VerificationCode::class; + + throw new RuntimeException("The model class must extend the `{$model}` class"); + } + + return $modelClass; + } + protected function isTestVerifiable(string $verifiable) : bool { $testVerifiables = config('verification-code.test_verifiables', []); diff --git a/tests/Facade/CustomVerificationCodeClassTest.php b/tests/Facade/CustomVerificationCodeClassTest.php new file mode 100644 index 0000000..18d4a6a --- /dev/null +++ b/tests/Facade/CustomVerificationCodeClassTest.php @@ -0,0 +1,44 @@ +assertSame(VerificationCode::class ,VerificationCodeFacade::getModelClass()); + } + + /** @test */ + public function it_returns_the_model_class_that_was_set_in_the_config() + { + config()->set('verification-code.model', ModelDoesExtendVerificationCode::class); + + $this->assertSame(ModelDoesExtendVerificationCode::class ,VerificationCodeFacade::getModelClass()); + } + + /** @test */ + public function it_throws_exception_if_notification_does_not_extend_the_verification_notification_class() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('The model class must extend the `NextApps\VerificationCode\Models\VerificationCode` class'); + + config()->set('verification-code.model', ModelDoesNotExtendVerificationCode::class); + + VerificationCodeFacade::getModelClass(); + } +} + +class ModelDoesExtendVerificationCode extends VerificationCode +{ +} + +class ModelDoesNotExtendVerificationCode +{ +} From b0693435e77b66af82bd515b7f88a74e0d4167c8 Mon Sep 17 00:00:00 2001 From: Bart Kuenen Date: Wed, 23 Aug 2023 14:19:09 +0200 Subject: [PATCH 2/2] fix linting --- tests/Facade/CustomVerificationCodeClassTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Facade/CustomVerificationCodeClassTest.php b/tests/Facade/CustomVerificationCodeClassTest.php index 18d4a6a..c59cf1f 100644 --- a/tests/Facade/CustomVerificationCodeClassTest.php +++ b/tests/Facade/CustomVerificationCodeClassTest.php @@ -2,9 +2,9 @@ namespace NextApps\VerificationCode\Tests\Facade; -use NextApps\VerificationCode\VerificationCode as VerificationCodeFacade; use NextApps\VerificationCode\Models\VerificationCode; use NextApps\VerificationCode\Tests\TestCase; +use NextApps\VerificationCode\VerificationCode as VerificationCodeFacade; use RuntimeException; class CustomVerificationCodeClassTest extends TestCase @@ -12,7 +12,7 @@ class CustomVerificationCodeClassTest extends TestCase /** @test */ public function it_returns_the_model_verification_code_class_by_default() { - $this->assertSame(VerificationCode::class ,VerificationCodeFacade::getModelClass()); + $this->assertSame(VerificationCode::class, VerificationCodeFacade::getModelClass()); } /** @test */ @@ -20,7 +20,7 @@ public function it_returns_the_model_class_that_was_set_in_the_config() { config()->set('verification-code.model', ModelDoesExtendVerificationCode::class); - $this->assertSame(ModelDoesExtendVerificationCode::class ,VerificationCodeFacade::getModelClass()); + $this->assertSame(ModelDoesExtendVerificationCode::class, VerificationCodeFacade::getModelClass()); } /** @test */