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

DateHeader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 40
ccs 11
cts 11
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateTime() 0 6 3
A getConsumer() 0 3 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
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