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

CommentPart::getValueFromParts()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
rs 9.9666
ccs 0
cts 0
cp 0
cc 3
nc 1
nop 1
crap 12
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\Part;
9
10
use Psr\Log\LoggerInterface;
11
use ZBateson\MbWrapper\MbWrapper;
12
13
/**
14
 * Represents a mime header comment -- text in a structured mime header
15
 * value existing within parentheses.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
class CommentPart extends ContainerPart
20
{
21
    /**
22
     * @var HeaderPartFactory used to create intermediate parts.
23
     */
24
    protected HeaderPartFactory $partFactory;
25
26
    /**
27
     * @var string the contents of the comment
28 17
     */
29
    protected string $comment;
30 17
31 17
    public function __construct(
32 17
        LoggerInterface $logger,
33 17
        MbWrapper $charsetConverter,
34 17
        HeaderPartFactory $partFactory,
35
        array $children
36
    ) {
37
        $this->partFactory = $partFactory;
38
        parent::__construct($logger, $charsetConverter, $children);
39
        $this->comment = $this->value;
40 2
        $this->value = '';
41
        $this->isSpace = true;
42 2
        $this->canIgnoreSpacesBefore = true;
43
        $this->canIgnoreSpacesAfter = true;
44
    }
45
46
    protected function getValueFromParts(array $parts) : string
47
    {
48
        $partFactory = $this->partFactory;
49
        return parent::getValueFromParts(\array_map(
50
            function ($p) use ($partFactory) {
51
                if ($p instanceof CommentPart) {
52
                    return $partFactory->newQuotedLiteralPart([$partFactory->newToken('(' . $p->getComment() . ')')]);
53
                } elseif ($p instanceof QuotedLiteralPart) {
54
                    return $partFactory->newQuotedLiteralPart([$partFactory->newToken('"' . \str_replace('(["\\])', '\$1', $p->getValue()) . '"')]);
55
                }
56
                return $p;
57
            },
58
            $parts
59
        ));
60
    }
61
62
    /**
63
     * Returns the comment's text.
64
     */
65
    public function getComment() : string
66
    {
67
        return $this->comment;
68
    }
69
70
    /**
71
     * Returns an empty string.
72
     */
73
    public function getValue() : string
74
    {
75
        return '';
76
    }
77
}
78