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

PostgresExpression   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2
ccs 21
cts 21
cp 1
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\Metrics\Trend;
5
6
/**
7
 * Class     PostgresExpression
8
 *
9
 * @package  Arcanedev\LaravelMetrics\Expressions\TrendDateFormat
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class PostgresExpression 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
        $interval = $this->interval();
27
28 44
        switch ($this->unit) {
29 33
            case Trend::BY_MONTHS:
30 8
                return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM')";
31
32 27
            case Trend::BY_WEEKS:
33 8
                return "to_char({$this->wrap($this->value)}{$interval}, 'IYYY-IW')";
34
35 21
            case Trend::BY_DAYS:
36 8
                return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM-DD')";
37
38 15
            case Trend::BY_HOURS:
39 8
                return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM-DD HH24:00')";
40
41 9
            case Trend::BY_MINUTES:
42 8
                return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM-DD HH24:mi:00')";
43
44
            default:
45 4
                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 44
    private function interval(): string
60
    {
61 44
        $offset = $this->offset();
62
63 44
        if ($offset > 0)
64 20
            return ' + interval \''.$offset.' hour\'';
65 44
        elseif ($offset === 0)
66 44
            return '';
67
68 20
        return ' - interval \''.($offset * -1).' HOUR\'';
69
    }
70
}
71