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

AddressGroupConsumerService::isStartToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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