Completed
Push — master ( 994973...a422c2 )
by ARCANEDEV
06:22
created

TrendDatePeriod::make()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11.9991

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 21
cp 0.6667
rs 8.0555
c 0
b 0
f 0
cc 9
nc 26
nop 4
crap 11.9991
1
<?php namespace Arcanedev\LaravelMetrics\Helpers;
2
3
use Arcanedev\LaravelMetrics\Metrics\Trend;
4
use Cake\Chronos\Chronos;
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class     TrendDatePeriod
9
 *
10
 * @package  Arcanedev\LaravelMetrics\Helpers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class TrendDatePeriod
14
{
15
    /* -----------------------------------------------------------------
16
     |  Main Methods
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * @param  \Cake\Chronos\Chronos  $start
22
     * @param  \Cake\Chronos\Chronos  $end
23
     * @param  string                 $unit
24
     * @param  mixed                  $timezone
25
     *
26
     * @return \Illuminate\Support\Collection
27
     */
28 12
    public static function make(Chronos $start, Chronos $end, string $unit, $timezone)
29
    {
30 12
        $period = new Collection;
31 12
        $next   = $start;
32
33 12
        if ( ! empty($timezone)) {
34
            $next = $start->setTimezone($timezone);
35
            $end  = $end->setTimezone($timezone);
36
        }
37
38 12
        $period->push($next);
39
40 12
        while ($next->lt($end)) {
41 12
            if ($unit === Trend::BY_MONTHS)
42 4
                $next = $next->addMonths(1);
43 8
            elseif ($unit === Trend::BY_WEEKS)
44
                $next = $next->addWeeks(1);
45 8
            elseif ($unit === Trend::BY_DAYS)
46 8
                $next = $next->addDays(1);
47
            elseif ($unit === Trend::BY_HOURS)
48
                $next = $next->addHours(1);
49
            elseif ($unit === Trend::BY_MINUTES)
50
                $next = $next->addMinutes(1);
51
52 12
            if ($next->lte($end))
53 12
                $period->push($next);
54
        }
55
56 12
        return $period;
57
    }
58
}
59