1 | <?php |
||
2 | |||
3 | namespace Sfneal\PostOffice\Mailables; |
||
4 | |||
5 | use Illuminate\Bus\Queueable; |
||
6 | use Illuminate\Queue\SerializesModels; |
||
7 | |||
8 | abstract class Mailable extends \Illuminate\Mail\Mailable |
||
9 | { |
||
10 | // todo: refactor to allow interfaces to set properties without passing to constructor |
||
11 | // todo: refactor to use basic mailable building instead of views (new class?) |
||
12 | use Queueable, SerializesModels; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
13 | |||
14 | /** |
||
15 | * @var string View blade to use for mailable content |
||
16 | */ |
||
17 | public $view; |
||
18 | |||
19 | /** |
||
20 | * @var string First line of the email body |
||
21 | */ |
||
22 | public $greeting; |
||
23 | |||
24 | /** |
||
25 | * @var string Recipient email address |
||
26 | */ |
||
27 | public $email; |
||
28 | |||
29 | /** |
||
30 | * @var string Subject of the email |
||
31 | */ |
||
32 | public $title; |
||
33 | |||
34 | /** |
||
35 | * @var array|string Array of strings to be displayed as paragraphs |
||
36 | */ |
||
37 | public $messages; |
||
38 | |||
39 | /** |
||
40 | * @var array Action button url and text |
||
41 | */ |
||
42 | public $call_to_action; |
||
43 | |||
44 | /** |
||
45 | * Mailable constructor. |
||
46 | * |
||
47 | * @param string|null $greeting |
||
48 | * @param string|null $email |
||
49 | * @param string|null $title |
||
50 | * @param null $messages |
||
0 ignored issues
–
show
|
|||
51 | * @param array|null $call_to_action |
||
52 | * @param string|null $view |
||
53 | */ |
||
54 | public function __construct(string $greeting = null, |
||
55 | string $email = null, |
||
56 | string $title = null, |
||
57 | $messages = null, |
||
58 | array $call_to_action = null, |
||
59 | string $view = null) |
||
60 | { |
||
61 | $this->greeting = $greeting ?? $this->greeting; |
||
62 | $this->email = $email ?? $this->email; |
||
63 | $this->title = $title ?? $this->title; |
||
64 | /** @phpstan-ignore-next-line */ |
||
65 | $this->messages = $messages ?? $this->messages; |
||
66 | $this->call_to_action = $call_to_action ?? $this->call_to_action; |
||
67 | |||
68 | $this->view = $this->setView($view); |
||
69 | $this->onQueue(config('post-office.queue')); |
||
70 | $this->onConnection(config('post-office.driver')); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Build the message. |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function build(): self |
||
79 | { |
||
80 | return $this->subject($this->title) |
||
81 | ->view($this->view, [ |
||
82 | 'greeting' => $this->greeting, |
||
83 | 'email' => $this->email, |
||
84 | 'title' => $this->title, |
||
85 | 'messages' => $this->messages, |
||
86 | 'call_to_action' => $this->call_to_action, |
||
87 | ]); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Set the Mailable's $view property. |
||
92 | * |
||
93 | * 1. $view parameter passed to the constructor |
||
94 | * 2. $view property declared in child `AbstractMailable` extension |
||
95 | * 3. 'post-office.mailable.view' config value (defaults to post-office::email) |
||
96 | * |
||
97 | * @param string|null $view |
||
98 | * @return string |
||
99 | */ |
||
100 | private function setView(string $view = null): string |
||
101 | { |
||
102 | return $view ?? $this->view ?? config('post-office.mailables.view'); |
||
103 | } |
||
104 | } |
||
105 |