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