Completed
Push — master ( ab502e...07a33d )
by Zaahid
06:14
created

AddressGroupConsumer::processParts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
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\Part\AddressGroupPart;
10
11
/**
12
 * Parses a single group of addresses (as a named-group part of an address
13
 * header).
14
 * 
15
 * Finds addresses using its AddressConsumer sub-consumer separated by commas,
16
 * and ends processing once a semi-colon is found.
17
 * 
18
 * Prior to returning to its calling client, AddressGroupConsumer constructs a
19
 * single Part\AddressGroupPart object filling it with all located addresses, and
20
 * returns it.
21
 * 
22
 * The AddressGroupConsumer extends AddressBaseConsumer to define start/end
23
 * tokens, token separators, and construct a Part\AddressGroupPart for returning to
24
 * clients.
25
 * 
26
 * @author Zaahid Bateson
27
 */
28
class AddressGroupConsumer extends AddressBaseConsumer
29
{
30
    /**
31
     * Overridden to return patterns matching the beginning and end markers of a
32
     * group address: colon and semi-colon (":" and ";") characters.
33
     * 
34
     * @return string[] the patterns
35
     */
36 2
    public function getTokenSeparators()
37
    {
38 2
        return [':', ';'];
39
    }
40
    
41
    /**
42
     * AddressGroupConsumer returns true if the passed token is a semi-colon.
43
     * 
44
     * @param string $token
45
     * @return boolean false
46
     */
47 2
    protected function isEndToken($token)
48
    {
49 2
        return ($token === ';');
50
    }
51
    
52
    /**
53
     * AddressGroupConsumer returns true if the passed token is a colon.
54
     * 
55
     * @param string $token
56
     * @return boolean false
57
     */
58 2
    protected function isStartToken($token)
59
    {
60 2
        return ($token === ':');
61
    }
62
    
63
    /**
64
     * Performs post-processing on parsed parts.
65
     * 
66
     * AddressGroupConsumer returns an array with a single Part\AddressGroupPart
67
     * element with all email addresses from this and any sub-groups.
68
     * 
69
     * @param \ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts
70
     * @return AddressGroupPart[]|array
71
     */
72 2
    protected function processParts(array $parts)
73
    {
74 2
        $emails = [];
75 2
        foreach ($parts as $part) {
76 2
            if ($part instanceof AddressGroupPart) {
77 1
                $emails = array_merge($emails, $part->getAddresses());
78 1
                continue;
79
            }
80 2
            $emails[] = $part;
81 2
        }
82 2
        $group = $this->partFactory->newAddressGroupPart($emails);
83 2
        return [$group];
84
    }
85
}
86