1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Helpers; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException; |
8
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Trend; |
9
|
|
|
use Cake\Chronos\Chronos; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TrendDatePeriod |
14
|
|
|
* |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class TrendDatePeriod |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Main Methods |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Make date period (range) for the trend metric. |
26
|
|
|
* |
27
|
|
|
* @param \Cake\Chronos\Chronos $start |
28
|
|
|
* @param \Cake\Chronos\Chronos $end |
29
|
|
|
* @param string $unit |
30
|
|
|
* @param mixed|null $timezone |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Support\Collection |
33
|
|
|
*/ |
34
|
60 |
|
public static function make(Chronos $start, Chronos $end, string $unit, $timezone = null) |
35
|
|
|
{ |
36
|
60 |
|
$period = new Collection; |
37
|
60 |
|
$next = $start; |
38
|
|
|
|
39
|
60 |
|
if ( ! empty($timezone)) { |
40
|
28 |
|
$next = $start->setTimezone($timezone); |
41
|
28 |
|
$end = $end->setTimezone($timezone); |
42
|
|
|
} |
43
|
|
|
|
44
|
60 |
|
$period->push($next); |
45
|
|
|
|
46
|
60 |
|
while ($next->lt($end)) { |
47
|
60 |
|
$next = self::getNextDate($unit, $next); |
|
|
|
|
48
|
|
|
|
49
|
56 |
|
if ($next->lte($end)) |
50
|
48 |
|
$period->push($next); |
51
|
|
|
} |
52
|
|
|
|
53
|
56 |
|
return $period; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the starting date. |
58
|
|
|
* |
59
|
|
|
* @param string $unit |
60
|
|
|
* @param mixed|null $range |
61
|
|
|
* |
62
|
|
|
* @return \Cake\Chronos\Chronos |
63
|
|
|
*/ |
64
|
60 |
|
public static function getStartingDate(string $unit, $range = null): Chronos |
65
|
|
|
{ |
66
|
60 |
|
$range = empty($range) ? 1 : ($range - 1); |
67
|
60 |
|
$now = Chronos::now(); |
68
|
|
|
|
69
|
60 |
|
switch ($unit) { |
70
|
|
|
case Trend::BY_MONTHS: |
71
|
24 |
|
return $now->subMonths($range)->firstOfMonth()->setTime(0, 0); |
72
|
|
|
|
73
|
|
|
case Trend::BY_WEEKS: |
74
|
8 |
|
return $now->subWeeks($range)->startOfWeek()->setTime(0, 0); |
75
|
|
|
|
76
|
|
|
case Trend::BY_DAYS: |
77
|
8 |
|
return $now->subDays($range)->setTime(0, 0); |
|
|
|
|
78
|
|
|
|
79
|
|
|
case Trend::BY_HOURS: |
80
|
8 |
|
return with($now->subHours($range), function (Chronos $date) { |
81
|
8 |
|
return $date->setTime($date->hour, 0); |
82
|
8 |
|
}); |
83
|
|
|
|
84
|
|
|
case Trend::BY_MINUTES: |
85
|
8 |
|
return with($now->subMinutes($range), function (Chronos $date) { |
86
|
8 |
|
return $date->setTime($date->hour, $date->minute); |
87
|
8 |
|
}); |
88
|
|
|
|
89
|
|
|
default: |
90
|
4 |
|
throw InvalidTrendUnitException::make($unit); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/* ----------------------------------------------------------------- |
95
|
|
|
| Other Methods |
96
|
|
|
| ----------------------------------------------------------------- |
97
|
|
|
*/ |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get next date. |
101
|
|
|
* |
102
|
|
|
* @param string $unit |
103
|
|
|
* @param \Cake\Chronos\Chronos $next |
104
|
|
|
* |
105
|
|
|
* @return \Cake\Chronos\Chronos |
106
|
|
|
*/ |
107
|
60 |
|
private static function getNextDate(string $unit, Chronos $next): Chronos |
108
|
|
|
{ |
109
|
60 |
|
switch ($unit) { |
110
|
|
|
case Trend::BY_MONTHS: |
111
|
24 |
|
return $next->addMonths(1); |
112
|
|
|
|
113
|
|
|
case Trend::BY_WEEKS: |
114
|
8 |
|
return $next->addWeeks(1); |
115
|
|
|
|
116
|
|
|
case Trend::BY_DAYS: |
117
|
8 |
|
return $next->addDays(1); |
118
|
|
|
|
119
|
|
|
case Trend::BY_HOURS: |
120
|
8 |
|
return $next->addHours(1); |
121
|
|
|
|
122
|
|
|
case Trend::BY_MINUTES: |
123
|
8 |
|
return $next->addMinutes(1); |
124
|
|
|
|
125
|
|
|
default: |
126
|
4 |
|
throw InvalidTrendUnitException::make($unit); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.