1 | <?php |
||||||
2 | |||||||
3 | namespace PeriodicNotice\Notifications; |
||||||
4 | |||||||
5 | use Illuminate\Notifications\Messages\MailMessage; |
||||||
6 | use Illuminate\Support\Collection; |
||||||
7 | use Illuminate\Support\HtmlString; |
||||||
8 | |||||||
9 | class MailMessageBuilder |
||||||
10 | { |
||||||
11 | protected MailMessage $mailMessage; |
||||||
12 | |||||||
13 | protected Collection $entities; |
||||||
14 | |||||||
15 | 1 | public function __construct(?MailMessage $mailMessage = null) |
|||||
16 | { |
||||||
17 | 1 | $this->mailMessage = $mailMessage ?? new MailMessage(); |
|||||
18 | } |
||||||
19 | |||||||
20 | 1 | public static function make(...$args): static |
|||||
21 | { |
||||||
22 | 1 | return new static(...$args); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
23 | } |
||||||
24 | |||||||
25 | 1 | public function build(): MailMessage |
|||||
26 | { |
||||||
27 | 1 | $this->mailSubject() |
|||||
28 | 1 | ->mailGreeting() |
|||||
29 | 1 | ->mailSalutation() |
|||||
30 | 1 | ->mailContentBeforeList(); |
|||||
31 | |||||||
32 | 1 | $itemsCount = $this->entities->count(); |
|||||
33 | 1 | $counter = 0; |
|||||
34 | /** @var \PeriodicNotice\Contracts\SendableEntity $entity */ |
||||||
35 | 1 | foreach ($this->entities as $entity) { |
|||||
36 | 1 | $counter++; |
|||||
37 | |||||||
38 | 1 | $this->mailContentBeforeListItem($entity, $counter); |
|||||
39 | |||||||
40 | 1 | $this->mailMessage->line("[{$entity->notificationEntityTitle()}]({$entity->notificationEntityWebUrl()})") |
|||||
41 | 1 | ->line($entity->notificationEntityDescription()); |
|||||
42 | |||||||
43 | 1 | if ($counter < $itemsCount) { |
|||||
44 | 1 | $this->mailContentListItemSeparator($entity, $counter); |
|||||
45 | } |
||||||
46 | 1 | $this->mailContentAfterListItem($entity, $counter); |
|||||
47 | } |
||||||
48 | |||||||
49 | 1 | if (method_exists($this, 'mailContentAfterList')) { |
|||||
50 | 1 | $this->mailContentAfterList(); |
|||||
51 | } |
||||||
52 | |||||||
53 | 1 | return $this->mailMessage; |
|||||
54 | } |
||||||
55 | |||||||
56 | 1 | public function useEntries(Collection $entities): static |
|||||
57 | { |
||||||
58 | 1 | $this->entities = $entities; |
|||||
59 | |||||||
60 | 1 | return $this; |
|||||
61 | } |
||||||
62 | |||||||
63 | 1 | protected function mailSubject(): static |
|||||
64 | { |
||||||
65 | 1 | $this->mailMessage->subject(trans('periodic-notice::notification.mail.subject')); |
|||||
66 | |||||||
67 | 1 | return $this; |
|||||
68 | } |
||||||
69 | |||||||
70 | 1 | protected function mailGreeting(): static |
|||||
71 | { |
||||||
72 | 1 | $this->mailMessage->greeting(trans('periodic-notice::notification.mail.greeting')); |
|||||
73 | |||||||
74 | 1 | return $this; |
|||||
75 | } |
||||||
76 | |||||||
77 | 1 | protected function mailSalutation(): static |
|||||
78 | { |
||||||
79 | 1 | return $this; |
|||||
80 | } |
||||||
81 | |||||||
82 | 1 | protected function mailContentBeforeList(): static |
|||||
83 | { |
||||||
84 | 1 | $this->mailMessage->line(new HtmlString('<br>')); |
|||||
85 | |||||||
86 | 1 | return $this; |
|||||
87 | } |
||||||
88 | |||||||
89 | 1 | protected function mailContentAfterList(): static |
|||||
90 | { |
||||||
91 | 1 | $this->mailMessage->line(new HtmlString('<br>')) |
|||||
92 | 1 | ->action( |
|||||
93 | 1 | trans('periodic-notice::notification.mail.action_text'), |
|||||
94 | 1 | trans('periodic-notice::notification.mail.action_link') |
|||||
95 | 1 | ); |
|||||
96 | |||||||
97 | 1 | return $this; |
|||||
98 | } |
||||||
99 | |||||||
100 | 1 | protected function mailContentBeforeListItem($entity, int $itemNumber): static |
|||||
0 ignored issues
–
show
The parameter
$entity 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$itemNumber 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
101 | { |
||||||
102 | 1 | return $this; |
|||||
103 | } |
||||||
104 | |||||||
105 | 1 | protected function mailContentListItemSeparator($entity, int $itemNumber): static |
|||||
0 ignored issues
–
show
The parameter
$itemNumber 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$entity 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
106 | { |
||||||
107 | 1 | $this->mailMessage->line(new HtmlString('<br>')) |
|||||
108 | 1 | ->line('---------------') |
|||||
109 | 1 | ->line(new HtmlString('<br>')); |
|||||
110 | |||||||
111 | 1 | return $this; |
|||||
112 | } |
||||||
113 | |||||||
114 | 1 | protected function mailContentAfterListItem($entity, int $itemNumber): static |
|||||
0 ignored issues
–
show
The parameter
$itemNumber 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$entity 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
115 | { |
||||||
116 | 1 | return $this; |
|||||
117 | } |
||||||
118 | } |
||||||
119 |