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

AddressHeader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
eloc 22
c 3
b 0
f 0
dl 0
loc 101
ccs 26
cts 26
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A hasAddress() 0 8 3
A getGroups() 0 3 1
A getEmail() 0 4 2
A getAddresses() 0 3 1
A getPersonName() 0 6 2
A filterAndAssignToParts() 0 9 4
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;
9
10
use ZBateson\MailMimeParser\Header\Consumer\AddressBaseConsumerService;
11
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart;
12
use ZBateson\MailMimeParser\Header\Part\AddressPart;
13
14
/**
15
 * A header containing one or more email addresses and/or groups of addresses.
16
 *
17
 * An address is separated by a comma, and each group separated by a semi-colon.
18
 * The AddressHeader provides a complete list of all addresses referenced in a
19
 * header including any addresses in groups, in addition to being able to access
20
 * the groups separately if needed.
21
 *
22
 * For full specifications, see {@link https://www.ietf.org/rfc/rfc2822.txt}
23
 *
24
 * @author Zaahid Bateson
25
 */
26
class AddressHeader extends AbstractHeader
27
{
28
    /**
29
     * @var AddressPart[] array of addresses, included all addresses contained
30
     *      in groups.
31
     */
32
    protected array $addresses = [];
33
34
    /**
35
     * @var AddressGroupPart[] array of address groups (lists).
36
     */
37
    protected array $groups = [];
38
39 107
    public function __construct(
40
        AddressBaseConsumerService $consumerService,
41
        string $name,
42
        string $value
43
    ) {
44 107
        parent::__construct($consumerService, $name, $value);
45
    }
46
47
    /**
48
     * Filters $this->allParts into the parts required by $this->parts
49
     * and assignes it.
50
     *
51
     * The AbstractHeader::filterAndAssignToParts method filters out CommentParts.
52
     */
53 107
    protected function filterAndAssignToParts() : void
54
    {
55 107
        parent::filterAndAssignToParts();
56 107
        foreach ($this->parts as $part) {
57 106
            if ($part instanceof AddressPart) {
58 105
                $this->addresses[] = $part;
59 4
            } elseif ($part instanceof AddressGroupPart) {
60 4
                $this->addresses = \array_merge($this->addresses, $part->getAddresses());
61 4
                $this->groups[] = $part;
62
            }
63
        }
64
    }
65
66
    /**
67
     * Returns all address parts in the header including any addresses that are
68
     * in groups (lists).
69
     *
70
     * @return AddressPart[] The addresses.
71
     */
72 1
    public function getAddresses() : array
73
    {
74 1
        return $this->addresses;
75
    }
76
77
    /**
78
     * Returns all group parts (lists) in the header.
79
     *
80
     * @return AddressGroupPart[]
81
     */
82 1
    public function getGroups() : array
83
    {
84 1
        return $this->groups;
85
    }
86
87
    /**
88
     * Returns true if an address exists with the passed email address.
89
     *
90
     * Comparison is done case insensitively.
91
     *
92
     */
93 1
    public function hasAddress(string $email) : bool
94
    {
95 1
        foreach ($this->addresses as $addr) {
96 1
            if (\strcasecmp($addr->getEmail(), $email) === 0) {
97 1
                return true;
98
            }
99
        }
100 1
        return false;
101
    }
102
103
    /**
104
     * Returns the first email address in the header.
105
     *
106
     * @return ?string The email address
107
     */
108 1
    public function getEmail() : ?string
109
    {
110 1
        if (!empty($this->addresses)) {
111 1
            return $this->addresses[0]->getEmail();
112
        }
113
    }
114
115
    /**
116
     * Returns the name associated with the first email address to complement
117
     * getEmail() if one is set, or null if not.
118
     *
119
     * @return string|null The person name.
120
     */
121 98
    public function getPersonName() : ?string
122
    {
123 98
        if (!empty($this->addresses)) {
124 97
            return $this->addresses[0]->getName();
125
        }
126 1
        return null;
127
    }
128
}
129