Passed
Push — master ( 697fa2...45166c )
by Zaahid
03:28
created

DateHeader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

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

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