Passed
Push — main ( 7ca792...577f5e )
by Dimitri
03:34
created

SymfonyMailer   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 355
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 101
dl 0
loc 355
rs 8.96
c 1
b 0
f 0
wmc 43

29 Methods

Rating   Name   Duplication   Size   Complexity  
A attachment() 0 11 3
A buildDsn() 0 3 1
A alt() 0 3 1
A bcc() 0 11 2
A replyTo() 0 11 2
A setCharset() 0 5 1
A lastId() 0 3 1
A html() 0 5 1
A cc() 0 11 2
A message() 0 3 1
A __construct() 0 5 1
A setPassword() 0 5 1
A sign() 0 3 1
A subject() 0 5 1
A setTimeout() 0 5 1
A to() 0 11 2
A from() 0 5 1
A header() 0 11 3
A setDebug() 0 5 1
A makeAddress() 0 9 2
A setPort() 0 5 1
A setHost() 0 5 1
A setPriority() 0 7 2
A setProtocol() 0 5 1
A setUsername() 0 5 1
A send() 0 12 3
A transporter() 0 8 2
A text() 0 5 1
A setEncryption() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like SymfonyMailer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SymfonyMailer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace BlitzPHP\Mail\Adapters;
4
5
use Symfony\Component\Mailer\Mailer;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mailer\Mailer 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...
6
use Symfony\Component\Mailer\Transport;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mailer\Transport 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...
7
use Symfony\Component\Mime\Address;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mime\Address 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...
8
use Symfony\Component\Mime\Email;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mime\Email 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...
9
use Throwable;
10
11
class SymfonyMailer extends AbstractAdapter
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected array $dependancies = [
17
        ['class' => Mailer::class, 'package' => 'symfony/mailer']
18
    ];
19
20
    /**
21
	 * @var Email
22
	 */
23
    protected $mailer;
24
25
    private ?Mailer $transporter = null;
26
27
    private string $charset    = 'utf-8';
28
    private string $protocol   = self::PROTOCOL_SMTP;
29
    private int $timeout       = 0;
30
    private int $port          = 587;
31
    private string $host       = '';
32
    private string $username   = '';
33
    private string $password   = '';
34
    private string $encryption = self::ENCRYPTION_TLS;
35
    private int $debug         = 0;
36
37
    public function __construct(bool $debug = false)
