Passed
Push — master ( a626ba...b864c0 )
by Zaahid
15:44 queued 12:03
created

CommentPart::getValueFromParts()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 11
cp 0.8182
rs 9.9666
cc 3
nc 1
nop 1
crap 3.054
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
     */
29
    protected string $comment;
30
31 17
    public function __construct(
32
        LoggerInterface $logger,
33
        MbWrapper $charsetConverter,
34
        HeaderPartFactory $partFactory,
35
        array $children
36
    ) {
37 17
        $this->partFactory = $partFactory;
38 17
        parent::__construct($logger, $charsetConverter, $children);
39 17
        $this->comment = $this->value;
40 17
        $this->value = '';
41 17
        $this->isSpace = true;
42 17
        $this->canIgnoreSpacesBefore = true;
43 17
        $this->canIgnoreSpacesAfter = true;
44
    }
45
46 17
    protected function getValueFromParts(array $parts) : string
47
    {
48 17
        $partFactory = $this->partFactory;
49 17
        return parent::getValueFromParts(\array_map(
50 17
            function ($p) use ($partFactory) {
51 17
                if ($p instanceof CommentPart) {
52
                    return $partFactory->newQuotedLiteralPart([$partFactory->newToken('(' . $p->getComment() . ')')]);
53 17
                } elseif ($p instanceof QuotedLiteralPart) {
54
                    return $partFactory->newQuotedLiteralPart([$partFactory->newToken('"' . \str_replace('(["\\])', '\$1', $p->getValue()) . '"')]);
55
                }
56 17
                return $p;
57 17
            },
58 17
            $parts
59 17
        ));
60
    }
61
62
    /**
63
     * Returns the comment's text.
64
     */
65 2
    public function getComment() : string
66
    {
67 2
        return $this->comment;
68
    }
69
70
    /**
71
     * Returns an empty string.
72
     */
73 2
    public function getValue() : string
74
    {
75 2
        return '';
76
    }
77
}
78