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

ReceivedConsumerService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
eloc 30
c 0
b 0
f 0
dl 0
loc 111
ccs 37
cts 37
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenSeparators() 0 3 1
A isStartToken() 0 3 1
A advanceToNextToken() 0 13 6
A isEndToken() 0 3 1
A processParts() 0 10 3
A __construct() 0 22 1
A getTokenSplitPattern() 0 4 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 Iterator;
11
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
12
use ZBateson\MailMimeParser\Header\Part\Token;
13
use ZBateson\MailMimeParser\Header\Consumer\Received\DomainConsumerService;
14
use ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumerService;
15
use ZBateson\MailMimeParser\Header\Consumer\Received\ReceivedDateConsumerService;
16
17
/**
18
 * Parses a Received header into ReceivedParts, ReceivedDomainParts, a DatePart,
19
 * and CommentParts.
20
 *
21
 * Parts that don't correspond to any of the above are discarded.
22
 *
23
 * @author Zaahid Bateson
24
 */
25
class ReceivedConsumerService extends AbstractConsumerService
26
{
27 11
    public function __construct(
28
        HeaderPartFactory $partFactory,
29
        DomainConsumerService $fromDomainConsumerService,
30
        DomainConsumerService $byDomainConsumerService,
31
        GenericReceivedConsumerService $viaGenericReceivedConsumerService,
32
        GenericReceivedConsumerService $withGenericReceivedConsumerService,
33
        GenericReceivedConsumerService $idGenericReceivedConsumerService,
34
        GenericReceivedConsumerService $forGenericReceivedConsumerService,
35
        ReceivedDateConsumerService $receivedDateConsumerService,
36
        CommentConsumerService $commentConsumerService
37
    ) {
38 11
        parent::__construct(
39 11
            $partFactory,
40 11
            [
41 11
                $fromDomainConsumerService,
42 11
                $byDomainConsumerService,
43 11
                $viaGenericReceivedConsumerService,
44 11
                $withGenericReceivedConsumerService,
45 11
                $idGenericReceivedConsumerService,
46 11
                $forGenericReceivedConsumerService,
47 11
                $receivedDateConsumerService,
48 11
                $commentConsumerService
49 11
            ]
50 11
        );
51
    }
52
53
    /**
54
     * ReceivedConsumerService doesn't have any token separators of its own.
55
     * Sub-Consumers will return separators matching 'part' word separators, for
56
     * example 'from' and 'by', and ';' for date, etc...
57
     *
58
     * @return string[] an array of regex pattern matchers
59
     */
60 11
    protected function getTokenSeparators() : array
61
    {
62 11
        return [];
63
    }
64
65
    /**
66
     * ReceivedConsumerService doesn't have an end token, and so this just
67
     * returns false.
68
     */
69 11
    protected function isEndToken(string $token) : bool
70
    {
71 11
        return false;
72
    }
73
74
    /**
75
     * ReceivedConsumerService doesn't start consuming at a specific token, it's
76
     * the base handler for the Received header, and so this always returns
77
     * false.
78
     *
79
     * @codeCoverageIgnore
80
     */
81
    protected function isStartToken(string $token) : bool
82
    {
83
        return false;
84
    }
85
86
    /**
87
     * Overridden to exclude the MimeLiteralPart pattern that comes by default
88
     * in AbstractConsumer.
89
     *
90
     * @return string the regex pattern
91
     */
92 11
    protected function getTokenSplitPattern() : string
93
    {
94 11
        $sChars = \implode('|', $this->getAllTokenSeparators());
95 11
        return '~(' . $sChars . ')~';
96
    }
97
98
    /**
99
     * Overridden to /not/ advance when the end token matches a start token for
100
     * a sub-consumer.
101
     *
102
     * @return static
103
     */
104 11
    protected function advanceToNextToken(Iterator $tokens, bool $isStartToken) : AbstractConsumerService
105
    {
106 11
        if ($isStartToken) {
107 10
            $tokens->next();
108 11
        } elseif ($tokens->valid() && !$this->isEndToken($tokens->current())) {
109 5
            foreach ($this->subConsumers as $consumer) {
110 5
                if ($consumer->isStartToken($tokens->current())) {
111 4
                    return $this;
112
                }
113
            }
114 4
            $tokens->next();
115
        }
116 11
        return $this;
117
    }
118
119
    /**
120
     * Overridden to combine all part values into a single string and return it
121
     * as an array with a single element.
122
     *
123
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
124
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[]
125
     */
126 11
    protected function processParts(array $parts) : array
127
    {
128 11
        $ret = [];
129 11
        foreach ($parts as $part) {
130 11
            if ($part instanceof Token) {
131 3
                continue;
132
            }
133 10
            $ret[] = $part;
134
        }
135 11
        return $ret;
136
    }
137
}
138