Passed
Push — master ( 9f937e...2b8dea )
by Zaahid
06:38
created

MimeEncodedHeader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
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
namespace ZBateson\MailMimeParser\Header;
8
9
use ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer;
10
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
11
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPart;
12
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
13
14
/**
15
 * Allows a header to be mime-encoded and be decoded with a consumer after
16
 * decoding.
17
 *
18
 * The entire header's value must only consist of mime-encoded parts for this to
19
 * apply.
20
 * 
21
 * @author Zaahid Bateson
22
 */
23
abstract class MimeEncodedHeader extends AbstractHeader
24
{
25
    /**
26
     * @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory for
27
     * mime decoding.
28
     */
29
    protected $mimeLiteralPartFactory;
30
31
    /**
32
     * Includes
33
     *
34
     * @param ConsumerService $consumerService
35
     * @param string $name
36
     * @param string $value
37
     */
38 3
    public function __construct(
39
        MimeLiteralPartFactory $mimeLiteralPartFactory,
40
        ConsumerService $consumerService,
41
        $name,
42
        $value
43
    ) {
44 3
        $this->mimeLiteralPartFactory = $mimeLiteralPartFactory;
45 3
        parent::__construct($consumerService, $name, $value);
46 3
    }
47
48
    /**
49
     * Mime-decodes the raw value if the whole raw value only consists of mime-
50
     * encoded parts and whitespace prior to invoking the passed consumer.
51
     *
52
     * @param AbstractConsumer $consumer
53
     */
54 3
    protected function setParseHeaderValue(AbstractConsumer $consumer)
55
    {
56 3
        $value = $this->rawValue;
57 3
        $matchp = '~^(\s*' . MimeLiteralPart::MIME_PART_PATTERN . '\s*)+$~';
58 3
        if (preg_match($matchp, $value)) {
59 2
            $p = $this->mimeLiteralPartFactory->newInstance($value);
60 2
            $value = $p->getValue();
61
        }
62 3
        $this->parts = $consumer($value);
63 3
    }
64
}
65