Completed
Push — master ( f20687...500eb7 )
by ARCANEDEV
03:56
created

SqlServerExpression::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
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
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     SqlServerExpression
8
 *
9
 * @package  Arcanedev\LaravelMetrics\Expressions\TrendDateFormat
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class SqlServerExpression extends Expression
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Get the value of the expression.
21
     *
22
     * @return mixed
23
     */
24 44
    public function getValue()
25
    {
26 44
        $date = "DATEADD(hour, {$this->interval()}, {$this->wrap($this->value)})";
27
28 44
        switch ($this->unit) {
29 22
            case Trend::BY_MONTHS:
30 8
                return "FORMAT({$date}, 'yyyy-MM')";
31
32 18
            case Trend::BY_WEEKS:
33 8
                return "concat(YEAR({$date}), '-', datepart(ISO_WEEK, {$date}))";
34
35 14
            case Trend::BY_DAYS:
36 8
                return "FORMAT({$date}, 'yyyy-MM-dd')";
37
38 10
            case Trend::BY_HOURS:
39 8
                return "FORMAT({$date}, 'yyyy-MM-dd HH:00')";
40
41 6
            case Trend::BY_MINUTES:
42 8
                return "FORMAT({$date}, 'yyyy-MM-dd HH:mm:00')";
43
44
            default:
45 4
                throw InvalidTrendUnitException::make($this->unit);
46
        }
47
    }
48
49
    /**
50
     * Get the interval.
51
     *
52
     * @return string
53
     */
54 44
    private function interval(): string
55
    {
56 44
        $offset = $this->offset();
57
58 44
        if ($offset > 0)
59 20
            return $offset;
60 44
        elseif ($offset === 0)
61 44
            return '';
62
63 20
        return '-'.($offset * -1);
64
    }
65
}
66