Completed
Pull Request — master (#19)
by John
05:56 queued 03:42
created

AbstractFieldParserDate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getText() 0 10 2
getDateTime() 0 1 ?
A getDateFormat() 0 4 1
A factory() 0 6 1
1
<?php
2
3
namespace Graze\CiffRenderer\Parser\FieldParser\FieldParserDate;
4
5
use Graze\CiffRenderer\Parser\FieldParser\FieldParserFixedText;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
7
use Graze\CiffRenderer\Parser\FieldParser\FieldParserDate\DateFormatter\DateFormatterFactory;
8
use Graze\CiffRenderer\Exception\UnsupportedDateFormatException;
9
10
abstract class AbstractFieldParserDate extends FieldParserFixedText implements FieldParserInterface
11
{
12
    /**
13
     * @var DateFormatterFactory
14
     */
15
    private $dateFormatterFactory;
16
17
    /**
18
     * @param DateFormatterFactory $dateFormatterFactory
19
     */
20
    public function __construct(DateFormatterFactory $dateFormatterFactory)
21
    {
22
        $this->dateFormatterFactory = $dateFormatterFactory;
23
    }
24
25
    /**
26
     * @return string
27
     * @throws UnsupportedDateFormatException
28
     */
29
    public function getText()
30
    {
31
        try {
32
            $dateFormat = $this->getDateFormat();
33
            $dateFormatter = $this->dateFormatterFactory->getFormatter($dateFormat);
34
            return $dateFormatter->format($this->getDateTime(), $dateFormat);
35
        } catch (\Exception $e) {
36
            throw new UnsupportedDateFormatException($this->getDateFormat());
37
        }
38
    }
39
40
    /**
41
     * Fetch a DateTime object set to the correct date/time.
42
     *
43
     * @return \DateTimeInterface
44
     */
45
    abstract protected function getDateTime();
46
47
    /**
48
     * @return string
49
     */
50
    public function getDateFormat()
51
    {
52
        return (string) $this->xmlField->Data->Object->Default;
53
    }
54
55
    /**
56
     * @return FieldParserInterface
57
     */
58
    public static function factory()
59
    {
60
        return new static(
61
            new DateFormatterFactory()
62
        );
63
    }
64
}
65