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

CommentPart   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 6
eloc 21
c 0
b 0
f 0
dl 0
loc 57
ccs 22
cts 24
cp 0.9167
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A __construct() 0 13 1
A getComment() 0 3 1
A getValueFromParts() 0 13 3
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