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

FormatsTrends   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 35.7%

Importance

Changes 0
Metric Value
wmc 24
lcom 2
cbo 2
dl 0
loc 214
ccs 20
cts 56
cp 0.357
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A formatAggregateKeyByWeeks() 0 7 1
A formatAggregateKeyByDays() 0 4 1
A formatAggregateKeyByHours() 0 4 1
A formatAggregateKeyByMinutes() 0 4 1
A formatLabelByWeeks() 0 10 1
A formatLabelByDays() 0 4 1
A formatLabelByHours() 0 8 2
A formatLabelByMinutes() 0 8 2
B formatAggregateKey() 0 22 6
B formatLabelBy() 0 22 6
A formatAggregateKeyByMonths() 0 4 1
A formatLabelByMonths() 0 4 1
1
<?php namespace Arcanedev\LaravelMetrics\Metrics\Concerns;
2
3
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException;
4
use Cake\Chronos\Chronos;
5
6
/**
7
 * Trait     FormatsTrends
8
 *
9
 * @package  Arcanedev\LaravelMetrics\Metrics\Concerns
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait FormatsTrends
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Format aggregate key.
21
     *
22
     * @param  \Cake\Chronos\Chronos  $date
23
     * @param  string                 $unit
24
     *
25
     * @return string
26
     */
27 12
    protected static function formatAggregateKey(Chronos $date, string $unit): string
28
    {
29
        switch ($unit) {
30 12
            case static::BY_MONTHS:
31 4
                return static::formatAggregateKeyByMonths($date);
32
33 8
            case static::BY_WEEKS:
34
                return static::formatAggregateKeyByWeeks($date);
35
36 8
            case static::BY_DAYS:
37 8
                return static::formatAggregateKeyByDays($date);
38
39
            case static::BY_HOURS:
40
                return static::formatAggregateKeyByHours($date);
41
42
            case static::BY_MINUTES:
43
                return static::formatAggregateKeyByMinutes($date);
44
45
            default:
46
                throw InvalidTrendUnitException::make($unit);
47
        }
48
    }
49
50
    /**
51
     * Format the label.
52
     *
53
     * @param  \Cake\Chronos\Chronos  $date
54
     * @param  string                 $unit
55
     * @param  bool                   $twelveHourTime
56
     *
57
     * @return string
58
     */
59 12
    protected static function formatLabelBy(Chronos $date, string $unit, bool $twelveHourTime = false): string
60
    {
61
        switch ($unit) {
62 12
            case static::BY_MONTHS:
63 4
                return static::formatLabelByMonths($date);
64
65 8
            case static::BY_WEEKS:
66
                return static::formatLabelByWeeks($date);
67
68 8
            case static::BY_DAYS:
69 8
                return static::formatLabelByDays($date);
70
71
            case static::BY_HOURS:
72
                return static::formatLabelByHours($date, $twelveHourTime);
73
74
            case static::BY_MINUTES:
75
                return static::formatLabelByMinutes($date, $twelveHourTime);
76
77
            default:
78
                throw InvalidTrendUnitException::make($unit);
79
        }
80
    }
81
82
    /* -----------------------------------------------------------------
83
     |  Formatters
84
     | -----------------------------------------------------------------
85
     */
86
87
    /**
88
     * Format the given date by months.
89
     *
90
     * @param  \Cake\Chronos\Chronos  $date
91
     *
92
     * @return string
93
     */
94 4
    protected static function formatAggregateKeyByMonths(Chronos $date): string
95
    {
96 4
        return $date->format('Y-m');
97
    }
98
99
    /**
100
     * Format the given date by weeks.
101
     *
102
     * @param  \Cake\Chronos\Chronos  $date
103
     *
104
     * @return string
105
     */
106
    protected static function formatAggregateKeyByWeeks(Chronos $date): string
107
    {
108
        return sprintf('%s %s',
109
            static::formatAggregateKeyByDays($date->startOfWeek()),
110
            static::formatAggregateKeyByDays($date->endOfWeek())
111
        );
112
    }
113
114
    /**
115
     * Format the given date by days.
116
     *
117
     * @param  \Cake\Chronos\Chronos  $date
118
     *
119
     * @return string
120
     */
121 8
    protected static function formatAggregateKeyByDays(Chronos $date): string
122
    {
123 8
        return $date->format('Y-m-d');
124
    }
125
126
    /**
127
     * Format the given date by hours.
128
     *
129
     * @param \Cake\Chronos\Chronos $date
130
     *
131
     * @return string
132
     */
133
    protected static function formatAggregateKeyByHours(Chronos $date): string
134
    {
135
        return $date->format('Y-m-d H:00');
136
    }
137
138
    /**
139
     * Format the given date by minutes.
140
     *
141
     * @param \Cake\Chronos\Chronos $date
142
     *
143
     * @return string
144
     */
145
    protected static function formatAggregateKeyByMinutes(Chronos $date): string
146
    {
147
        return $date->format('Y-m-d H:i');
148
    }
149
150
    /**
151
     * Get the label by Months.
152
     *
153
     * @param  \Cake\Chronos\Chronos  $date
154
     *
155
     * @return string
156
     */
157 4
    protected static function formatLabelByMonths(Chronos $date): string
158
    {
159 4
        return __($date->format('F')).' '.$date->format('Y');
160
    }
161
162
    /**
163
     * Get the label by Weeks.
164
     *
165
     * @param  \Cake\Chronos\Chronos  $date
166
     *
167
     * @return string
168
     */
169
    protected static function formatLabelByWeeks(Chronos $date): string
170
    {
171
        return sprintf('%s %s - %s %s',
172
            __($date->startOfWeek()->format('F')),
173
            $date->startOfWeek()->format('j'),
174
175
            __($date->endOfWeek()->format('F')),
176
            $date->endOfWeek()->format('j')
177
        );
178
    }
179
180
    /**
181
     * Get the label by Days.
182
     *
183
     * @param  \Cake\Chronos\Chronos  $date
184
     *
185
     * @return string
186
     */
187 8
    protected static function formatLabelByDays(Chronos $date): string
188
    {
189 8
        return __($date->format('F')).' '.$date->format('j, Y');
190
    }
191
192
    /**
193
     * Get the label by Hours.
194
     *
195
     * @param  \Cake\Chronos\Chronos  $date
196
     * @param  bool                   $twelveHourTime
197
     *
198
     * @return string
199
     */
200
    protected static function formatLabelByHours(Chronos $date, bool $twelveHourTime = false): string
201
    {
202
        return sprintf('%s %s - %s',
203
            __($date->format('F')),
204
            $date->format('j'),
205
            $date->format($twelveHourTime ? 'g:00 A' : 'G:00')
206
        );
207
    }
208
209
    /**
210
     * Get the label by Minutes.
211
     *
212
     * @param  \Cake\Chronos\Chronos  $date
213
     * @param  bool                   $twelveHourTime
214
     *
215
     * @return string
216
     */
217
    protected static function formatLabelByMinutes(Chronos $date, bool $twelveHourTime): string
218
    {
219
        return sprintf('%s %s - %s',
220
            __($date->format('F')),
221
            $date->format('j'),
222
            $date->format($twelveHourTime ? 'g:i A' : 'G:i')
223
        );
224
    }
225
}
226