Passed
Push — master ( ef1e4a...697fa2 )
by Zaahid
03:45
created

DateHeader::getDateTimeImmutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
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\ConsumerService;
10
use ZBateson\MailMimeParser\Header\Part\DatePart;
11
12
use DateTime;
13
use DateTimeImmutable;
14
15
/**
16
 * Reads a DatePart value header in either RFC 2822 or RFC 822 format.
17
 * 
18
 * @author Zaahid Bateson
19
 */
20
class DateHeader extends AbstractHeader
21
{
22
    /**
23
     * Returns a DateConsumer.
24
     * 
25
     * @param ConsumerService $consumerService
26
     * @return \ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer
27
     */
28 4
    protected function getConsumer(ConsumerService $consumerService)
29
    {
30 4
        return $consumerService->getDateConsumer();
31
    }
32
    
33
    /**
34
     * Convenience method returning the part's DateTime object, or null if the
35
     * date could not be parsed.
36
     * 
37
     * @return \DateTime
38
     */
39 3
    public function getDateTime()
40
    {
41 3
        if (!empty($this->parts) && $this->parts[0] instanceof DatePart) {
42 2
            return $this->parts[0]->getDateTime();
43
        }
44 1
        return null;
45
    }
46
47
    /**
48
     * Returns a DateTimeImmutable for the part's DateTime object, or null if
49
     * the date could not be parsed.
50
     *
51
     * @return \DateTimeImmutable
52
     */
53 2
    public function getDateTimeImmutable()
54
    {
55 2
        $dateTime = $this->getDateTime();
56 2
        if ($dateTime !== null) {
57 1
            return DateTimeImmutable::createFromMutable($dateTime);
58
        }
59 1
        return null;
60
    }
61
}
62