Completed
Push — master ( 1842cb...2414f9 )
by ARCANEDEV
03:37
created

MySqlExpression::interval()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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