ReceivedConsumerService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 24
rs 9.9
ccs 13
cts 13
cp 1
cc 1
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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