Passed
Pull Request — master (#7)
by Alex
12:47
created

DateFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 40
c 1
b 0
f 1
dl 0
loc 127
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createDateInterval() 0 9 2
A diff() 0 9 2
A createFromFormat() 0 9 2
A createDateTime() 0 9 2
A createDateTimeZone() 0 9 2
A __construct() 0 6 1
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 string|null $spec
41
     * @param null        $timeZone
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $timeZone is correct as it would always require null to be passed?
Loading history...
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(
53
                $e->getMessage(),
54
                $e->getCode(),
55
                $e
56
            );
57
        }
58
    }
59
60
    /**
61
     * @param string $spec
62
     * @param string $format
63
     * @param null   $timeZone
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $timeZone is correct as it would always require null to be passed?
Loading history...
64
     *
65
     * @return \DateTimeInterface
66
     *
67
     * @throws DateFactoryException
68
     */
69
    public function createFromFormat(string $spec, string $format, $timeZone = null): \DateTimeInterface
70
    {
71
        try {
72
            return $this->dateTimeFactory->createFromFormat($spec, $format, $timeZone);
73
        } catch (DateTimeFactoryException $e) {
74
            throw new DateFactoryException(
75
                $e->getMessage(),
76
                $e->getCode(),
77
                $e
78
            );
79
        }
80
    }
81
82
    /**
83
     * @param string $spec
84
     *
85
     * @return \DateTimeZone
86
     *
87
     * @throws DateFactoryException
88
     */
89
    public function createDateTimeZone(string $spec): \DateTimeZone
90
    {
91
        try {
92
            return $this->dateTimeFactory->createDateTimeZone($spec);
93
        } catch (DateTimeFactoryException $e) {
94
            throw new DateFactoryException(
95
                $e->getMessage(),
96
                $e->getCode(),
97
                $e
98
            );
99
        }
100
    }
101
102
    /**
103
     * @param string $spec
104
     *
105
     * @return \DateInterval
106
     *
107
     * @throws DateFactoryException
108
     */
109
    public function createDateInterval(string $spec): \DateInterval
110
    {
111
        try {
112
            return $this->dateIntervalFactory->createDateInterval($spec);
113
        } catch (DateIntervalFactoryException $e) {
114
            throw new DateFactoryException(
115
                sprintf('Failed to create date interval \'%s\': %s', $spec, $e->getMessage()),
116
                $e->getCode(),
117
                $e
118
            );
119
        }
120
    }
121
122
    /**
123
     * Perform a diff of two dates and return the \DateInterval
124
     *
125
     * @param \DateTimeInterface $origin   The origin date
126
     * @param \DateTimeInterface $target   The date to compare to
127
     * @param bool               $absolute If the interval is negative, should it be forced to be a positive value?
128
     *
129
     * @return \DateInterval
130
     *
131
     * @throws DateFactoryException If the date diff cannot be performed
132
     */
133
    public function diff(\DateTimeInterface $origin, \DateTimeInterface $target, bool $absolute = false): \DateInterval
134
    {
135
        try {
136
            return $this->dateIntervalFactory->diff($origin, $target, $absolute);
137
        } catch (DateIntervalFactoryException $e) {
138
            throw new DateFactoryException(
139
                sprintf('Failed to perform date diff: %s', $e->getMessage()),
140
                $e->getCode(),
141
                $e
142
            );
143
        }
144
    }
145
}
146