dev-think-one /
laravel-email-change-verification
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EmailChangeVerification\Notifications; |
||
| 4 | |||
| 5 | use Illuminate\Notifications\Messages\MailMessage; |
||
| 6 | use Illuminate\Notifications\Notification; |
||
| 7 | |||
| 8 | class EmailChangeNotification extends Notification |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The email verification token. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | public string $token; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The new email. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public string $newEmail; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The callback that should be used to create the verification email URL. |
||
| 26 | * |
||
| 27 | * @var \Closure|null |
||
| 28 | */ |
||
| 29 | public static $createUrlCallback; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The callback that should be used to build the mail message. |
||
| 33 | * |
||
| 34 | * @var \Closure|null |
||
| 35 | */ |
||
| 36 | public static $toMailCallback; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create a notification instance. |
||
| 40 | * |
||
| 41 | * @param string $token |
||
| 42 | * @param string $newEmail |
||
| 43 | */ |
||
| 44 | 5 | public function __construct(string $token, string $newEmail) |
|
| 45 | { |
||
| 46 | 5 | $this->token = $token; |
|
| 47 | 5 | $this->newEmail = $newEmail; |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the notification's channels. |
||
| 52 | * |
||
| 53 | * @param mixed $notifiable |
||
| 54 | * |
||
| 55 | * @return array|string |
||
| 56 | */ |
||
| 57 | 5 | public function via($notifiable) |
|
|
0 ignored issues
–
show
|
|||
| 58 | { |
||
| 59 | 5 | return [ 'mail' ]; |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Build the mail representation of the notification. |
||
| 64 | * |
||
| 65 | * @param mixed $notifiable |
||
| 66 | * |
||
| 67 | * @return \Illuminate\Notifications\Messages\MailMessage |
||
| 68 | */ |
||
| 69 | 3 | public function toMail($notifiable) |
|
| 70 | { |
||
| 71 | 3 | if (static::$toMailCallback) { |
|
| 72 | 1 | return call_user_func(static::$toMailCallback, $notifiable, $this->token); |
|
| 73 | } |
||
| 74 | |||
| 75 | 2 | if (static::$createUrlCallback) { |
|
| 76 | 1 | $url = call_user_func(static::$createUrlCallback, $notifiable, $this->token); |
|
| 77 | } else { |
||
| 78 | 1 | $url = url(route('email.change.verification', [ |
|
| 79 | 1 | 'token' => $this->token, |
|
| 80 | 1 | 'email' => $notifiable->getEmailForChangeEmail(), |
|
| 81 | 1 | 'new_email' => $this->newEmail, |
|
| 82 | 1 | ], false)); |
|
| 83 | } |
||
| 84 | |||
| 85 | 2 | return $this->buildMailMessage($url); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the email verification notification mail message for the given URL. |
||
| 90 | * |
||
| 91 | * @param string $url |
||
| 92 | * |
||
| 93 | * @return \Illuminate\Notifications\Messages\MailMessage |
||
| 94 | */ |
||
| 95 | 2 | protected function buildMailMessage($url) |
|
| 96 | { |
||
| 97 | 2 | return ( new MailMessage ) |
|
| 98 | 2 | ->subject(__('Email Change Verification')) |
|
| 99 | 2 | ->line(__('You are receiving this email because we received a email change request for your account.')) |
|
| 100 | 2 | ->line(__('New Email is: :email.', [ 'email' => $this->newEmail ])) |
|
| 101 | 2 | ->action(__('Accept Changes'), $url) |
|
| 102 | 2 | ->line(__('This link will expire in :count minutes.', [ 'count' => config('email-change-verification.brokers.' . config('email-change-verification.default') . '.expire') ])) |
|
| 103 | 2 | ->line(__('If you did not request a email change, no further action is required.')); |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set a callback that should be used when creating the new email verification button URL. |
||
| 108 | * |
||
| 109 | * @param \Closure $callback |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | 1 | public static function createUrlUsing($callback) |
|
| 114 | { |
||
| 115 | 1 | static::$createUrlCallback = $callback; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set a callback that should be used when building the notification mail message. |
||
| 120 | * |
||
| 121 | * @param \Closure $callback |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | 1 | public static function toMailUsing($callback) |
|
| 126 | { |
||
| 127 | 1 | static::$toMailCallback = $callback; |
|
| 128 | } |
||
| 129 | } |
||
| 130 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.