Completed
Push — master ( fd75b5...8d9a36 )
by ARCANEDEV
03:38
created

HasRanges::getSelectedRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelMetrics\Metrics\Concerns;
2
3
/**
4
 * Trait     HasRanges
5
 *
6
 * @package  Arcanedev\LaravelMetrics\Metrics\Concerns
7
 * @author   ARCANEDEV <[email protected]>
8
 *
9
 * @property  \Illuminate\Http\Request  $request
10
 *
11
 * @method  array  ranges()
12
 */
13
trait HasRanges
14
{
15
    /* -----------------------------------------------------------------
16
     |  Other Methods
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Convert the ranges for json serialization.
22
     *
23
     * @return array
24
     */
25 4
    public function rangesToArray(): array
26
    {
27 4
        $ranges = method_exists($this, 'ranges') ? $this->ranges() : [];
28
29
        return array_map(function ($value, $label) {
30 4
            return compact('value', 'label');
31 4
        }, array_keys($ranges), $ranges);
32
    }
33
34
    /**
35
     * Calculate the current range.
36
     *
37
     * @param  string|int             $range
38
     * @param  \Cake\Chronos\Chronos  $now
39
     *
40
     * @return array
41
     */
42 20
    protected function currentRange($range, $now): array
43
    {
44
        return [
45 20
            $now->subDays($range),
46 20
            $now,
47
        ];
48
    }
49
50
    /**
51
     * Calculate the previous range.
52
     *
53
     * @param  string|int             $range
54
     * @param  \Cake\Chronos\Chronos  $now
55
     *
56
     * @return array
57
     */
58 20
    protected function previousRange($range, $now): array
59
    {
60
        return [
61 20
            $now->subDays($range * 2),
62 20
            $now->subDays($range)->subSeconds(1),
63
        ];
64
    }
65
}
66