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