Passed
Push — main ( db810f...4cd58e )
by Dimitri
12:03
created

Mailable   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 52
c 2
b 1
f 0
dl 0
loc 212
rs 10
wmc 28

12 Methods

Rating   Name   Duplication   Size   Complexity  
A priority() 0 3 1
F send() 0 56 16
A subject() 0 3 1
A replyTo() 0 3 1
A with() 0 3 1
A bcc() 0 3 1
A cc() 0 3 1
A data() 0 11 2
A from() 0 3 1
A attachments() 0 3 1
A headers() 0 3 1
A content() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Mail;
13
14
use ReflectionClass;
15
use ReflectionProperty;
16
17
abstract class Mailable
18
{
19
    /**
20
     * Définition des pièces jointes du mail
21
     */
22
    public function attachments(): array
23
    {
24
        return [];
25
    }
26
27
    /**
28
     * Définition des adresses de copie (BCC) au mail
29
     *
30
     * @return array<string, string>|string[]
31
     *
32
     * @example
33
     * ```php
34
     *  [
35
     *      '[email protected]' => 'john doe',
36
     *      '[email protected]',
37
     *  ]
38
     * ```
39
     */
40
    public function bcc(): array
41
    {
42
        return [];
43
    }
44
45
    /**
46
     * Définition des adresses de copie (CC) au mail
47
     *
48
     * @return array<string, string>|string[]
49
     *
50
     * @example
51
     * ```php
52
     *  [
53
     *      '[email protected]' => 'john doe',
54
     *      '[email protected]',
55
     *  ]
56
     * ```
57
     */
58
    public function cc(): array
59
    {
60
        return [];
61
    }
62
63
    /**
64
     * Définition des éléments du contenu du mail
65
     */
66
    public function content(): array
67
    {
68
        return [
69
            'view' => '',
70
            'html' => '',
71
            'text' => '',
72
        ];
73
    }
74
75
    /**
76
     * Définition de l'adresse de l'expediteur du mail
77
     *
78
     * @return string[]
79
     *
80
     * @example
81
     * ```php
82
     *  ['[email protected]', 'John Doe']
83
     *  ['[email protected]']
84
     * ```
85
     */
86
    public function from(): array
87
    {
88
        return [];
89
    }
90
91
    /**
92
     * Définition des entetes supplementaires du mail
93
     *
94
     * @return array<string, string>
95
     *
96
     * @example
97
     * ```php
98
     *  [
99
     *      'X-Custom-Header' => 'Custom Value',
100
     *  ]
101
     * ```
102
     */
103
    public function headers(): array
104
    {
105
        return [];
106
    }
107
108
    /**
109
     * Définition du niveau de priorité du mail
110
     */
111
    public function priority(): int
112
    {
113
        return Mail::PRIORITY_NORMAL;
114
    }
115
116
    /**
117
     * Définition des adresses de reponse (ReplyTo) du mail
118
     *
119
     * @return array<string, string>|string[]
120
     *
121
     * @example
122
     * ```php
123
     *  [
124
     *      '[email protected]' => 'john doe',
125
     *      '[email protected]',
126
     *  ]
127
     * ```
128
     */
129
    public function replyTo(): array
130
    {
131
        return [];
132
    }
133
134
    /**
135
     * Définition du sujet du mail
136
     */
137
    public function subject(): string
138
    {
139
        return '';
140
    }
141
142
    /**
143
     * Définition des données à transférer à la vue qui générera le mail
144
     */
145
    public function with(): array
146
    {
147
        return [];
148
    }
149
150
    /**
151
     * Données à transférer à la vue qui générera le mail
152
     *
153
     * @internal
154
     */
155
    public function data(): array
156
    {
157
        $reflection = new ReflectionClass(static::class);
158
159
        $data = [];
160
161
        foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
162
            $data[$prop->getName()] = $prop->getValue();
163
        }
164
165
        return array_merge($data, $this->with());
166
    }
167
168
    /**
169
     * Envoi du mail
170
     *
171
     * @internal
172
     */
173
    public function send(Mail $mail): bool
174
    {
175
        foreach ($this->bcc() as $key => $value) {
176
            if (empty($value) || ! is_string($value)) {
177
                continue;
178
            }
179
180
            if (is_string($key)) {
181
                $mail->bcc($key, $value);
182
            } else {
183
                $mail->bcc($value);
184
            }
185
        }
186
187
        foreach ($this->cc() as $key => $value) {
188
            if (empty($value) || ! is_string($value)) {
189
                continue;
190
            }
191
192
            if (is_string($key)) {
193
                $mail->cc($key, $value);
194
            } else {
195
                $mail->cc($value);
196
            }
197
        }
198
199
        $content = $this->content();
200
201
        if (! empty($content['view'])) {
202
            $mail->view($content['view'], $this->data());
203
        } elseif (! empty($content['html'])) {
204
            $mail->html($content['html']);
205
        }
206
        if (! empty($content['text'])) {
207
            $mail->text($content['text']);
208
        }
209
210
        $mail->from(...$this->from());
0 ignored issues
show
Bug introduced by
$this->from() is expanded, but the parameter $address of BlitzPHP\Mail\Mail::from() does not expect variable arguments. ( Ignorable by Annotation )

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

210
        $mail->from(/** @scrutinizer ignore-type */ ...$this->from());
Loading history...
211
        $mail->header($this->headers());
212
        $mail->priority($this->priority());
213
214
        foreach ($this->replyTo() as $key => $value) {
215
            if (empty($value) || ! is_string($value)) {
216
                continue;
217
            }
218
219
            if (is_string($key)) {
220
                $mail->replyTo($key, $value);
221
            } else {
222
                $mail->replyTo($value);
223
            }
224
        }
225
226
        $mail->subject($this->subject());
227
228
        return $mail->send();
229
    }
230
}
231