Passed
Pull Request — master (#7)
by Alex
07:24
created

DateFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DateTime;
6
7
use Arp\DateTime\Exception\DateFactoryException;
8
use Arp\DateTime\Exception\DateIntervalFactoryException;
9
use Arp\DateTime\Exception\DateTimeFactoryException;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\DateTime
14
 */
15
final class DateFactory implements DateFactoryInterface
16
{
17
    /**
18
     * @var DateTimeFactoryInterface
19
     */
20
    private DateTimeFactoryInterface $dateTimeFactory;
21
22
    /**
23
     * @var DateIntervalFactoryInterface
24
     */
25
    private DateIntervalFactoryInterface $dateIntervalFactory;
26
27
    /**
28
     * @param DateTimeFactoryInterface          $dateTimeFactory
29
     * @param DateIntervalFactoryInterface|null $dateIntervalFactory
30
     */
31
    public function __construct(
32
        DateTimeFactoryInterface $dateTimeFactory,
33
        DateIntervalFactoryInterface $dateIntervalFactory = null
34
    ) {
35
        $this->dateTimeFactory = $dateTimeFactory ?? new DateTimeFactory();
36
        $this->dateIntervalFactory = $dateIntervalFactory ?? new DateIntervalFactory();
37
    }
38
39
    /**
40
     * @param null|string               $spec
41
     * @param string|\DateTimeZone|null $timeZone
42
     *
43
     * @return \DateTimeInterface
44
     *
45
     * @throws DateFactoryException
46
     */
47
    public function createDateTime(string $spec = null, $timeZone = null): \DateTimeInterface
48
    {
49
        try {
50
            return $this->dateTimeFactory->createDateTime($spec, $timeZone);
51
        } catch (DateTimeFactoryException $e) {
52
            throw new DateFactoryException($e->getMessage(), $e->getCode(), $e);
53
        }
54
    }
55
56
    /**
57
     * @param string                    $spec
58
     * @param string                    $format
59
     * @param string|\DateTimeZone|null $timeZone
60
     *
61
     * @return \DateTimeInterface
62
     *
63
     * @throws DateFactoryException
64
     */
65
    public function createFromFormat(string $spec, string $format, $timeZone = null): \DateTimeInterface
66
    {
67
        try {
68
            return $this->dateTimeFactory->createFromFormat($spec, $format, $timeZone);
69
        } catch (DateTimeFactoryException $e) {
70
            throw new DateFactoryException($e->getMessage(), $e->getCode(), $e);
71
        }
72
    }
73
74
    /**
75
     * @param string $spec
76
     *
77
     * @return \DateTimeZone
78
     *
79
     * @throws DateFactoryException
80
     */
81
    public function createDateTimeZone(string $spec): \DateTimeZone
82
    {
83
        try {
84
            return $this->dateTimeFactory->createDateTimeZone($spec);
85
        } catch (DateTimeFactoryException $e) {
86
            throw new DateFactoryException($e->getMessage(), $e->getCode(), $e);
87
        }
88
    }
89
90
    /**
91
     * @param string $spec
92
     *
93
     * @return \DateInterval
94
     *
95
     * @throws DateFactoryException
96
     */
97
    public function createDateInterval(string $spec): \DateInterval
98
    {
99
        try {
100
            return $this->dateIntervalFactory->createDateInterval($spec);
101
        } catch (DateIntervalFactoryException $e) {
102
            throw new DateFactoryException($e->getMessage(), $e->getCode(), $e);
103
        }
104
    }
105
106
    /**
107
     * @param \DateTimeInterface $origin
108
     * @param \DateTimeInterface $target
109
     * @param bool               $absolute
110
     *
111
     * @return \DateInterval
112
     *
113
     * @throws DateFactoryException
114
     */
115
    public function diff(\DateTimeInterface $origin, \DateTimeInterface $target, bool $absolute = false): \DateInterval
116
    {
117
        try {
118
            return $this->dateIntervalFactory->diff($origin, $target, $absolute);
119
        } catch (DateIntervalFactoryException $e) {
120
            throw new DateFactoryException($e->getMessage(), $e->getCode(), $e);
121
        }
122
    }
123
}
124