Passed
Push — main ( 9116b3...d79e5f )
by Dimitri
09:50 queued 04:37
created

Mailable::cc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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>|list<string>
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Mail\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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>|list<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 list<string>
79
     *
80
     * @example
81
     * ```php
82
     *  ['[email protected]', 'John Doe']
83
     *  ['[email protected]']
84
     * ```
85
     */
86
    public function from(): array
87
    {
88
        $from = config('mail.from');
89
90
        return [$from['address'] ?? '', $from['name'] ?? ''];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($from['addr...', $from['name'] ?? '') returns the type array<integer,mixed|string> which is incompatible with the documented return type BlitzPHP\Mail\list.
Loading history...
91
    }
92
93
    /**
94
     * Définition des entetes supplementaires du mail
95
     *
96
     * @return array<string, string>
97
     *
98
     * @example
99
     * ```php
100
     *  [
101
     *      'X-Custom-Header' => 'Custom Value',
102
     *  ]
103
     * ```
104
     */
105
    public function headers(): array
106
    {
107
        return [];
108
    }
109
110
    /**
111
     * Définition du niveau de priorité du mail
112
     */
113
    public function priority(): int
114
    {
115
        return Mail::PRIORITY_NORMAL;
116
    }
117
118
    /**
119
     * Définition des adresses de reponse (ReplyTo) du mail
120
     *
121
     * @return array<string, string>|list<string>
122
     *
123
     * @example
124
     * ```php
125
     *  [
126
     *      '[email protected]' => 'john doe',
127
     *      '[email protected]',
128
     *  ]
129
     * ```
130
     */
131
    public function replyTo(): array
132
    {
133
        return [];
134
    }
135
136
    /**
137
     * Définition du sujet du mail
138
     */
139
    public function subject(): string
140
    {
141
        return '';
142
    }
143
144
    /**
145
     * Définition des adresses de destination (to) au mail
146
     *
147
     * @return array<string, string>|list<string>
148
     *
149
     * @example
150
     * ```php
151
     *  [
152
     *      '[email protected]' => 'john doe',
153
     *      '[email protected]',
154
     *  ]
155
     * ```
156
     */
157
    public function to(): array
158
    {
159
        return [];
160
    }
161
162
    /**
163
     * Définition des données à transférer à la vue qui générera le mail
164
     */
165
    public function with(): array
166
    {
167
        return [];
168
    }
169
170
    /**
171
     * Données à transférer à la vue qui générera le mail
172
     *
173
     * @internal
174
     */
175
    public function data(): array
176
    {
177
        $reflection = new ReflectionClass(static::class);
178
179
        $data = [];
180
181
        foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
182
            $data[$prop->getName()] = $prop->getValue($this);
183
        }
184
185
        return array_merge($data, $this->with());
186
    }
187
188
    /**
189
     * Envoi du mail
190
     *
191
     * @internal
192
     */
193
    public function send(Mail $mail): bool
194
    {
195
        foreach ($this->bcc() as $key => $value) {
196
            if (empty($value) || ! is_string($value)) {
197
                continue;
198
            }
199
200
            if (is_string($key)) {
201
                $mail->bcc($key, $value);
202
            } else {
203
                $mail->bcc($value);
204
            }
205
        }
206
207
        foreach ($this->cc() as $key => $value) {
208
            if (empty($value) || ! is_string($value)) {
209
                continue;
210
            }
211
212
            if (is_string($key)) {
213
                $mail->cc($key, $value);
214
            } else {
215
                $mail->cc($value);
216
            }
217
        }
218
219
        $content = $this->content();
220
221
        if (! empty($content['view'])) {
222
            $mail->view($content['view'], $this->data());
223
        } elseif (! empty($content['html'])) {
224
            $mail->html($content['html']);
225
        }
226
        if (! empty($content['text'])) {
227
            $mail->text($content['text']);
228
        }
229
230
        $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

230
        $mail->from(/** @scrutinizer ignore-type */ ...$this->from());
Loading history...
231
        $mail->header($this->headers());
232
        $mail->priority($this->priority());
233
234
        foreach ($this->replyTo() as $key => $value) {
235
            if (empty($value) || ! is_string($value)) {
236
                continue;
237
            }
238
239
            if (is_string($key)) {
240
                $mail->replyTo($key, $value);
241
            } else {
242
                $mail->replyTo($value);
243
            }
244
        }
245
246
        $mail->subject($this->subject());
247
248
        foreach ($this->to() as $key => $value) {
249
            if (empty($value) || ! is_string($value)) {
250
                continue;
251
            }
252
253
            if (is_string($key)) {
254
                $mail->to($key, $value);
255
            } else {
256
                $mail->to($value);
257
            }
258
        }
259
260
        return $mail->send();
261
    }
262
}
263