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

DateHeader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 44
ccs 12
cts 17
cp 0.7059
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateTime() 0 6 3
A __construct() 0 12 1
A getDateTimeImmutable() 0 7 2
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;
9
10
use DateTime;
11
use DateTimeImmutable;
12
use Psr\Log\LoggerInterface;
13
use ZBateson\MailMimeParser\MailMimeParser;
14
use ZBateson\MailMimeParser\Header\Consumer\DateConsumerService;
15
use ZBateson\MailMimeParser\Header\Part\DatePart;
16
17
/**
18
 * Reads a DatePart value header in either RFC 2822 or RFC 822 format.
19
 *
20
 * @author Zaahid Bateson
21
 */
22
class DateHeader extends AbstractHeader
23
{
24 23
    public function __construct(
25
        string $name,
26
        string $value,
27
        ?LoggerInterface $logger = null,
28
        ?DateConsumerService $consumerService = null
29
    ) {
30 23
        $di = MailMimeParser::getGlobalContainer();
31 23
        parent::__construct(
32 23
            $logger ?? $di->get(LoggerInterface::class),
33 23
            $consumerService ?? $di->get(DateConsumerService::class),
34 23
            $name,
35 23
            $value
36 23
        );
37
    }
38
39
    /**
40
     * Convenience method returning the part's DateTime object, or null if the
41
     * date could not be parsed.
42
     *
43
     * @return ?DateTime The parsed DateTime object.
44
     */
45 5
    public function getDateTime() : ?DateTime
46
    {
47 5
        if (!empty($this->parts) && $this->parts[0] instanceof DatePart) {
48 4
            return $this->parts[0]->getDateTime();
49
        }
50 1
        return null;
51
    }
52
53
    /**
54
     * Returns a DateTimeImmutable for the part's DateTime object, or null if
55
     * the date could not be parsed.
56
     *
57
     * @return ?DateTimeImmutable The parsed DateTimeImmutable object.
58
     */
59
    public function getDateTimeImmutable() : ?DateTimeImmutable
60
    {
61
        $dateTime = $this->getDateTime();
62
        if ($dateTime !== null) {
63
            return DateTimeImmutable::createFromMutable($dateTime);
64
        }
65
        return null;
66
    }
67
}
68