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

IdConsumerService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
c 0
b 0
f 0
dl 0
loc 54
ccs 16
cts 16
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isEndToken() 0 3 1
A processParts() 0 7 1
A isStartToken() 0 3 1
A getPartForToken() 0 6 2
A getTokenSeparators() 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\IHeaderPart;
11
use ZBateson\MailMimeParser\Header\Part\CommentPart;
12
13
/**
14
 * Parses a single ID from an ID header.  Begins consuming on a '<' char, and
15
 * ends on a '>' char.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
class IdConsumerService extends GenericConsumerService
20
{
21
    /**
22
     * Overridden to return patterns matching the beginning part of an ID ('<'
23
     * and '>' chars).
24
     *
25
     * @return string[] the patterns
26
     */
27 4
    public function getTokenSeparators() : array
28
    {
29 4
        return ['\s+', '<', '>'];
30
    }
31
32
    /**
33
     * Returns true for '>'.
34
     */
35 84
    protected function isEndToken(string $token) : bool
36
    {
37 84
        return ($token === '>');
38
    }
39
40
    /**
41
     * Returns true for '<'.
42
     */
43 82
    protected function isStartToken(string $token) : bool
44
    {
45 82
        return ($token === '<');
46
    }
47
48
    /**
49
     * Returns null for whitespace, and LiteralPart for anything else.
50
     */
51 84
    protected function getPartForToken(string $token, bool $isLiteral) : ?IHeaderPart
52
    {
53 84
        if (\preg_match('/^\s+$/', $token)) {
54 2
            return null;
55
        }
56 84
        return $this->partFactory->newLiteralPart($token);
57
    }
58
59
    /**
60
     * Overridden to combine non-comment parts into a single part and return
61
     * any comment parts after.
62
     *
63
     * @param IHeaderPart[] $parts
64
     * @return IHeaderPart[]
65
     */
66 84
    protected function processParts(array $parts) : array
67
    {
68 84
        $id = \array_reduce(\array_filter($parts), function ($c, $p) {
69 84
            return $c . $p->getValue();
70 84
        }, '');
71 84
        return array_merge([$this->partFactory->newLiteralPart($id)], \array_values(\array_filter($parts, function ($p) {
72 84
            return ($p instanceof CommentPart);
73 84
        })));
74
    }
75
}
76