|
4 | 4 |
|
5 | 5 | use Illuminate\Support\Facades\Hash;
|
6 | 6 | use Illuminate\Support\Facades\Notification;
|
| 7 | +use NextApps\VerificationCode\Exceptions\InvalidClassException; |
7 | 8 | use NextApps\VerificationCode\Models\VerificationCode;
|
8 | 9 | use NextApps\VerificationCode\Notifications\VerificationCodeCreated;
|
| 10 | +use NextApps\VerificationCode\Notifications\VerificationCodeCreatedInterface; |
9 | 11 |
|
10 | 12 | class VerificationCodeManager
|
11 | 13 | {
|
12 | 14 | /**
|
13 | 15 | * Create and send a verification code via mail.
|
14 | 16 | *
|
15 | 17 | * @param string $verifiable
|
| 18 | + * @param string $channel |
16 | 19 | *
|
17 | 20 | * @return void
|
18 | 21 | */
|
19 |
| - public function send($verifiable) |
| 22 | + public function send($verifiable, $channel = 'mail') |
20 | 23 | {
|
21 | 24 | $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 | + } |
22 | 31 |
|
23 | 32 | if (in_array($verifiable, $testVerifiables)) {
|
24 | 33 | return;
|
25 | 34 | }
|
26 | 35 |
|
27 | 36 | $code = VerificationCode::createFor($verifiable);
|
28 | 37 |
|
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)); |
33 | 41 | } else {
|
34 |
| - Notification::route('mail', $verifiable) |
35 |
| - ->notifyNow((new VerificationCodeCreated($code))); |
| 42 | + Notification::route($channel, $verifiable) |
| 43 | + ->notifyNow(new $notificationClass($code)); |
36 | 44 | }
|
37 | 45 | }
|
38 | 46 |
|
|
0 commit comments