PostgresExpression::interval()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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