Completed
Push — docs-installation-setup ( 464016 )
by Kamil
05:18
created

Year   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 9 1
A getSql() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\CoreBundle\Doctrine\DQL;
15
16
use Doctrine\ORM\Query\AST\ArithmeticExpression;
17
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
18
use Doctrine\ORM\Query\Lexer;
19
use Doctrine\ORM\Query\Parser;
20
use Doctrine\ORM\Query\SqlWalker;
21
22
final class Year extends FunctionNode
23
{
24
    /** @var ArithmeticExpression|null */
25
    public $date;
26
27
    public function parse(Parser $parser): void
28
    {
29
        $parser->match(Lexer::T_IDENTIFIER);
30
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
31
32
        $this->date = $parser->ArithmeticPrimary();
33
34
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
35
    }
36
37
    public function getSql(SqlWalker $sqlWalker): string
38
    {
39
        $platformName = $sqlWalker->getConnection()->getDatabasePlatform()->getName();
40
41
        switch ($platformName) {
42
            case 'mysql':
43
                return sprintf('YEAR(%s)', $sqlWalker->walkArithmeticPrimary($this->date));
44
45
            case 'postgresql':
46
                return sprintf('EXTRACT(YEAR FROM %s)', $sqlWalker->walkArithmeticPrimary($this->date));
47
        }
48
49
        throw new \RuntimeException(sprintf('Platform "%s" is not supported!', $platformName));
50
    }
51
}
52