1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2021 Hilmi Erdem KEREN |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Erdemkeren\Otp; |
9
|
|
|
|
10
|
|
|
use Illuminate\Bus\Queueable; |
11
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
12
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
13
|
|
|
use Illuminate\Notifications\Notification; |
14
|
|
|
use Illuminate\Support\Traits\Macroable; |
15
|
|
|
use function is_array; |
16
|
|
|
|
17
|
|
|
class TokenNotification extends Notification implements ShouldQueue |
18
|
|
|
{ |
19
|
|
|
use Queueable, Macroable; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The token. |
23
|
|
|
* |
24
|
|
|
* @var OtpToken |
25
|
|
|
*/ |
26
|
|
|
public OtpToken $token; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* TokenNotification constructor. |
30
|
|
|
* |
31
|
|
|
* @param OtpToken $token |
32
|
|
|
*/ |
33
|
|
|
public function __construct(OtpToken $token) |
34
|
|
|
{ |
35
|
6 |
|
$this->token = $token; |
36
|
|
|
} |
37
|
6 |
|
|
38
|
6 |
|
/** |
39
|
|
|
* Get the notification's delivery channels. |
40
|
|
|
* |
41
|
|
|
* @param mixed $notifiable |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function via(mixed $notifiable): array |
45
|
|
|
{ |
46
|
|
|
$channels = $this->notifiableHasOtpChannels($notifiable) |
47
|
2 |
|
? $notifiable->otpChannels() |
48
|
|
|
: config('otp.default_channels'); |
|
|
|
|
49
|
2 |
|
|
50
|
1 |
|
return is_array($channels) |
51
|
2 |
|
? $channels |
52
|
|
|
: array_map('trim', explode(',', $channels)); |
53
|
2 |
|
} |
54
|
1 |
|
|
55
|
2 |
|
/** |
56
|
|
|
* Get the mail presentation of the notification. |
57
|
|
|
* |
58
|
|
|
* @return MailMessage |
59
|
|
|
*/ |
60
|
|
|
public function toMail(): MailMessage |
61
|
|
|
{ |
62
|
|
|
$plainText = $this->token->plainText(); |
63
|
1 |
|
|
64
|
|
|
return (new MailMessage()) |
65
|
1 |
|
->subject(trans('erdemkeren.otp.token-notification.email.subject')) |
|
|
|
|
66
|
1 |
|
->subject(trans('erdemkeren.otp.token-notification.email.greeting')) |
67
|
1 |
|
->subject(trans('erdemkeren.otp.token-notification.email.line1', $plainText)) |
68
|
1 |
|
->subject(trans('erdemkeren.otp.token-notification.email.line2', $plainText)) |
69
|
1 |
|
->subject(trans('erdemkeren.otp.token-notification.email.line3', $plainText)); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the sms presentation of the notification. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function toSms(): string |
78
|
1 |
|
{ |
79
|
|
|
return trans('erdemkeren.otp.token-notification.sms', $this->token->plainText()); |
|
|
|
|
80
|
|
|
} |
81
|
1 |
|
|
82
|
1 |
|
/** |
83
|
|
|
* Determine if the notifiable has otp channels or not. |
84
|
|
|
* |
85
|
|
|
* @param mixed $notifiable |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
private function notifiableHasOtpChannels(mixed $notifiable): bool |
89
|
|
|
{ |
90
|
|
|
return ! is_null($notifiable) |
91
|
|
|
&& method_exists($notifiable, 'otpChannels') |
92
|
|
|
&& ! empty($notifiable->otpChannels()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|