1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\WelcomeNotification; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Foundation\Auth\User; |
7
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
8
|
|
|
use Illuminate\Notifications\Notification; |
9
|
|
|
use Illuminate\Support\Facades\Lang; |
10
|
|
|
use Illuminate\Support\Facades\URL; |
11
|
|
|
|
12
|
|
|
class WelcomeNotification extends Notification |
13
|
|
|
{ |
14
|
|
|
/** @var \Illuminate\Foundation\Auth\User */ |
15
|
|
|
public $user; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
public $showWelcomeFormUrl; |
19
|
|
|
|
20
|
|
|
/** @var \Closure|null */ |
21
|
|
|
public static $toMailCallback; |
22
|
|
|
|
23
|
|
|
/** @var \Carbon\Carbon */ |
24
|
|
|
public $validUntil; |
25
|
|
|
|
26
|
|
|
public function __construct(Carbon $validUntil) |
27
|
|
|
{ |
28
|
|
|
$this->validUntil = $validUntil; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function via($notifiable) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return ['mail']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function toMail($notifiable) |
37
|
|
|
{ |
38
|
|
|
$this->initializeNotificationProperties($notifiable); |
39
|
|
|
|
40
|
|
|
if (static::$toMailCallback) { |
41
|
|
|
return call_user_func(static::$toMailCallback, $notifiable); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->buildWelcomeNotificationMessage(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function buildWelcomeNotificationMessage(): MailMessage |
48
|
|
|
{ |
49
|
|
|
return (new MailMessage) |
50
|
|
|
->subject(Lang::get('Welcome')) |
51
|
|
|
->line(Lang::get('You are receiving this email because an account was created for you.')) |
52
|
|
|
->action(Lang::get('Set initial password'), $this->showWelcomeFormUrl) |
53
|
|
|
->line(Lang::get('This welcome link will expire in :count minutes.', ['count' => $this->validUntil->diffInRealMinutes()])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function toMailUsing($callback) |
57
|
|
|
{ |
58
|
|
|
static::$toMailCallback = $callback; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function initializeNotificationProperties(User $user) |
62
|
|
|
{ |
63
|
|
|
$this->user = $user; |
64
|
|
|
|
65
|
|
|
$this->user->welcome_valid_until = $this->validUntil; |
66
|
|
|
$this->user->save(); |
67
|
|
|
|
68
|
|
|
$this->showWelcomeFormUrl = URL::temporarySignedRoute( |
69
|
|
|
'welcome', |
70
|
|
|
$this->validUntil, |
71
|
|
|
['user' => $user->id] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.