Passed
Push — master ( 74d1f8...8d5e60 )
by Zaahid
03:07
created

AddressHeader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 117
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroups() 0 3 1
A getConsumer() 0 3 1
A getAddresses() 0 3 1
A hasAddress() 0 8 3
A setParseHeaderValue() 0 9 4
A getValue() 0 6 2
A getEmail() 0 3 1
A getPersonName() 0 6 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
namespace ZBateson\MailMimeParser\Header;
8
9
use ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer;
10
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
11
use ZBateson\MailMimeParser\Header\Part\AddressPart;
12
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart;
13
14
/**
15
 * Reads an address list header using the AddressBaseConsumer.
16
 * 
17
 * An address list may consist of one or more addresses and address groups.
18
 * Each address separated by a comma, and each group separated by a semi-colon.
19
 * 
20
 * For full specifications, see https://www.ietf.org/rfc/rfc2822.txt
21
 *
22
 * @author Zaahid Bateson
23
 */
24
class AddressHeader extends AbstractHeader
25
{
26
    /**
27
     * @var \ZBateson\MailMimeParser\Header\Part\AddressPart[] array of
28
     * addresses 
29
     */
30
    protected $addresses = [];
31
    
32
    /**
33
     * @var \ZBateson\MailMimeParser\Header\Part\AddressGroupPart[] array of
34
     * address groups
35
     */
36
    protected $groups = [];
37
    
38
    /**
39
     * Returns an AddressBaseConsumer.
40
     * 
41
     * @param ConsumerService $consumerService
42
     * @return \ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer
43
     */
44 11
    protected function getConsumer(ConsumerService $consumerService)
45
    {
46 11
        return $consumerService->getAddressBaseConsumer();
47
    }
48
    
49
    /**
50
     * Overridden to extract all addresses into addresses array.
51
     * 
52
     * @param AbstractConsumer $consumer
53
     */
54 11
    protected function setParseHeaderValue(AbstractConsumer $consumer)
55
    {
56 11
        parent::setParseHeaderValue($consumer);
57 11
        foreach ($this->parts as $part) {
58 10
            if ($part instanceof AddressPart) {
59 9
                $this->addresses[] = $part;
60 4
            } elseif ($part instanceof AddressGroupPart) {
61 4
                $this->addresses = array_merge($this->addresses, $part->getAddresses());
62 10
                $this->groups[] = $part;
63
            }
64
        }
65 11
    }
66
    
67
    /**
68
     * Returns all address parts in the header including all addresses that are
69
     * in groups.
70
     * 
71
     * @return \ZBateson\MailMimeParser\Header\Part\AddressPart[]
72
     */
73 1
    public function getAddresses()
74
    {
75 1
        return $this->addresses;
76
    }
77
    
78
    /**
79
     * Returns all group parts in the header.
80
     * 
81
     * @return \ZBateson\MailMimeParser\Header\Part\AddressGroupPart[]
82
     */
83 1
    public function getGroups()
84
    {
85 1
        return $this->groups;
86
    }
87
    
88
    /**
89
     * Returns true if an address exists with the passed email address.
90
     * 
91
     * Comparison is done case insensitively.
92
     * 
93
     * @param string $email
94
     * @return boolean
95
     */
96 1
    public function hasAddress($email)
97
    {
98 1
        foreach ($this->addresses as $addr) {
99 1
            if (strcasecmp($addr->getEmail(), $email) === 0) {
100 1
                return true;
101
            }
102
        }
103 1
        return false;
104
    }
105
106
    /**
107
     * Overridden to return the first email address in the header.
108
     *
109
     * @return string
110
     */
111 3
    public function getValue()
112
    {
113 3
        if (!empty($this->addresses)) {
114 2
            return $this->addresses[0]->getEmail();
115
        }
116 1
        return null;
117
    }
118
119
    /**
120
     * Same as getValue, but for clarity to match AddressPart.
121
     *
122
     * @return string
123
     */
124
    public function getEmail()
125
    {
126
        return $this->getValue();
127
    }
128
129
    /**
130
     * Returns the name associated with the first email address to complement
131
     * getValue().
132
     * 
133
     * @return string
134
     */
135 3
    public function getPersonName()
136
    {
137 3
        if (!empty($this->parts)) {
138 2
            return $this->parts[0]->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ZBateson\MailMimeParser\Header\Part\HeaderPart. It seems like you code against a sub-type of ZBateson\MailMimeParser\Header\Part\HeaderPart such as ZBateson\MailMimeParser\...art\SplitParameterToken or ZBateson\MailMimeParser\Header\Part\ParameterPart or ZBateson\MailMimeParser\...r\Part\AddressGroupPart. ( Ignorable by Annotation )

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

138
            return $this->parts[0]->/** @scrutinizer ignore-call */ getName();
Loading history...
139
        }
140 1
        return null;
141
    }
142
}
143