Skip to content

Commit c62a617

Browse files
authored
Merge pull request #4 from nextapps-be/feature/add_custom_notification_class
Feature/add custom notification class
2 parents 4248f84 + 17cd4d4 commit c62a617

File tree

8 files changed

+105
-8
lines changed

8 files changed

+105
-8
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ You sometimes may want to exclude one or more characters from the selected or de
6060
### Expire hours
6161
The amount of hours it takes for a verification code to expire. You're free to increase this in the config.
6262

63+
### Custom Notification
64+
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.
65+
66+
Lastly set the config value `notification` to the full path of your newly created notification.
67+
6368
### Queue
6469
In specific cases you may want to put the verification code notifications on a queue. This can be done by defining the queue in the config (e.g. `queue => 'notifications'`).
6570

config/verification-code.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@
4848
*/
4949
'expire_hours' => 1,
5050

51+
/*
52+
|--------------------------------------------------------------------------
53+
| Custom notification
54+
|--------------------------------------------------------------------------
55+
|
56+
| This class contains the notification sent to users upon creation
57+
| of the verification code.
58+
|
59+
| It should implement the following interfaces:
60+
| - Illuminate\Contracts\Queue\ShouldQueue
61+
| - NextApps\VerificationCode\Notifications\VerificationCodeCreatedInterface
62+
|
63+
*/
64+
'notification' => \NextApps\VerificationCode\Notifications\VerificationCodeCreated::class,
65+
5166
/*
5267
|--------------------------------------------------------------------------
5368
| Queue
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NextApps\VerificationCode\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidClassException extends Exception
8+
{
9+
public static function handle(): self
10+
{
11+
return new static('The notification should extend the VerificationCodeCreatedInterface.');
12+
}
13+
}

src/Notifications/VerificationCodeCreated.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Notifications\Messages\MailMessage;
88
use Illuminate\Notifications\Notification;
99

10-
class VerificationCodeCreated extends Notification implements ShouldQueue
10+
class VerificationCodeCreated extends Notification implements ShouldQueue, VerificationCodeCreatedInterface
1111
{
1212
use Queueable;
1313

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NextApps\VerificationCode\Notifications;
4+
5+
interface VerificationCodeCreatedInterface
6+
{
7+
/**
8+
* Create a new message instance.
9+
*
10+
* @param string $code
11+
*/
12+
public function __construct(string $code);
13+
}

src/VerificationCodeManager.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,43 @@
44

55
use Illuminate\Support\Facades\Hash;
66
use Illuminate\Support\Facades\Notification;
7+
use NextApps\VerificationCode\Exceptions\InvalidClassException;
78
use NextApps\VerificationCode\Models\VerificationCode;
89
use NextApps\VerificationCode\Notifications\VerificationCodeCreated;
10+
use NextApps\VerificationCode\Notifications\VerificationCodeCreatedInterface;
911

1012
class VerificationCodeManager
1113
{
1214
/**
1315
* Create and send a verification code via mail.
1416
*
1517
* @param string $verifiable
18+
* @param string $channel
1619
*
1720
* @return void
1821
*/
19-
public function send($verifiable)
22+
public function send($verifiable, $channel = 'mail')
2023
{
2124
$testVerifiables = config('verification-code.test_verifiables', []);
25+
$notificationClass = config('verification-code.notification', VerificationCodeCreated::class);
26+
$queue = config('verification-code.queue', null);
27+
28+
if (! is_subclass_of($notificationClass, VerificationCodeCreatedInterface::class)) {
29+
throw InvalidClassException::handle();
30+
}
2231

2332
if (in_array($verifiable, $testVerifiables)) {
2433
return;
2534
}
2635

2736
$code = VerificationCode::createFor($verifiable);
2837

29-
if (config('verification-code.queue') !== null) {
30-
Notification::route('mail', $verifiable)
31-
->notify((new VerificationCodeCreated($code))
32-
->onQueue(config('verification-code.queue')));
38+
if ($queue !== null) {
39+
Notification::route($channel, $verifiable)
40+
->notify((new $notificationClass($code))->onQueue($queue));
3341
} else {
34-
Notification::route('mail', $verifiable)
35-
->notifyNow((new VerificationCodeCreated($code)));
42+
Notification::route($channel, $verifiable)
43+
->notifyNow(new $notificationClass($code));
3644
}
3745
}
3846

tests/VerificationCodeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Illuminate\Notifications\AnonymousNotifiable;
66
use Illuminate\Support\Facades\Notification;
77
use Illuminate\Support\Str;
8+
use NextApps\VerificationCode\Exceptions\InvalidClassException;
89
use NextApps\VerificationCode\Models\VerificationCode;
910
use NextApps\VerificationCode\Notifications\VerificationCodeCreated;
11+
use NextApps\VerificationCode\Notifications\WrongNotification;
1012
use NextApps\VerificationCode\VerificationCode as VerificationCodeFacade;
1113

1214
class VerificationCodeTest extends TestCase
@@ -173,4 +175,33 @@ public function it_returns_false_if_test_verifiable_with_empty_test_code()
173175

174176
$this->assertFalse(VerificationCodeFacade::verify('', $verifiable));
175177
}
178+
179+
/** @test */
180+
public function it_works_if_notification_config_value_is_empty()
181+
{
182+
$email = $this->faker->safeEmail;
183+
184+
VerificationCodeFacade::send($email);
185+
186+
$this->assertNotNull(VerificationCode::where('verifiable', $email)->first());
187+
188+
Notification::assertSentTo(
189+
new AnonymousNotifiable,
190+
VerificationCodeCreated::class,
191+
function ($notification, $channels, $notifiable) use ($email) {
192+
return $notifiable->routes['mail'] === $email;
193+
});
194+
}
195+
196+
/** @test */
197+
public function it_throws_an_error_if_notification_does_not_extend_the_verification_notification_class()
198+
{
199+
$this->expectException(InvalidClassException::class);
200+
$this->expectExceptionMessage('The notification should extend the VerificationCodeCreatedInterface.');
201+
$email = $this->faker->safeEmail;
202+
203+
config()->set('verification-code.notification', WrongNotification::class);
204+
205+
VerificationCodeFacade::send($email);
206+
}
176207
}

tests/stubs/WrongNotification.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace NextApps\VerificationCode\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Notifications\Notification;
8+
9+
class WrongNotification extends Notification implements ShouldQueue
10+
{
11+
use Queueable;
12+
}

0 commit comments

Comments
 (0)