Passed
Push — master ( 48cf72...e0d442 )
by Zaahid
03:27
created

parseTokensIntoParts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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