Passed
Push — master ( 3e77af...7c612c )
by Brian
02:49
created

ResetPassword::toMailUsing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Modules\Admins\Notifications\Auth;
4
5
use Illuminate\Notifications\Messages\MailMessage;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Support\Facades\Lang;
8
9
/**
10
 * @see \Illuminate\Auth\Notifications\ResetPassword
11
 */
12
class ResetPassword extends Notification
13
{
14
    /**
15
     * The password reset token.
16
     *
17
     * @var string
18
     */
19
    public $token;
20
21
    /**
22
     * The callback that should be used to create the reset password URL.
23
     *
24
     * @var (\Closure(mixed, string): string)|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\Closure(mixed, string): string)|null at position 1 could not be parsed: Expected ')' at position 1, but found '\Closure'.
Loading history...
25
     */
26
    public static $createUrlCallback;
27
28
    /**
29
     * The callback that should be used to build the mail message.
30
     *
31
     * @var (\Closure(mixed, string): \Illuminate\Notifications\Messages\MailMessage)|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\Closure(mixed, string)...sages\MailMessage)|null at position 1 could not be parsed: Expected ')' at position 1, but found '\Closure'.
Loading history...
32
     */
33
    public static $toMailCallback;
34
35
    /**
36
     * Create a notification instance.
37
     *
38
     * @param  string  $token
39
     * @return void
40
     */
41
    public function __construct($token)
42
    {
43
        $this->token = $token;
44
    }
45
46
    /**
47
     * Get the notification's channels.
48
     *
49
     * @param  mixed  $notifiable
50
     * @return array|string
51
     */
52
    public function via($notifiable)
53
    {
54
        return ['mail'];
55
    }
56
57
    /**
58
     * Build the mail representation of the notification.
59
     *
60
     * @param  mixed  $notifiable
61
     * @return \Illuminate\Notifications\Messages\MailMessage
62
     */
63
    public function toMail($notifiable)
64
    {
65
        if (static::$toMailCallback) {
66
            return call_user_func(static::$toMailCallback, $notifiable, $this->token);
67
        }
68
69
        return $this->buildMailMessage($this->resetUrl($notifiable));
70
    }
71
72
    /**
73
     * Get the reset password notification mail message for the given URL.
74
     *
75
     * @param  string  $url
76
     * @return \Illuminate\Notifications\Messages\MailMessage
77
     */
78
    protected function buildMailMessage($url)
79
    {
80
        return (new MailMessage)
81
            ->subject(Lang::get('Reset Password Notification'))
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facad...Password Notification') can also be of type array; however, parameter $subject of Illuminate\Notifications...impleMessage::subject() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

81
            ->subject(/** @scrutinizer ignore-type */ Lang::get('Reset Password Notification'))
Loading history...
82
            ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
83
            ->action(Lang::get('Reset Password'), $url)
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facad...::get('Reset Password') can also be of type array; however, parameter $text of Illuminate\Notifications...SimpleMessage::action() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
            ->action(/** @scrutinizer ignore-type */ Lang::get('Reset Password'), $url)
Loading history...
84
            ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.admins.expire')]))
85
            ->line(Lang::get('If you did not request a password reset, no further action is required.'));
86
    }
87
88
    /**
89
     * Get the reset URL for the given notifiable.
90
     *
91
     * @param  mixed  $notifiable
92
     * @return string
93
     */
94
    protected function resetUrl($notifiable)
95
    {
96
        if (static::$createUrlCallback) {
97
            return call_user_func(static::$createUrlCallback, $notifiable, $this->token);
98
        }
99
100
        return url(route('admin.password.reset', [
101
            'token' => $this->token,
102
            'email' => $notifiable->getEmailForPasswordReset(),
103
        ], false));
104
    }
105
106
    /**
107
     * Set a callback that should be used when creating the reset password button URL.
108
     *
109
     * @param  \Closure(mixed, string): string  $callback
110
     * @return void
111
     */
112
    public static function createUrlUsing($callback)
113
    {
114
        static::$createUrlCallback = $callback;
115
    }
116
117
    /**
118
     * Set a callback that should be used when building the notification mail message.
119
     *
120
     * @param  \Closure(mixed, string): \Illuminate\Notifications\Messages\MailMessage  $callback
121
     * @return void
122
     */
123
    public static function toMailUsing($callback)
124
    {
125
        static::$toMailCallback = $callback;
126
    }
127
}
128