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

SqliteExpression::getValue()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

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