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

DateConsumerService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 8
c 0
b 0
f 0
dl 0
loc 33
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processParts() 0 10 2
A getPartForToken() 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 date header into a Part\DatePart taking care of comment and quoted
15
 * parts as necessary.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
class DateConsumerService extends GenericConsumerService
20
{
21
    /**
22
     * Returns a Part\LiteralPart for the current token
23
     *
24
     * @param string $token the token
25
     * @param bool $isLiteral set to true if the token represents a literal -
26
     *        e.g. an escaped token
27
     */
28 21
    protected function getPartForToken(string $token, bool $isLiteral) : ?IHeaderPart
29
    {
30 21
        return $this->partFactory->newLiteralPart($token);
31
    }
32
33
    /**
34
     * Concatenates the passed parts and constructs a single Part\DatePart,
35
     * returning it in an array with a single element.
36
     *
37
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts The parsed
38
     *        parts.
39
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[] Array of resulting
40
     *         final parts.
41
     */
42 21
    protected function processParts(array $parts) : array
43
    {
44 21
        $strValue = '';
45 21
        foreach ($parts as $part) {
46 21
            $strValue .= $part->getValue();
47
        }
48 21
        $comments = \array_values(\array_filter($parts, function ($part) {
49 21
            return ($part instanceof CommentPart);
50 21
        }));
51 21
        return \array_merge([$this->partFactory->newDatePart($strValue)], $comments);
52
    }
53
}
54