SplitParameterPart   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 36
c 2
b 0
f 0
dl 0
loc 78
ccs 40
cts 40
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A combineAdjacentUnencodedParts() 0 19 5
A getNameFromParts() 0 3 1
A __construct() 0 9 1
A getMimeTokens() 0 10 2
A getValueFromParts() 0 16 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\Part;
9
10
use Psr\Log\LoggerInterface;
11
use ZBateson\MbWrapper\MbWrapper;
12
13
/**
14
 * Holds a running value for an RFC-2231 split header parameter.
15
 *
16
 * ParameterConsumer creates SplitParameterTokens when a split header parameter
17
 * is first found, and adds subsequent split parts to an already created one if
18
 * the parameter name matches.
19
 *
20
 * @author Zaahid Bateson
21
 */
22
class SplitParameterPart extends ParameterPart
23
{
24
    /**
25
     * @var HeaderPartFactory used to create combined MimeToken parts.
26
     */
27
    protected HeaderPartFactory $partFactory;
28
29
    /**
30
     * Initializes a SplitParameterToken.
31
     *
32
     * @param ParameterPart[] $children
33
     */
34 8
    public function __construct(
35
        LoggerInterface $logger,
36
        MbWrapper $charsetConverter,
37
        HeaderPartFactory $headerPartFactory,
38
        array $children
39
    ) {
40 8
        $this->partFactory = $headerPartFactory;
41 8
        NameValuePart::__construct($logger, $charsetConverter, [$children[0]], $children);
42 8
        $this->children = $children;
43
    }
44
45 8
    protected function getNameFromParts(array $parts) : string
46
    {
47 8
        return $parts[0]->getName();
48
    }
49
50 3
    private function getMimeTokens(string $value) : array
51
    {
52 3
        $pattern = MimeToken::MIME_PART_PATTERN;
53
        // remove whitespace between two adjacent mime encoded parts
54 3
        $normed = \preg_replace("/($pattern)\\s+(?=$pattern)/", '$1', $value);
55
        // with PREG_SPLIT_DELIM_CAPTURE, matched and unmatched parts are returned
56 3
        $aMimeParts = \preg_split("/($pattern)/", $normed, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
57 3
        return \array_map(
58 3
            fn ($p) => (\preg_match("/$pattern/", $p)) ? $this->partFactory->newMimeToken($p) : $this->partFactory->newToken($p, true, true),
59 3
            $aMimeParts
60 3
        );
61
    }
62
63 8
    private function combineAdjacentUnencodedParts(array $parts) : array
64
    {
65 8
        $runningValue = '';
66 8
        $returnedParts = [];
67 8
        foreach ($parts as $part) {
68 8
            if (!$part->encoded) {
69 3
                $runningValue .= $part->value;
70 3
                continue;
71
            }
72 6
            if (!empty($runningValue)) {
73 1
                $returnedParts = \array_merge($returnedParts, $this->getMimeTokens($runningValue));
74 1
                $runningValue = '';
75
            }
76 6
            $returnedParts[] = $part;
77
        }
78 8
        if (!empty($runningValue)) {
79 2
            $returnedParts = \array_merge($returnedParts, $this->getMimeTokens($runningValue));
80
        }
81 8
        return $returnedParts;
82
    }
83
84 8
    protected function getValueFromParts(array $parts) : string
85
    {
86 8
        $sorted = $parts;
87 8
        \usort($sorted, fn ($a, $b) => $a->index <=> $b->index);
88
89 8
        $first = $sorted[0];
90 8
        $this->language = $first->language;
91 8
        $charset = $this->charset = $first->charset;
92
93 8
        $combined = $this->combineAdjacentUnencodedParts($sorted);
94
95 8
        return \implode('', \array_map(
96 8
            fn ($p) => ($p instanceof ParameterPart && $p->encoded)
97 6
                ? $this->decodePartValue($p->getValue(), ($p->charset === null) ? $charset : $p->charset)
98 8
                : $p->getValue(),
99 8
            $combined
100 8
        ));
101
    }
102
}
103