Completed
Pull Request — master (#41)
by Emanuele
05:27
created

FeatureNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 6
crap 1
1
<?php
2
3
namespace Ae\FeatureBundle\Twig\Node;
4
5
use Ae\FeatureBundle\Twig\Extension\FeatureExtension;
6
use Twig_Environment;
7
use Twig_Node;
8
use Twig_Node_Expression_Array;
9
use Twig_Node_Expression_Constant;
10
use Twig_Node_Expression_ExtensionReference;
11
use Twig_Node_Expression_MethodCall;
12
use Twig_Node_If;
13
14
/**
15
 * @author Carlo Forghieri <[email protected]>
16
 */
17
class FeatureNode extends Twig_Node_If
18
{
19 3
    public function __construct($name, $parent, $body, $else, $lineno, $tag = null)
20
    {
21 3
        $tests = new Twig_Node([
22 3
            $this->createExpression($name, $parent, $lineno),
23 3
            $body,
24
        ]);
25
26 3
        parent::__construct($tests, $else, $lineno, $tag);
27 3
    }
28
29 3
    protected function createExpression($name, $parent, $lineno)
30
    {
31 3
        return new Twig_Node_Expression_MethodCall(
32 3
            new Twig_Node_Expression_ExtensionReference(
33 3
                version_compare(Twig_Environment::VERSION, '1.26.0', '>=')
34
                    ? FeatureExtension::class
35 3
                    : 'feature',
36
                $lineno
37
            ),
38 3
            'isGranted',
39 3
            new Twig_Node_Expression_Array(
40
                [
41 3
                    new Twig_Node_Expression_Constant('name', $lineno),
42 3
                    new Twig_Node_Expression_Constant($name, $lineno),
43 3
                    new Twig_Node_Expression_Constant('parent', $lineno),
44 3
                    new Twig_Node_Expression_Constant($parent, $lineno),
45
                ],
46
                $lineno
47
            ),
48
            $lineno
49
        );
50
    }
51
}
52