38
    {
39
        parent::__construct($debug);
40
41
        $this->mailer = new Email();
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function setPort(int $port): self 
48
    {
49
        $this->port = $port;
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function setHost(string $host): self 
58
    {
59
        $this->host = $host;
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function setUsername(string $username): self 
68
    {
69
        $this->username = $username;
70
71
        return $this;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function setPassword(string $password): self 
78
    {
79
        $this->password = $password;
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function setDebug(int $debug = 1): self 
88
    {
89
        $this->debug = $debug;
90
91
        return $this;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function setProtocol(string $protocol): self 
98
    {
99
        $this->protocol = $protocol;
100
101
        return $this;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function setTimeout(int $timeout): self 
108
    {
109
        $this->timeout = $timeout;
110
111
        return $this;
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function setCharset(string $charset): self 
118
    {
119
        $this->charset = $charset;
120
121
        return $this;
122
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127
    public function setPriority(int $priority): self 
128
    {
129
        if (in_array($priority, static::PRIORITY_MAP, true)) {
130
            $this->mailer->priority($priority);
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139
    public function setEncryption(?string $encryption): self 
140
    {
141
        if (in_array($encryption, [null, static::ENCRYPTION_SSL, static::ENCRYPTION_TLS], true)) {
142
            $this->encryption = $encryption;
143
        }
144
145
        return $this;
146
    }
147
148
149
    /**
150
	 * {@inheritDoc}
151
     */
152
    public function alt(string $content) : self
153
    {
154
        return $this;
155
    }
156
157
    /**
158
	 * {@inheritDoc}
159
     */
160
    public function attachment(array|string $path, string $name = '', string $encoding = '', string $disposition = 'attachment'): self
161
    {
162
        if (is_string($path)) {
0 ignored issues
show
introduced by
The condition is_string($path) is always false.
Loading history...
163
            $path = [$path => $name];
164
        }
165
166
        foreach ($path As $key => $value) {
167
            $this->mailer->attachFromPath($key, $value, $encoding);
168
        }
169
170
        return $this;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    public function bcc(array|string $address, bool|string $name = '', bool $set = false): self
177
    {
178
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
179
180
        if ($set) {
181
            $this->mailer->bcc(...$addresses);
182
        } else {
183
            $this->mailer->addBcc(...$addresses);
184
        }
185
186
        return $this;
187
    }
188
189
    /**
190
	 * {@inheritDoc}
191
     */
192
    public function cc(array|string $address, bool|string $name = '', bool $set = false): self
193
    {
194
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
195
196
        if ($set) {
197
            $this->mailer->cc(...$addresses);
198
        } else {
199
            $this->mailer->addCC(...$addresses);
200
        }
201
202
        return $this;
203
    }
204
205
    /**
206
	 * {@inheritDoc}
207
     */
208
    public function from(string $address, string $name = ''): self
209
    {
210
        $this->mailer->from($this->makeAddress($address, $name));
211
      
212
        return $this;
213
    }
214
215
    /**
216
	 * {@inheritDoc}
217
     */
218
    public function header(array|string $name, ?string $value = null): self
219
    {
220
        if (is_string($name)) {
0 ignored issues
show
introduced by
The condition is_string($name) is always false.
Loading history...
221
            $name = [$name => $value];
222
        }
223
224
        foreach ($name As $key => $value) {
225
            $this->mailer->getHeaders()->addTextHeader($key, $value);
226
        }
227
        
228
        return $this;
229
    }
230
231
    /**
232
     * {@inheritDoc}
233
     */
234
    public function html(string $content): self
235
    {
236
        $this->mailer->html($content, $this->charset);
237
        
238
        return $this;
239
    }
240
    
241
    /**
242
	 * {@inheritDoc}
243
     */
244
    public function message(string $message): self
245
    {
246
        return $this;
247
    }
248
249
    /**
250
	 * {@inheritDoc}
251
     */
252
    public function replyTo(array|string $address, bool|string $name = '', bool $set = false): self
253
    {
254
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
255
256
        if ($set) {
257
            $this->mailer->replyTo(...$addresses);
258
        } else {
259
            $this->mailer->addReplyTo(...$addresses);
260
        }
261
262
        return $this;
263
    }
264
265
    /**
266
     * {@inheritDoc}
267
     */
268
    public function send() : bool
269
    {
270
        try {
271
            $this->transporter()->send($this->mailer);
272
        
273
            return true;
274
        } catch (Throwable $e) {
275
            if ($this->debug > 0) {
276
                throw $e;
277
            }
278
279
            return false;
280
        }
281
    }
282
283
    /**
284
     * {@inheritDoc}
285
     */
286
    public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): self
287
    {
288
        return $this;
289
    }
290
291
    /**
292
	 * {@inheritDoc}
293
     */
294
    public function subject(string $subject) : self
295
    {
296
        $this->mailer->subject($subject);
297
298
        return $this;
299
    }
300
301
    /**
302
     * {@inheritDoc}
303
     */
304
    public function text(string $content): self
305
    {
306
        $this->mailer->text($content, $this->charset);
307
        
308
        return $this;
309
    }
310
311
    /**
312
	 * {@inheritDoc}
313
     */
314
    public function to(array|string $address, bool|string $name = '', bool $set = false): self
315
    {
316
        [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set);
317
318
        if ($set) {
319
            $this->mailer->to(...$addresses);
320
        } else {
321
            $this->mailer->addTo(...$addresses);
322
        }
323
324
        return $this;
325
    }
326
327
    /**
328
	 * {@inheritDoc}
329
     */
330
    public function lastId(): string
331
    {
332
        return $this->mailer->generateMessageId();
333
    }
334
    
335
336
    /**
337
     * {@inheritDoc}
338
     *
339
     * @return Address
340
     */
341
    protected function makeAddress(string $email, string $name) 
342
    {
343
        if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
344
            $tmp = $email;
345
            $email = $name;
346
            $name = $tmp;
347
        }
348
349
        return new Address($email, $name);
350
    }
351
352
    private function transporter(): Mailer
353
    {
354
        if (null !== $this->transporter) {
355
            return $this->transporter;
356
        }
357
358
        return $this->transporter = new Mailer(
359
            Transport::fromDsn($this->buildDsn())
360
        );
361
    }
362
363
    private function buildDsn(): string
364
    {
365
        return '';  
366
    }
367
}
368