MySqlExpression   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 62
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getValue() 0 24 6
A interval() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Expressions\TrendDateFormat;
6
7
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException;
8
use Arcanedev\LaravelMetrics\Metrics\Trend;
9
10
/**
11
 * Class     MySqlExpression
12
 *
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class MySqlExpression extends Expression
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Get the value of the expression.
24
     *
25
     * @return string
26
     */
27 88
    public function getValue(): string
28
    {
29 88
        $interval = $this->interval();
30
31 88
        switch ($this->unit) {
32
            case Trend::BY_MONTHS:
33 16
                return "date_format({$this->wrap($this->value)}{$interval}, '%Y-%m')";
34
35
            case Trend::BY_WEEKS:
36 16
                return "date_format({$this->wrap($this->value)}{$interval}, '%x-%v')";
37
38
            case Trend::BY_DAYS:
39 16
                return "date_format({$this->wrap($this->value)}{$interval}, '%Y-%m-%d')";
40
41
            case Trend::BY_HOURS:
42 16
                return "date_format({$this->wrap($this->value)}{$interval}, '%Y-%m-%d %H:00')";
43
44
            case Trend::BY_MINUTES:
45 16
                return "date_format({$this->wrap($this->value)}{$interval}, '%Y-%m-%d %H:%i:00')";
46
47
            default:
48 8
                throw InvalidTrendUnitException::make($this->unit);
49
        }
50
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Other Methods
54
     | -----------------------------------------------------------------
55
     */
56
57
    /**
58
     * Get the interval.
59
     *
60
     * @return string
61
     */
62 88
    private function interval(): string
63
    {
64 88
        $offset = $this->offset();
65
66 88
        if ($offset > 0) {
67 40
            return " + INTERVAL {$offset} HOUR";
68
        }
69
70 88
        if ($offset === 0) {
71 88
            return '';
72
        }
73
74 40
        return ' - INTERVAL '.($offset * -1).' HOUR';
75
    }
76
}
77