Passed
Push — master ( a626ba...b864c0 )
by Zaahid
15:44 queued 12:03
created

AddressGroupPart   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 19
c 0
b 0
f 0
dl 0
loc 65
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 4
A getAddresses() 0 3 1
A getAddress() 0 6 2
A __construct() 0 18 2
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 Psr\Log\LoggerInterface;
11
use Psr\Log\LogLevel;
12
use ZBateson\MbWrapper\MbWrapper;
13
14
/**
15
 * Holds a group of addresses, and an optional group name.
16
 *
17
 * Because AddressGroupConsumer is only called once a colon (":") character is
18
 * found, an AddressGroupPart is initially constructed without a $name.  Once it
19
 * is returned to AddressConsumer, a new AddressGroupPart is created out of
20
 * AddressGroupConsumer's AddressGroupPart.
21
 *
22
 * @author Zaahid Bateson
23
 */
24
class AddressGroupPart extends NameValuePart
25
{
26
    /**
27
     * @var AddressPart[] an array of AddressParts
28
     */
29
    protected array $addresses;
30
31
    /**
32
     * Creates an AddressGroupPart out of the passed array of AddressParts/
33
     * AddressGroupParts and name.
34
     *
35
     * @param HeaderPart[] $nameParts
36
     * @param AddressPart[]|AddressGroupPart[] $addressesAndGroupParts
37
     */
38 2
    public function __construct(
39
        LoggerInterface $logger,
40
        MbWrapper $charsetConverter,
41
        array $nameParts,
42
        array $addressesAndGroupParts
43
    ) {
44 2
        parent::__construct(
45 2
            $logger,
46 2
            $charsetConverter,
47 2
            $nameParts,
48 2
            $addressesAndGroupParts
49 2
        );
50 2
        $this->addresses = \array_merge(...\array_map(
51 2
            fn ($p) => ($p instanceof AddressGroupPart) ? $p->getAddresses() : [$p],
52 2
            $addressesAndGroupParts
53 2
        ));
54
        // for backwards compatibility
55 2
        $this->value = $this->name;
56
    }
57
58
    /**
59
     * Return the AddressGroupPart's array of addresses.
60
     *
61
     * @return AddressPart[] An array of address parts.
62
     */
63 1
    public function getAddresses() : array
64
    {
65 1
        return $this->addresses;
66
    }
67
68
    /**
69
     * Returns the AddressPart at the passed index or null.
70
     *
71
     * @param int $index The 0-based index.
72
     * @return ?AddressPart The address.
73
     */
74 1
    public function getAddress(int $index) : ?AddressPart
75
    {
76 1
        if (!isset($this->addresses[$index])) {
77 1
            return null;
78
        }
79 1
        return $this->addresses[$index];
80
    }
81
82 1
    protected function validate() : void
83
    {
84 1
        if ($this->name === null || \mb_strlen($this->name) === 0) {
85 1
            $this->addError('Address group doesn\'t have a name', LogLevel::ERROR);
86
        }
87 1
        if (empty($this->addresses)) {
88 1
            $this->addError('Address group doesn\'t have any email addresses defined in it', LogLevel::NOTICE);
89
        }
90
    }
91
}
92