Total Complexity | 50 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
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 |
||
15 | class SymfonyMailer extends AbstractAdapter |
||
16 | { |
||
17 | /** |
||
18 | * {@inheritDoc} |
||
19 | */ |
||
20 | protected array $dependancies = [ |
||
21 | ['class' => Mailer::class, 'package' => 'symfony/mailer'] |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * @var Email |
||
26 | */ |
||
27 | protected $mailer; |
||
28 | |||
29 | private ?Mailer $transporter = null; |
||
30 | |||
31 | private string $charset = self::CHARSET_UTF8; |
||
32 | private string $dsn = ''; |
||
33 | private string $protocol = self::PROTOCOL_SMTP; |
||
34 | private int $timeout = 0; |
||
35 | private int $port = 587; |
||
36 | private string $host = ''; |
||
37 | private string $username = ''; |
||
38 | private string $password = ''; |
||
39 | private string $encryption = self::ENCRYPTION_TLS; |
||
40 | private int $debug = 0; |
||
41 | |||
42 | public function __construct(bool $debug = false) |
||
43 | { |
||
44 | $this->mailer = new Email(); |
||
45 | |||
46 | parent::__construct($debug); |
||
47 | } |
||
48 | |||
49 | |||
50 | public function setDsn(string $dsn): self |
||
51 | { |
||
52 | $this->dsn = $dsn; |
||
53 | |||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritDoc} |
||
59 | */ |
||
60 | public function setPort(int $port): self |
||
61 | { |
||
62 | $this->port = $port; |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritDoc} |
||
69 | */ |
||
70 | public function setHost(string $host): self |
||
71 | { |
||
72 | $this->host = $host; |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | public function setUsername(string $username): self |
||
81 | { |
||
82 | $this->username = $username; |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritDoc} |
||
89 | */ |
||
90 | public function setPassword(string $password): self |
||
91 | { |
||
92 | $this->password = $password; |
||
93 | |||
94 | return $this; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | public function setDebug(int $debug = 1): self |
||
101 | { |
||
102 | $this->debug = $debug; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritDoc} |
||
109 | */ |
||
110 | public function setProtocol(string $protocol): self |
||
111 | { |
||
112 | $this->protocol = $protocol; |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | */ |
||
120 | public function setTimeout(int $timeout): self |
||
121 | { |
||
122 | $this->timeout = $timeout; |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | public function setCharset(string $charset): self |
||
131 | { |
||
132 | $this->charset = $charset; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritDoc} |
||
139 | */ |
||
140 | public function setPriority(int $priority): self |
||
141 | { |
||
142 | if (in_array($priority, static::PRIORITY_MAP, true)) { |
||
143 | $this->mailer->priority($priority); |
||
144 | } |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritDoc} |
||
151 | */ |
||
152 | public function setEncryption(?string $encryption): self |
||
153 | { |
||
154 | if (in_array($encryption, [null, static::ENCRYPTION_SSL, static::ENCRYPTION_TLS], true)) { |
||
155 | $this->encryption = $encryption; |
||
156 | } |
||
157 | |||
158 | return $this; |
||
159 | } |
||
160 | |||
161 | |||
162 | /** |
||
163 | * {@inheritDoc} |
||
164 | */ |
||
165 | public function alt(string $content) : self |
||
166 | { |
||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * {@inheritDoc} |
||
172 | */ |
||
173 | public function attach(array|string $path, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'attachment'): self |
||
174 | { |
||
175 | if (is_string($path)) { |
||
176 | $path = [$path => $name]; |
||
177 | } |
||
178 | |||
179 | foreach ($path As $key => $value) { |
||
180 | $this->mailer->addPart(new DataPart(new File($key), $value, $type)); |
||
181 | } |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * {@inheritDoc} |
||
188 | */ |
||
189 | public function attachBinary($binary, string $name, string $encoding = self::ENCODING_BASE64, string $type = '', string $disposition = 'attachment'): self |
||
190 | { |
||
191 | $this->mailer->addPart(new DataPart(@fopen($binary, 'r'), $name, $type)); |
||
192 | |||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * {@inheritDoc} |
||
198 | */ |
||
199 | public function bcc(array|string $address, bool|string $name = '', bool $set = false): self |
||
200 | { |
||
201 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
202 | |||
203 | if ($set) { |
||
204 | $this->mailer->bcc(...$addresses); |
||
205 | } else { |
||
206 | $this->mailer->addBcc(...$addresses); |
||
207 | } |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * {@inheritDoc} |
||
214 | */ |
||
215 | public function cc(array|string $address, bool|string $name = '', bool $set = false): self |
||
216 | { |
||
217 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
218 | |||
219 | if ($set) { |
||
220 | $this->mailer->cc(...$addresses); |
||
221 | } else { |
||
222 | $this->mailer->addCC(...$addresses); |
||
223 | } |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * {@inheritDoc} |
||
230 | */ |
||
231 | public function dkim(string $pk, string $passphrase = '', string $selector = '', string $domain = ''): self |
||
232 | { |
||
233 | |||
234 | $signer = new DkimSigner($pk, $domain ?: site_url(), $selector ?: 'blitz', [], $passphrase); |
||
235 | |||
236 | $this->mailer = $signer->sign($this->mailer); |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritDoc} |
||
243 | */ |
||
244 | public function embedded(string $path, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): self |
||
245 | { |
||
246 | $this->mailer->addPart((new DataPart(new File($path), $cid, $type))->asInline()); |
||
247 | |||
248 | return $this; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * {@inheritDoc} |
||
253 | */ |
||
254 | public function embeddedBinary($binary, string $cid, string $name = '', string $type = '', string $encoding = self::ENCODING_BASE64, string $disposition = 'inline'): self |
||
255 | { |
||
256 | $this->mailer->addPart((new DataPart(@fopen($binary, 'r'), $name, $type))->asInline()); |
||
257 | |||
258 | return $this; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * {@inheritDoc} |
||
263 | */ |
||
264 | public function from(string $address, string $name = ''): self |
||
265 | { |
||
266 | $this->mailer->from($this->makeAddress($address, $name)); |
||
267 | |||
268 | return $this; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * {@inheritDoc} |
||
273 | */ |
||
274 | public function header(array|string $name, ?string $value = null): self |
||
275 | { |
||
276 | if (is_string($name)) { |
||
277 | $name = [$name => $value]; |
||
278 | } |
||
279 | |||
280 | foreach ($name As $key => $value) { |
||
281 | $this->mailer->getHeaders()->addTextHeader($key, $value); |
||
282 | } |
||
283 | |||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * {@inheritDoc} |
||
289 | */ |
||
290 | public function html(string $content): self |
||
291 | { |
||
292 | $this->mailer->html($content, $this->charset); |
||
293 | |||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * {@inheritDoc} |
||
299 | */ |
||
300 | public function message(string $message): self |
||
301 | { |
||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * {@inheritDoc} |
||
307 | */ |
||
308 | public function replyTo(array|string $address, bool|string $name = '', bool $set = false): self |
||
309 | { |
||
310 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
311 | |||
312 | if ($set) { |
||
313 | $this->mailer->replyTo(...$addresses); |
||
314 | } else { |
||
315 | $this->mailer->addReplyTo(...$addresses); |
||
316 | } |
||
317 | |||
318 | return $this; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * {@inheritDoc} |
||
323 | */ |
||
324 | public function send() : bool |
||
325 | { |
||
326 | try { |
||
327 | $this->transporter()->send($this->mailer); |
||
328 | |||
329 | return true; |
||
330 | } catch (Throwable $e) { |
||
331 | if ($this->debug > 0) { |
||
332 | throw $e; |
||
333 | } |
||
334 | |||
335 | return false; |
||
336 | } |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * {@inheritDoc} |
||
341 | */ |
||
342 | public function sign(string $cert_filename, string $key_filename, string $key_pass, string $extracerts_filename = ''): self |
||
343 | { |
||
344 | $signer = new SMimeSigner($cert_filename, $key_filename, $key_pass, $extracerts_filename); |
||
345 | |||
346 | $this->mailer = $signer->sign($this->mailer); |
||
347 | |||
348 | return $this; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * {@inheritDoc} |
||
353 | */ |
||
354 | public function subject(string $subject) : self |
||
355 | { |
||
356 | $this->mailer->subject($subject); |
||
357 | |||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * {@inheritDoc} |
||
363 | */ |
||
364 | public function text(string $content): self |
||
365 | { |
||
366 | $this->mailer->text($content, $this->charset); |
||
367 | |||
368 | return $this; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * {@inheritDoc} |
||
373 | */ |
||
374 | public function to(array|string $address, bool|string $name = '', bool $set = false): self |
||
375 | { |
||
376 | [$addresses, $set] = $this->parseMultipleAddresses($address, $name, $set); |
||
377 | |||
378 | if ($set) { |
||
379 | $this->mailer->to(...$addresses); |
||
380 | } else { |
||
381 | $this->mailer->addTo(...$addresses); |
||
382 | } |
||
383 | |||
384 | return $this; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * {@inheritDoc} |
||
389 | */ |
||
390 | public function lastId(): string |
||
391 | { |
||
392 | return $this->mailer->generateMessageId(); |
||
393 | } |
||
394 | |||
395 | |||
396 | /** |
||
397 | * {@inheritDoc} |
||
398 | * |
||
399 | * @return Address |
||
400 | */ |
||
401 | protected function makeAddress(string $email, string $name) |
||
402 | { |
||
403 | [$email, $name] = parent::makeAddress($email, $name); |
||
404 | |||
405 | return new Address($email, $name); |
||
406 | } |
||
407 | |||
408 | private function transporter(): Mailer |
||
409 | { |
||
410 | if (null !== $this->transporter) { |
||
411 | return $this->transporter; |
||
412 | } |
||
413 | |||
414 | return $this->transporter = new Mailer( |
||
415 | Transport::fromDsn($this->buildDsn()) |
||
416 | ); |
||
417 | } |
||
418 | |||
419 | private function buildDsn(): string |
||
432 | }; |
||
433 | } |
||
434 | } |
||
435 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths