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

SubjectConsumerService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 17
c 1
b 0
f 0
dl 0
loc 72
ccs 20
cts 21
cp 0.9524
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenParts() 0 3 1
A processParts() 0 8 2
A getPartForToken() 0 11 4
A getTokenSplitPattern() 0 4 1
A __construct() 0 3 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\MimeLiteralPartFactory;
11
use ZBateson\MailMimeParser\Header\IHeaderPart;
12
use Iterator;
13
14
/**
15
 * Extends AbstractGenericConsumerService to use a MimeLiteralPartFactory, and
16
 * to preserve all whitespace and escape sequences as-is (unlike other headers
17
 * subject headers don't have escape chars such as '\\' for a backslash).
18
 *
19
 * SubjectConsumerService doesn't define any sub-consumers.
20
 *
21
 * @author Zaahid Bateson
22
 */
23
class SubjectConsumerService extends AbstractGenericConsumerService
24
{
25 2
    public function __construct(MimeLiteralPartFactory $partFactory)
26
    {
27 2
        parent::__construct($partFactory);
28
    }
29
30
    /**
31
     * Overridden to preserve whitespace.
32
     *
33
     * Whitespace between two words is preserved unless the whitespace begins
34
     * with a newline (\n or \r\n), in which case the entire string of
35
     * whitespace is discarded, and a single space ' ' character is used in its
36
     * place.
37
     */
38 97
    protected function getPartForToken(string $token, bool $isLiteral) : ?IHeaderPart
39
    {
40 97
        if ($isLiteral) {
41
            return $this->partFactory->newLiteralPart($token);
42 97
        } elseif (\preg_match('/^\s+$/', $token)) {
43 75
            if (\preg_match('/^[\r\n]/', $token)) {
44 10
                return $this->partFactory->newToken(' ');
45
            }
46 75
            return $this->partFactory->newToken($token);
47
        }
48 97
        return $this->partFactory->newInstance($token);
49
    }
50
51
    /**
52
     * Returns an array of \ZBateson\MailMimeParser\Header\Part\HeaderPart for
53
     * the current token on the iterator.
54
     *
55
     * Overridden from AbstractConsumerService to remove special filtering for
56
     * backslash escaping, which also seems to not apply to Subject headers at
57
     * least in ThunderBird's implementation.
58
     *
59
     * @return IHeaderPart[]
60
     */
61 97
    protected function getTokenParts(Iterator $tokens) : array
62
    {
63 97
        return $this->getConsumerTokenParts($tokens);
64
    }
65
66
    /**
67
     * Overridden to not split out backslash characters and its next character
68
     * as a special case defined in AbstractConsumerService
69
     *
70
     * @return string the regex pattern
71
     */
72 2
    protected function getTokenSplitPattern() : string
73
    {
74 2
        $sChars = \implode('|', $this->getAllTokenSeparators());
75 2
        return '~(' . $sChars . ')~';
76
    }
77
78
    /**
79
     * Overridden to combine all part values into a single string and return it
80
     * as an array with a single element.
81
     *
82
     * The returned IHeaderParts are all LiteralParts.
83
     *
84
     * @param IHeaderPart[] $parts
85
     * @return IHeaderPart[]
86
     */
87 97
    protected function processParts(array $parts) : array
88
    {
89 97
        $strValue = '';
90 97
        $filtered = $this->filterIgnoredSpaces($parts);
91 97
        foreach ($filtered as $part) {
92 97
            $strValue .= $part->getValue();
93
        }
94 97
        return [$this->partFactory->newLiteralPart($strValue)];
95
    }
96
}
97