EmailChangeNotification::toMail()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 3
rs 9.9
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
Unused Code introduced by
The parameter $notifiable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    public function via(/** @scrutinizer ignore-unused */ $notifiable)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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