Passed
Push — master ( 1f2094...564202 )
by Zaahid
03:47
created

AddressConsumer::processSinglePart()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.9666
cc 4
nc 4
nop 3
crap 20
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\Consumer;
8
9
use ZBateson\MailMimeParser\Header\IHeaderPart;
10
use ZBateson\MailMimeParser\Header\Part\Token;
11
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart;
12
use ZBateson\MailMimeParser\Header\Part\AddressPart;
13
14
/**
15
 * Parses a single part of an address header.
16
 * 
17
 * Represents a single part of a list of addresses.  A part could be one email
18
 * address, or one 'group' containing multiple addresses.  The consumer ends on
19
 * finding either a comma token, representing a separation between addresses, or
20
 * a semi-colon token representing the end of a group.
21
 * 
22
 * A single email address may consist of just an email, or a name and an email
23
 * address.  Both of these are valid examples of a From header:
24
 *  - From: [email protected]
25
 *  - From: Jon Snow <[email protected]>
26
 * 
27
 * Groups must be named, for example:
28
 *  - To: Winterfell: [email protected], Arya Stark <[email protected]>;
29
 *
30
 * Addresses may contain quoted parts and comments, and names may be mime-header
31
 * encoded.
32
 * 
33
 * @author Zaahid Bateson
34
 */
35
class AddressConsumer extends AbstractConsumer
36
{
37
    /**
38
     * Returns the following as sub-consumers:
39
     *  - {@see AddressGroupConsumer}
40
     *  - {@see CommentConsumer}
41
     *  - {@see QuotedStringConsumer}
42
     * 
43
     * @return AbstractConsumer[] the sub-consumers
44
     */
45 100
    protected function getSubConsumers()
46
    {
47
        return [
48 100
            $this->consumerService->getAddressGroupConsumer(),
49 100
            $this->consumerService->getAddressEmailConsumer(),
50 100
            $this->consumerService->getCommentConsumer(),
51 100
            $this->consumerService->getQuotedStringConsumer(),
52
        ];
53
    }
54
    
55
    /**
56
     * Overridden to return patterns matching end tokens ("," and ";"), and
57
     * whitespace.
58
     * 
59
     * @return string[] the patterns
60
     */
61 100
    public function getTokenSeparators()
62
    {
63 100
        return [ ',', ';', '\s+' ];
64
    }
65
    
66
    /**
67
     * Returns true for commas and semi-colons.
68
     * 
69
     * Although the semi-colon is not strictly the end token of an
70
     * AddressConsumer, it could end a parent AddressGroupConsumer.
71
     * 
72
     * @param string $token
73
     * @return boolean false
74
     */
75 100
    protected function isEndToken($token)
76
    {
77 100
        return ($token === ',' || $token === ';');
78
    }
79
    
80
    /**
81
     * AddressConsumer is "greedy", so this always returns true.
82
     * 
83
     * @param string $token
84
     * @return boolean false
85
     */
86 95
    protected function isStartToken($token)
87
    {
88 95
        return true;
89
    }
90
    
91
    /**
92
     * Checks if the passed part represents the beginning or end of an address
93
     * part (less than/greater than characters) and either appends the value of
94
     * the part to the passed $strValue, or sets up $strName
95
     * 
96
     * @param IHeaderPart $part
97
     * @param string $strName
98
     * @param string $strValue
99
     */
100
    private function processSinglePart(IHeaderPart $part, &$strName, &$strValue)
0 ignored issues
show
Unused Code introduced by
The method processSinglePart() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
101
    {
102
        $pValue = $part->getValue();
103
        if ($part instanceof Token) {
104
            if ($pValue === '<') {
105
                $strName = $strValue;
106
                $strValue = '';
107
                return;
108
            } elseif ($pValue === '>') {
109
                return;
110
            }
111
        }
112
        $strValue .= $pValue;
113
    }
114
    
115
    /**
116
     * Performs final processing on parsed parts.
117
     * 
118
     * AddressConsumer's implementation looks for tokens representing the
119
     * beginning of an address part, to create a Part\AddressPart out of a
120
     * name/address pair, or assign the name part to a parsed
121
     * Part\AddressGroupPart returned from its AddressGroupConsumer
122
     * sub-consumer.
123
     * 
124
     * The returned array consists of a single element - either a
125
     * Part\AddressPart or a Part\AddressGroupPart.
126
     * 
127
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
128
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[]|array
129
     */
130 100
    protected function processParts(array $parts)
131
    {
132 100
        $strName = '';
133 100
        $strEmail = '';
134 100
        foreach ($parts as $part) {
135 100
            if ($part instanceof AddressGroupPart) {
136
                return [
137 1
                    $this->partFactory->newAddressGroupPart(
138 1
                        $part->getAddresses(),
139
                        $strEmail
140
                    )
141
                ];
142 100
            } elseif ($part instanceof AddressPart) {
143 96
                $strName = $strEmail;
144 96
                $strEmail = $part->getEmail();
145 96
                break;
146
            }
147 100
            $strEmail .= $part->getValue();
148
        }
149 100
        return [ $this->partFactory->newAddressPart($strName, $strEmail) ];
150
    }
151
}
152