ParameterNameValueConsumerService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 64
ccs 20
cts 20
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isStartToken() 0 3 1
A processParts() 0 10 2
A isEndToken() 0 3 1
A __construct() 0 11 1
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 Psr\Log\LoggerInterface;
11
use ZBateson\MailMimeParser\Header\IHeaderPart;
12
use ZBateson\MailMimeParser\Header\Part\ContainerPart;
13
use ZBateson\MailMimeParser\Header\Part\MimeTokenPartFactory;
14
15
/**
16
 * Parses an individual part of a parameter header.
17
 *
18
 * 'isStartToken' always returns true, so control is taken from
19
 * ParameterConsumerService always, and returned when a ';' is encountered (and
20
 * so processes a single part and returns it, then gets control back).0
21
 *
22
 * If an '=' is encountered, the ParameterValueConsumerService sub-consumer
23
 * takes control and parses the value of a parameter.
24
 *
25
 * If no '=' is encountered, it's assumed to be a single value element, which
26
 * should be the first part of a parameter header, e.g. 'text/html' in
27
 * Content-Type: text/html; charset=utf-8
28
 *
29
 * @author Zaahid Bateson
30
 */
31
class ParameterNameValueConsumerService extends AbstractGenericConsumerService
32
{
33 9
    public function __construct(
34
        LoggerInterface $logger,
35
        MimeTokenPartFactory $partFactory,
36
        ParameterValueConsumerService $parameterValueConsumerService,
37
        CommentConsumerService $commentConsumerService,
38
        QuotedStringConsumerService $quotedStringConsumerService
39
    ) {
40 9
        parent::__construct(
41 9
            $logger,
42 9
            $partFactory,
43 9
            [$parameterValueConsumerService, $commentConsumerService, $quotedStringConsumerService]
44 9
        );
45
    }
46
47
    /**
48
     * Returns semi-colon as a token separator, in addition to parent token
49
     * separators.
50
     *
51
     * @return string[]
52
     */
53 10
    protected function getTokenSeparators() : array
54
    {
55 10
        return \array_merge(parent::getTokenSeparators(), [';']);
56
    }
57
58
    /**
59
     * Always returns true to grab control from its parent
60
     * ParameterConsumerService.
61
     */
62 103
    protected function isStartToken(string $token) : bool
63
    {
64 103
        return true;
65
    }
66
67
    /**
68
     * Returns true if the token is a ';' char.
69
     */
70 111
    protected function isEndToken(string $token) : bool
71
    {
72 111
        return ($token === ';');
73
    }
74
75
    /**
76
     * Creates either a ContainerPart if an '=' wasn't encountered, indicating
77
     * this to be the main 'value' part of a header (or a malformed part of a
78
     * parameter header), or a ParameterPart if the last IHeaderPart in the
79
     * passed $parts array is already a ContainerPart (indicating it was parsed
80
     * in ParameterValueConsumerService.)
81
     *
82
     * @param IHeaderPart[] $parts The parsed parts.
83
     * @return IHeaderPart[] Array of resulting final parts.
84
     */
85 111
    protected function processParts(array $parts) : array
86
    {
87 111
        $nameOnly = $parts;
88 111
        $valuePart = \array_pop($nameOnly);
89 111
        if (!($valuePart instanceof ContainerPart)) {
90 105
            return [$this->partFactory->newContainerPart($parts)];
91
        }
92 108
        return [$this->partFactory->newParameterPart(
93 108
            $nameOnly,
94 108
            $valuePart
95 108
        )];
96
    }
97
}
98