GenericReceivedConsumerService::isEndToken()   A
last analyzed

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\Received;
9
10
use Psr\Log\LoggerInterface;
11
use ZBateson\MailMimeParser\Header\Consumer\AbstractGenericConsumerService;
12
use ZBateson\MailMimeParser\Header\Consumer\CommentConsumerService;
13
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
14
15
/**
16
 * Consumes simple literal strings for parts of a Received header.
17
 *
18
 * Starts consuming when the initialized $partName string is located, for
19
 * instance when initialized with "FROM", will start consuming on " FROM" or
20
 * "FROM ".
21
 *
22
 * The consumer ends when any possible "Received" header part is found, namely
23
 * on one of the following tokens: from, by, via, with, id, for, or when the
24
 * start token for the date stamp is found, ';'.
25
 *
26
 * The consumer allows comments in and around the consumer... although the
27
 * Received header specification only allows them before a part, for example,
28
 * technically speaking this is valid:
29
 *
30
 * "FROM machine (host) (comment) BY machine"
31
 *
32
 * However, this is not:
33
 *
34
 * "FROM machine (host) BY machine WITH (comment) ESMTP"
35
 *
36
 * The consumer will allow both.
37
 *
38
 * @author Zaahid Bateson
39
 */
40
class GenericReceivedConsumerService extends AbstractGenericConsumerService
41
{
42
    /**
43
     * @var string the current part name being parsed.
44
     *
45
     * This is always the lower-case name provided to the constructor, not the
46
     * actual string that started the consumer, which could be in any case.
47
     */
48
    protected $partName;
49
50
    /**
51
     * Constructor overridden to include $partName parameter.
52
     *
53
     */
54 17
    public function __construct(
55
        LoggerInterface $logger,
56
        HeaderPartFactory $partFactory,
57
        CommentConsumerService $commentConsumerService,
58
        string $partName
59
    ) {
60 17
        parent::__construct($logger, $partFactory, [$commentConsumerService]);
61 17
        $this->partName = $partName;
62
    }
63
64
    /**
65
     * Returns true if the passed token matches (case-insensitively)
66
     * $this->getPartName() with optional whitespace surrounding it.
67
     */
68 11
    protected function isStartToken(string $token) : bool
69
    {
70 11
        $pattern = '/^' . \preg_quote($this->partName, '/') . '$/i';
71 11
        return (\preg_match($pattern, $token) === 1);
72
    }
73
74
    /**
75
     * Returns true if the token matches (case-insensitively) any of the
76
     * following, with optional surrounding whitespace:
77
     *
78
     * o by
79
     * o via
80
     * o with
81
     * o id
82
     * o for
83
     * o ;
84
     */
85 14
    protected function isEndToken(string $token) : bool
86
    {
87 14
        return (\preg_match('/^(by|via|with|id|for|;)$/i', $token) === 1);
88
    }
89
90
    /**
91
     * Returns a whitespace separator (for filtering ignorable whitespace
92
     * between parts), and a separator matching the current part name as
93
     * set on $this->partName.
94
     *
95
     * @return string[] an array of regex pattern matchers
96
     */
97 16
    protected function getTokenSeparators() : array
98
    {
99 16
        return [
100 16
            '\s+',
101 16
            '(\A\s*|\s+)(?i)' . \preg_quote($this->partName, '/') . '(?-i)(?=\s+)'
102 16
        ];
103
    }
104
105
    /**
106
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
107
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[]
108
     */
109 10
    protected function processParts(array $parts) : array
110
    {
111 10
        return [$this->partFactory->newReceivedPart($this->partName, $parts)];
112
    }
113
}
114