Completed
Push — master ( 3b2998...e962b5 )
by Artem
01:00
created

DateTimeHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 85
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the DateTime library.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\DateTime;
14
15
use Fresh\DateTime\Exception\UnexpectedValueException;
16
17
/**
18
 * DateTimeHelper.
19
 *
20
 * @author Artem Henvald <[email protected]>
21
 */
22
class DateTimeHelper
23
{
24
    private const INTERNAL_DATE_FORMAT = 'Y-m-d';
25
26
    private $datesCache = [];
27
28
    /**
29
     * @param \DateTimeZone|null $timeZone
30
     *
31
     * @return \DateTime
32
     */
33
    public function getCurrentDatetime(?\DateTimeZone $timeZone = null): \DateTime
34
    {
35
        return new \DateTime('now', $timeZone);
36
    }
37
38
    /**
39
     * @param \DateTimeZone|null $timeZone
40
     *
41
     * @return \DateTimeImmutable
42
     */
43
    public function getCurrentDatetimeImmutable(?\DateTimeZone $timeZone = null): \DateTimeImmutable
44
    {
45
        return new \DateTimeImmutable('now', $timeZone);
46
    }
47
48
    /**
49
     * @param DateRange $dateRange
50
     *
51
     * @throws UnexpectedValueException
52
     *
53
     * @return \DateTimeInterface[]
54
     */
55
    public function getDatesFromDateRangeAsArrayOfObjects(DateRange $dateRange): array
56
    {
57
        $cacheKeyForDateRange = $this->getCacheKeyForDateRange($dateRange);
58
59
        if (!isset($this->datesCache[$cacheKeyForDateRange])) {
60
            $dateRange->assertSameTimezones();
61
62
            $since = $this->cloneDateTime($dateRange->getSince());
63
            $since->setTime(0, 0);
64
65
            $till = $this->cloneDateTime($dateRange->getTill());
66
            $till->setTime(23, 59, 59);
67
68
            $datesAsObjects = [];
69
            $period = new \DatePeriod($since, new \DateInterval('P1D'), $till);
70
            foreach ($period as $date) {
71
                $datesAsObjects[] = $date;
72
            }
73
74
            $this->datesCache[$cacheKeyForDateRange] = $datesAsObjects;
75
        }
76
77
        return $this->datesCache[$cacheKeyForDateRange];
78
    }
79
80
    /**
81
     * @param DateRange $dateRange
82
     *
83
     * @return string[]
84
     */
85
    public function getDatesFromDateRangeAsArrayOfStrings(DateRange $dateRange): array
86
    {
87
        $datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange);
88
89
        $datesAsStrings = [];
90
        foreach ($datesAsObjects as $datesAsObject) {
91
            $datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT);
92
        }
93
94
        return $datesAsStrings;
95
    }
96
97
    /**
98
     * @param DateRange $dateRange
99
     *
100
     * @return string
101
     */
102
    private function getCacheKeyForDateRange(DateRange $dateRange): string
103
    {
104
        $since = $dateRange->getSince();
105
        $till = $dateRange->getTill();
106
107
        return \sprintf(
108
            '%s_%s_%s_%s',
109
            $since->format(self::INTERNAL_DATE_FORMAT),
110
            $since->getTimezone()->getName(),
111
            $till->format(self::INTERNAL_DATE_FORMAT),
112
            $till->getTimezone()->getName(),
113
        );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
114
    }
115
116
    /**
117
     * @param \DateTimeInterface $originalDate
118
     *
119
     * @throws UnexpectedValueException
120
     *
121
     * @return \DateTime
122
     */
123
    private function cloneDateTime(\DateTimeInterface $originalDate): \DateTime
124
    {
125
        $date = \DateTime::createFromFormat(
126
            \DateTime::RFC3339,
127
            $originalDate->format(\DateTime::RFC3339),
128
            $originalDate->getTimezone()
129
        );
130
131
        if (!$date instanceof \DateTime) {
132
            throw new UnexpectedValueException(\sprintf('Could not create %s object', \DateTime::class));
133
        }
134
135
        return $date;
136
    }
137
}
138