Passed
Push — master ( 46ed75...ca2387 )
by Zaahid
03:33
created

AddressGroupPart   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 9
eloc 14
c 0
b 0
f 0
dl 0
loc 65
ccs 16
cts 18
cp 0.8889
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddresses() 0 3 1
A getName() 0 3 1
A __construct() 0 4 1
A getAddress() 0 6 2
A validate() 0 7 3
A getErrorBagChildren() 0 3 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
8
namespace ZBateson\MailMimeParser\Header\Part;
9
10
use ZBateson\MbWrapper\MbWrapper;
11
use Psr\Log\LogLevel;
12
13
/**
14
 * Holds a group of addresses, and an optional group name.
15
 *
16
 * Because AddressGroupConsumer is only called once a colon (":") character is
17
 * found, an AddressGroupPart is initially constructed without a $name.  Once it
18
 * is returned to AddressConsumer, a new AddressGroupPart is created out of
19
 * AddressGroupConsumer's AddressGroupPart.
20
 *
21
 * @author Zaahid Bateson
22
 */
23
class AddressGroupPart extends MimeLiteralPart
24
{
25
    /**
26
     * @var AddressPart[] an array of AddressParts
27
     */
28
    protected array $addresses;
29
30
    /**
31
     * Creates an AddressGroupPart out of the passed array of AddressParts and an
32
     * optional name (which may be mime-encoded).
33
     *
34
     * @param AddressPart[] $addresses
35
     */
36 2
    public function __construct(MbWrapper $charsetConverter, array $addresses, string $name = '')
37
    {
38 2
        parent::__construct($charsetConverter, \trim($name));
39 2
        $this->addresses = $addresses;
40
    }
41
42
    /**
43
     * Return the AddressGroupPart's array of addresses.
44
     *
45
     * @return AddressPart[] An array of address parts.
46
     */
47 1
    public function getAddresses() : array
48
    {
49 1
        return $this->addresses;
50
    }
51
52
    /**
53
     * Returns the AddressPart at the passed index or null.
54
     *
55
     * @param int $index The 0-based index.
56
     * @return ?AddressPart The address.
57
     */
58 1
    public function getAddress(int $index) : ?AddressPart
59
    {
60 1
        if (!isset($this->addresses[$index])) {
61 1
            return null;
62
        }
63 1
        return $this->addresses[$index];
64
    }
65
66
    /**
67
     * Returns the name of the group
68
     *
69
     * @return string The name
70
     */
71 1
    public function getName() : string
72
    {
73 1
        return $this->value;
74
    }
75
76
    protected function getErrorBagChildren() : array
77
    {
78
        return $this->addresses;
79
    }
80
81 1
    protected function validate() : void
82
    {
83 1
        if (mb_strlen($this->value) === 0) {
0 ignored issues
show
Bug introduced by
It seems like $this->value can also be of type null; however, parameter $string of mb_strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
        if (mb_strlen(/** @scrutinizer ignore-type */ $this->value) === 0) {
Loading history...
84 1
            $this->addError('Address group doesn\'t have a name', LogLevel::ERROR);
85
        }
86 1
        if (empty($this->addresses)) {
87 1
            $this->addError('Address group doesn\'t have any email addresses defined in it', LogLevel::NOTICE);
88
        }
89
    }
90
}
91