RouteExpression   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 23
dl 0
loc 74
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addDefaultArguments() 0 12 3
A setGenerateAs() 0 19 3
A __construct() 0 18 3
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Twig\Node;
12
13
use Twig\Node\Node;
14
use Twig\Node\Expression\AbstractExpression;
15
use Twig\Node\Expression\ArrayExpression;
16
use Twig\Node\Expression\FunctionExpression;
17
use Twig\Node\Expression\ConstantExpression;
18
use Twig\Error\SyntaxError;
19
20
/**
21
 * @author Yaroslav Honcharuk <[email protected]>
22
 */
23
class RouteExpression extends FunctionExpression
24
{
25
    /**
26
     * @param Node $arguments
27
     * @param int  $line
28
     *
29
     * @throws SyntaxError
30
     */
31 50
    public function __construct(Node $arguments, $line = 0)
32
    {
33 50
        if ($arguments->count() < 1) {
34 2
            throw new SyntaxError('At least one argument (name) is required.', $line);
35
        }
36
37 48
        if ($arguments->count() > 3) {
38 1
            throw new SyntaxError('Unrecognized extra arguments, only 3 (name, parameters, method) allowed.', $line);
39
        }
40
41 47
        $this->addDefaultArguments($arguments);
42
43 47
        parent::__construct('route_guard_route', $arguments, $line);
44
45
        // Set default generateAs parameters.
46
        // We call this function after parent constructor call to allow it to rely on internal structure
47
        // created by parent constructor (i.e., call $this->getNode('arguments'), $this->getTemplateLine()).
48 47
        $this->setGenerateAs('path', false);
49 47
    }
50
51
    /**
52
     * Note that $relative can be an instance of AbstractExpression, and it's execution result can be non-calculable at compile time.
53
     *
54
     * @param string                  $referenceType
55
     * @param bool|AbstractExpression $relative
56
     *
57
     * @return self
58
     *
59
     * @throws SyntaxError
60
     */
61 47
    public function setGenerateAs($referenceType, $relative = false)
62
    {
63 47
        if (!in_array($referenceType, ['url', 'path'], true)) {
64 1
            throw new SyntaxError(sprintf('Invalid reference type: %s', $referenceType), $this->getTemplateLine());
65
        }
66
67 47
        $referenceType = new ConstantExpression($referenceType, $this->getTemplateLine());
68
69 47
        if (!($relative instanceof AbstractExpression)) {
70 47
            $relative = new ConstantExpression($relative, $this->getTemplateLine());
71
        }
72
73 47
        $argument = new ArrayExpression([], $this->getTemplateLine());
74 47
        $argument->addElement($referenceType);
75 47
        $argument->addElement($relative);
76
77 47
        $this->getNode('arguments')->setNode(3, $argument);
78
79 47
        return $this;
80
    }
81
82
    /**
83
     * @param Node $arguments
84
     */
85 47
    private function addDefaultArguments(Node $arguments)
86
    {
87 47
        $line = $arguments->getTemplateLine();
88
89 47
        if (1 === $arguments->count()) {
90
            // add a default "parameters" argument
91 24
            $arguments->setNode(1, new ArrayExpression([], $line));
92
        }
93
94 47
        if (2 === $arguments->count()) {
95
            // add a default "method" argument
96 27
            $arguments->setNode(2, new ConstantExpression('GET', $line));
97
        }
98 47
    }
99
}
100