Completed
Push — master ( c45db1...5d2dfa )
by ARCANEDEV
05:49
created

HasRanges::ranges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 2
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
     |  Getters
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Get the selected range.
22
     *
23
     * @return mixed
24
     */
25 20
    public function getSelectedRange()
26
    {
27 20
        return $this->request->input('range');
28
    }
29
30
    /* -----------------------------------------------------------------
31
     |  Other Methods
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Convert the ranges for json serialization.
37
     *
38
     * @return array
39
     */
40 4
    public function rangesToArray(): array
41
    {
42 4
        $ranges = method_exists($this, 'ranges') ? $this->ranges() : [];
43
44
        return array_map(function ($value, $label) {
45 4
            return compact('value', 'label');
46 4
        }, array_keys($ranges), $ranges);
47
    }
48
49
    /**
50
     * Calculate the current range.
51
     *
52
     * @param  string|int             $range
53
     * @param  \Cake\Chronos\Chronos  $now
54
     *
55
     * @return array
56
     */
57 20
    protected function currentRange($range, $now): array
58
    {
59
        return [
60 20
            $now->subDays($range),
61 20
            $now,
62
        ];
63
    }
64
65
    /**
66
     * Calculate the previous range.
67
     *
68
     * @param  string|int             $range
69
     * @param  \Cake\Chronos\Chronos  $now
70
     *
71
     * @return array
72
     */
73 20
    protected function previousRange($range, $now): array
74
    {
75
        return [
76 20
            $now->subDays($range * 2),
77 20
            $now->subDays($range)->subSeconds(1),
78
        ];
79
    }
80
}
81