Passed
Pull Request — master (#7065)
by Michael
11:41
created

DateSubFunction::getSql()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 8.0155

Importance

Changes 0
Metric Value
cc 8
eloc 32
nc 8
nop 1
dl 0
loc 42
ccs 30
cts 32
cp 0.9375
crap 8.0155
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\QueryException;
8
use Doctrine\ORM\Query\SqlWalker;
9
use function strtolower;
10
11
/**
12
 * "DATE_SUB(date1, interval, unit)"
13
 */
14
class DateSubFunction extends DateAddFunction
15
{
16
    /**
17
     * @override
18
     * @inheritdoc
19
     */
20 7
    public function getSql(SqlWalker $sqlWalker)
21
    {
22 7
        switch (strtolower($this->unit->value)) {
23 7
            case 'second':
24 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubSecondsExpression(
25 1
                    $this->firstDateExpression->dispatch($sqlWalker),
26 1
                    $this->intervalExpression->dispatch($sqlWalker)
27
                );
28 6
            case 'minute':
29 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubMinutesExpression(
30 1
                    $this->firstDateExpression->dispatch($sqlWalker),
31 1
                    $this->intervalExpression->dispatch($sqlWalker)
32
                );
33 5
            case 'hour':
34 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubHourExpression(
35 1
                    $this->firstDateExpression->dispatch($sqlWalker),
36 1
                    $this->intervalExpression->dispatch($sqlWalker)
37
                );
38 4
            case 'day':
39 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubDaysExpression(
40 1
                    $this->firstDateExpression->dispatch($sqlWalker),
41 1
                    $this->intervalExpression->dispatch($sqlWalker)
42
                );
43 3
            case 'week':
44 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubWeeksExpression(
45 1
                    $this->firstDateExpression->dispatch($sqlWalker),
46 1
                    $this->intervalExpression->dispatch($sqlWalker)
47
                );
48 2
            case 'month':
49 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubMonthExpression(
50 1
                    $this->firstDateExpression->dispatch($sqlWalker),
51 1
                    $this->intervalExpression->dispatch($sqlWalker)
52
                );
53 1
            case 'year':
54 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubYearsExpression(
55 1
                    $this->firstDateExpression->dispatch($sqlWalker),
56 1
                    $this->intervalExpression->dispatch($sqlWalker)
57
                );
58
59
            default:
60
                throw QueryException::semanticalError(
61
                    'DATE_SUB() only supports units of type second, minute, hour, day, week, month and year.'
62
                );
63
        }
64
    }
65
}
66