HasRanges   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 13
cts 13
cp 1
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rangesToArray() 0 8 2
A currentRange() 0 7 1
A previousRange() 0 7 1
A getCurrentTimezone() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Metrics\Concerns;
6
7
use Illuminate\Http\Request;
8
9
/**
10
 * Trait     HasRanges
11
 *
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @property  \Illuminate\Http\Request  $request
15
 *
16
 * @method  array  ranges()
17
 */
18
trait HasRanges
19
{
20
    /* -----------------------------------------------------------------
21
     |  Other Methods
22
     | -----------------------------------------------------------------
23
     */
24
25
    /**
26
     * Convert the ranges for json serialization.
27
     *
28
     * @return array
29
     */
30 8
    public function rangesToArray(): array
31
    {
32 8
        $ranges = method_exists($this, 'ranges') ? $this->ranges() : [];
33
34 8
        return array_map(function ($value, $label) {
35 4
            return compact('value', 'label');
36 8
        }, array_keys($ranges), $ranges);
37
    }
38
39
    /**
40
     * Calculate the current range.
41
     *
42
     * @param  int                        $range
43
     * @param  \DateTimeZone|string|null  $timezone
44
     *
45
     * @return array
46
     */
47 28
    protected function currentRange(int $range, $timezone): array
48
    {
49
        return [
50 28
            now($timezone)->subDays($range),
51 28
            now($timezone),
52
        ];
53
    }
54
55
    /**
56
     * Calculate the previous range.
57
     *
58
     * @param  int                        $range
59
     * @param  \DateTimeZone|string|null  $timezone
60
     *
61
     * @return array
62
     */
63 28
    protected function previousRange(int $range, $timezone): array
64
    {
65
        return [
66 28
            now($timezone)->subDays($range * 2),
67 28
            now($timezone)->subDays($range)->subSeconds(1),
68
        ];
69
    }
70
71
    /**
72
     * Get the current timezone.
73
     *
74
     * @param \Illuminate\Http\Request $request
75
     *
76
     * @return \DateTimeZone|string|null
77
     */
78 56
    protected function getCurrentTimezone(Request $request)
79
    {
80 56
        return $request->input('timezone');
81
    }
82
}
83