Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

RouteExpression   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addDefaultArguments() 0 12 3
A setGenerateAs() 0 19 3
A __construct() 0 20 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 33
    public function __construct(Node $arguments, $line = 0)
32
    {
33
        // TODO: validate arguments types?
34
35 33
        if ($arguments->count() < 1) {
36 2
            throw new SyntaxError('At least one argument (name) is required.', $line);
37
        }
38
39 31
        if ($arguments->count() > 3) {
40 1
            throw new SyntaxError('Unrecognized extra arguments, only 3 (name, parameters, method) allowed.', $line);
41
        }
42
43 30
        $this->addDefaultArguments($arguments);
44
45 30
        parent::__construct('route_guard_route', $arguments, $line);
46
47
        // Set default generateAs parameters.
48
        // We call this function after parent constructor call to allow it to rely on internal structure
49
        // created by parent constructor (i.e., call $this->getNode('arguments'), $this->getTemplateLine()).
50 30
        $this->setGenerateAs('path', false);
51 30
    }
52
53
    /**
54
     * TODO: allow to specify $referenceType as a constant (one of UrlGeneratorInterface constants).
55
     * Note that $relative can be an instance of AbstractExpression, and it's execution result can be non-calculable at compile time.
56
     *
57
     * @param string                  $referenceType
58
     * @param bool|AbstractExpression $relative
59
     *
60
     * @return self
61
     *
62
     * @throws SyntaxError
63
     */
64 30
    public function setGenerateAs($referenceType, $relative = false)
65
    {
66 30
        if (!in_array($referenceType, ['url', 'path'], true)) {
67 1
            throw new SyntaxError(sprintf('Invalid reference type: %s', $referenceType), $this->getTemplateLine());
68
        }
69
70 30
        $referenceType = new ConstantExpression($referenceType, $this->getTemplateLine());
71
72 30
        if (!($relative instanceof AbstractExpression)) {
73 30
            $relative = new ConstantExpression($relative, $this->getTemplateLine());
74
        }
75
76 30
        $argument = new ArrayExpression([], $this->getTemplateLine());
77 30
        $argument->addElement($referenceType);
78 30
        $argument->addElement($relative);
79
80 30
        $this->getNode('arguments')->setNode(3, $argument);
81
82 30
        return $this;
83
    }
84
85
    /**
86
     * @param Node $arguments
87
     */
88 30
    private function addDefaultArguments(Node $arguments)
89
    {
90 30
        $line = $arguments->getTemplateLine();
91
92 30
        if (1 === $arguments->count()) {
93
            // add a default "parameters" argument
94 24
            $arguments->setNode(1, new ArrayExpression([], $line));
95
        }
96
97 30
        if (2 === $arguments->count()) {
98
            // add a default "method" argument
99 27
            $arguments->setNode(2, new ConstantExpression('GET', $line));
100
        }
101 30
    }
102
}
103