Completed
Push — master ( 0f05a9...994973 )
by ARCANEDEV
06:19
created

SqliteExpression   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2
ccs 19
cts 21
cp 0.9048
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getValue() 0 24 6
A interval() 0 11 3
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 48
    public function getValue()
26
    {
27 48
        $interval = $this->interval();
28
29 48
        switch ($this->unit) {
30 24
            case Trend::BY_MONTHS:
31 8
                return "strftime('%Y-%m', datetime({$this->wrap($this->value)}, {$interval}))";
32
33 20
            case Trend::BY_WEEKS:
34 8
                return "strftime('%Y-%W', datetime({$this->wrap($this->value)}, {$interval}))";
35
36 16
            case Trend::BY_DAYS:
37 16
                return "strftime('%Y-%m-%d', datetime({$this->wrap($this->value)}, {$interval}))";
38
39 8
            case Trend::BY_HOURS:
40 8
                return "strftime('%Y-%m-%d %H:00', datetime({$this->wrap($this->value)}, {$interval}))";
41
42 4
            case Trend::BY_MINUTES:
43 8
                return "strftime('%Y-%m-%d %H:%M:00', datetime({$this->wrap($this->value)}, {$interval}))";
44
45
            default:
46
                throw InvalidTrendUnitException::make($this->unit);
47
        }
48
    }
49
50
    /**
51
     * Get the interval.
52
     *
53
     * @return string
54
     */
55 48
    private function interval(): string
56
    {
57 48
        $offset = $this->offset();
58
59 48
        if ($offset > 0)
60
            return '\'+'.$offset.' hour\'';
61 48
        elseif ($offset === 0)
62 28
            return '\'+0 hour\'';
63
        else
64 20
            return '\'-'.($offset * -1).' hour\'';
65
    }
66
}
67