Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verification code model can be customized #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions config/verification-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
21 changes: 18 additions & 3 deletions src/VerificationCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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', []);
Expand Down
44 changes: 44 additions & 0 deletions tests/Facade/CustomVerificationCodeClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace NextApps\VerificationCode\Tests\Facade;

use NextApps\VerificationCode\Models\VerificationCode;
use NextApps\VerificationCode\Tests\TestCase;
use NextApps\VerificationCode\VerificationCode as VerificationCodeFacade;
use RuntimeException;

class CustomVerificationCodeClassTest extends TestCase
{
/** @test */
public function it_returns_the_model_verification_code_class_by_default()
{
$this->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
{
}
Loading