Envelope::addMessage()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 42
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Derafu: Biblioteca PHP (Núcleo).
7
 * Copyright (C) Derafu <https://www.derafu.org>
8
 *
9
 * Este programa es software libre: usted puede redistribuirlo y/o modificarlo
10
 * bajo los términos de la Licencia Pública General Affero de GNU publicada por
11
 * la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o
12
 * (a su elección) cualquier versión posterior de la misma.
13
 *
14
 * Este programa se distribuye con la esperanza de que sea útil, pero SIN
15
 * GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD
16
 * PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública
17
 * General Affero de GNU para obtener una información más detallada.
18
 *
19
 * Debería haber recibido una copia de la Licencia Pública General Affero de GNU
20
 * junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace Derafu\Lib\Core\Package\Prime\Component\Mail\Support;
26
27
use Derafu\Lib\Core\Package\Prime\Component\Mail\Contract\EnvelopeInterface;
28
use Derafu\Lib\Core\Package\Prime\Component\Mail\Contract\MessageInterface;
29
use Symfony\Component\Mailer\Envelope as SymfonyEnvelope;
30
use Symfony\Component\Mime\Email as SymfonyEmail;
31
32
/**
33
 * Clase que representa un sobre con mensajes de correo electrónico.
34
 */
35
class Envelope extends SymfonyEnvelope implements EnvelopeInterface
36
{
37
    /**
38
     * Mensajes que el sobre contiene para ser enviados por correo.
39
     *
40
     * @var MessageInterface[]
41
     */
42
    private array $messages;
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function addMessage(MessageInterface $message): static
48
    {
49
        assert($message instanceof SymfonyEmail);
50
51
        $from = $message->getFrom();
52
        $sender = $message->getSender();
53
54
        if (!$from && !$sender) {
0 ignored issues
show
introduced by
$from is an empty array, thus ! $from is always true.
Loading history...
Bug Best Practice introduced by
The expression $from of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55
            $message->from($this->getSender());
56
        }
57
58
        $to = $message->getTo();
59
        $cc = $message->getCc();
60
        $bcc = $message->getBcc();
61
62
        if (!$to && !$cc && !$bcc) {
0 ignored issues
show
introduced by
$cc is an empty array, thus ! $cc is always true.
Loading history...
Bug Best Practice introduced by
The expression $to of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $bcc of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
$to is an empty array, thus ! $to is always true.
Loading history...
introduced by
$bcc is an empty array, thus ! $bcc is always true.
Loading history...
Bug Best Practice introduced by
The expression $cc of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
            $message->to(...$this->getRecipients());
64
        }
65
66
        $this->messages[] = $message;
67
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function getMessages(): array
75
    {
76
        return $this->messages;
77
    }
78
79
    /**
80
     * Al clonar el objeto se eliminan los mensajes que el sobre contenía.
81
     *
82
     * Con esto queda un sobre "limpio" de Symfony sin los mensajes.
83
     *
84
     * @return void
85
     */
86
    public function __clone(): void
87
    {
88
        $this->messages = [];
89
    }
90
}
91