AddressGroupPart   A
last analyzed

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