SqlServerExpression   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 53
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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