Test Failed
Push — master ( e258e4...a626ba )
by Zaahid
15:25
created

ParameterConsumerService::getPartFor()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 9.9332
cc 4
nc 3
nop 3
crap 4
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\IHeaderPart;
13
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
14
use ZBateson\MailMimeParser\Header\Part\ParameterPart;
15
16
/**
17
 * Reads headers separated into parameters consisting of an optional main value,
18
 * and subsequent name/value pairs - for example text/html; charset=utf-8.
19
 *
20
 * A ParameterConsumerService's parts are separated by a semi-colon.  Its
21
 * name/value pairs are separated with an '=' character.
22
 *
23
 * Parts may be mime-encoded entities.  Additionally, a value can be quoted and
24
 * comments may exist.
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class ParameterConsumerService extends AbstractGenericConsumerService
29
{
30
    use QuotedStringMimeLiteralPartTokenSplitPatternTrait;
31
32
    public function __construct(
33
        LoggerInterface $logger,
34
        HeaderPartFactory $partFactory,
35
        ParameterNameValueConsumerService $parameterNameValueConsumerService,
36 18
        CommentConsumerService $commentConsumerService,
37
        QuotedStringConsumerService $quotedStringConsumerService
38 18
    ) {
39
        parent::__construct(
40
            $logger,
41
            $partFactory,
42
            [$parameterNameValueConsumerService, $commentConsumerService, $quotedStringConsumerService]
43
        );
44
    }
45
46
    /**
47
     * Disables advancing for start tokens.
48
     *
49
     * @return static
50
     */
51
    protected function advanceToNextToken(Iterator $tokens, bool $isStartToken) : static
52 18
    {
53
        if ($isStartToken) {
54 18
            return $this;
55 18
        }
56 18
        parent::advanceToNextToken($tokens, $isStartToken);
57
        return $this;
58
    }
59
60
    /**
61
     * Post processing involves creating Part\LiteralPart or Part\ParameterPart
62
     * objects out of created Token and LiteralParts.
63
     *
64 118
     * @param IHeaderPart[] $parts The parsed parts.
65
     * @return IHeaderPart[] Array of resulting final parts.
66 118
     */
67 1
    protected function processParts(array $parts) : array
68
    {
69 118
        $factory = $this->partFactory;
70
        return \array_values(\array_map(
71
            function ($partsArray) use ($factory) {
72
                if (count($partsArray) > 1) {
73
                    return $factory->newSplitParameterPart($partsArray);
74
                }
75
                return $partsArray[0];
76
            },
77 13
            \array_merge_recursive(...\array_map(
78
                function ($p) {
79
                    if ($p instanceOf ParameterPart && $p->getIndex() !== null) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected instanceof, but found instanceOf.
Loading history...
80
                        return [strtolower($p->getName()) => [$p]];
81
                    } else {
82
                        return [';' . spl_object_id($p) . ';' => [$p]];
83
                    }
84 13
                },
85 13
                $parts
86 13
            ))
87 13
        ));
88
    }
89
}
90