Completed
Push — master ( 1842cb...2414f9 )
by ARCANEDEV
03:37
created

TrendDatePeriod::getNextDate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 8.9457
cc 6
nc 6
nop 2
crap 6
1
<?php namespace Arcanedev\LaravelMetrics\Helpers;
2
3
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException;
4
use Arcanedev\LaravelMetrics\Metrics\Trend;
5
use Cake\Chronos\Chronos;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class     TrendDatePeriod
10
 *
11
 * @package  Arcanedev\LaravelMetrics\Helpers
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class TrendDatePeriod
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Make date period (range) for the trend metric.
23
     *
24
     * @param  \Cake\Chronos\Chronos  $start
25
     * @param  \Cake\Chronos\Chronos  $end
26
     * @param  string                 $unit
27
     * @param  mixed|null             $timezone
28
     *
29
     * @return \Illuminate\Support\Collection
30
     */
31 36
    public static function make(Chronos $start, Chronos $end, string $unit, $timezone = null)
32
    {
33 36
        $period = new Collection;
34 36
        $next   = $start;
35
36 36
        if ( ! empty($timezone)) {
37 20
            $next = $start->setTimezone($timezone);
38 20
            $end  = $end->setTimezone($timezone);
39
        }
40
41 36
        $period->push($next);
42
43 36
        while ($next->lt($end)) {
44 36
            $next = static::getNextDate($unit, $next);
0 ignored issues
show
Bug introduced by
Since getNextDate() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNextDate() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
45
46 32
            if ($next->lte($end))
47 32
                $period->push($next);
48
        }
49
50 32
        return $period;
51
    }
52
53
    /**
54
     * Get the starting date.
55
     *
56
     * @param  string        $unit
57
     * @param  mixed|null    $range
58
     *
59
     * @return \Cake\Chronos\Chronos
60
     */
61 36
    public static function getStartingDate(string $unit, $range = null): Chronos
62
    {
63 36
        $range = empty($range) ? 1 : ($range - 1);
64 36
        $now   = Chronos::now();
65
66 9
        switch ($unit) {
67 27
            case Trend::BY_MONTHS:
68 8
                return $now->subMonths($range)->firstOfMonth()->setTime(0, 0);
69
70 21
            case Trend::BY_WEEKS:
71 4
                return $now->subWeeks($range)->startOfWeek()->setTime(0, 0);
72
73 18
            case Trend::BY_DAYS:
74 12
                return $now->subDays($range)->setTime(0, 0);
75
76 9
            case Trend::BY_HOURS:
77
                return with($now->subHours($range), function (Chronos $date) {
78 4
                    return $date->setTime($date->hour, 0);
79 4
                });
80
81 6
            case Trend::BY_MINUTES:
82
                return with($now->subMinutes($range), function (Chronos $date) {
83 4
                    return $date->setTime($date->hour, $date->minute);
84 4
                });
85
86
            default:
87 4
                throw InvalidTrendUnitException::make($unit);
88
        }
89
    }
90
91
    /* -----------------------------------------------------------------
92
     |  Other Methods
93
     | -----------------------------------------------------------------
94
     */
95
96
    /**
97
     * Get next date.
98
     *
99
     * @param  string                 $unit
100
     * @param  \Cake\Chronos\Chronos  $next
101
     *
102
     * @return \Cake\Chronos\Chronos
103
     */
104 36
    private static function getNextDate(string $unit, Chronos $next): Chronos
105
    {
106 9
        switch ($unit) {
107 27
            case Trend::BY_MONTHS:
108 8
                return $next->addMonths(1);
109
110 21
            case Trend::BY_WEEKS:
111 4
                return $next->addWeeks(1);
112
113 18
            case Trend::BY_DAYS:
114 12
                return $next->addDays(1);
115
116 9
            case Trend::BY_HOURS:
117 4
                return $next->addHours(1);
118
119 6
            case Trend::BY_MINUTES:
120 4
                return $next->addMinutes(1);
121
122
            default:
123 4
                throw InvalidTrendUnitException::make($unit);
124
        }
125
    }
126
}
127