DeferReference::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 7
crap 1
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\Twig\Node;
3
4
use Twig_Compiler;
5
use Twig_Node_BlockReference;
6
7
/**
8
 * Represents a defer injection call node.
9
 *
10
 * @author Warnar Boekkooi <[email protected]>
11
 */
12
class DeferReference extends Twig_Node_BlockReference
13
{
14
    /**
15
     * @param string $name
16
     * @param string|false $variable
17
     * @param boolean $unique
18
     * @param string $reference
19
     * @param int|null $offset
20
     * @param integer $lineno The line number
21
     * @param string $tag The tag name associated with the Node
22
     */
23 1
    public function __construct($name, $variable, $unique, $reference, $offset, $lineno, $tag = null)
24
    {
25 1
        parent::__construct($name, $lineno, $tag);
26
27 1
        $this->setAttribute('reference', $reference);
28 1
        $this->setAttribute('unique', $unique);
29 1
        $this->setAttribute('variable', $variable);
30 1
        $this->setAttribute('offset', $offset);
31 1
    }
32
33
    /**
34
     * Compiles the node to PHP.
35
     *
36
     * @param Twig_Compiler $compiler A Twig_Compiler instance
37
     */
38 3
    public function compile(Twig_Compiler $compiler)
39
    {
40 3
        $name = $this->getAttribute('name');
41 3
        $reference = $this->getAttribute('reference');
42 3
        $variable = $this->getAttribute('variable');
43 3
        $offset = $this->getAttribute('offset');
44 3
        $offset = ($offset === null || $offset === false || strval(intval($offset)) != $offset ? 'null' : $offset);
45
46 3
        if ($variable) {
47
            $compiler
48 1
                ->addDebugInfo($this)
49 1
                ->write("if (!\$this->env->getExtension('defer')->contains('{$reference}', '$name|' . \$context['{$variable}'])) {\n")
50 1
                ->indent()
51 1
                    ->write("\$this->env->getExtension('defer')->cache('{$reference}', \$this->renderBlock('{$name}', \$context, \$blocks), '$name|' . \$context['{$variable}'], {$offset});\n")
52 1
                ->outdent()
53 1
                ->write("}\n")
54
            ;
55
56 1
            return;
57
        }
58 2
        if ($this->getAttribute('unique')) {
59
            $compiler
60 1
                ->addDebugInfo($this)
61 1
                ->write("if (!\$this->env->getExtension('defer')->contains('{$reference}', '{$name}')) {\n")
62 1
                ->indent()
63 1
                    ->write("\$this->env->getExtension('defer')->cache('{$reference}', \$this->renderBlock('{$name}', \$context, \$blocks), '{$name}', {$offset});\n")
64 1
                ->outdent()
65 1
                ->write("}\n")
66
            ;
67
68 1
            return;
69
        }
70
        $compiler
71 1
            ->addDebugInfo($this)
72 1
            ->write("\$this->env->getExtension('defer')->cache('{$reference}', \$this->renderBlock('{$name}', \$context, \$blocks), null, {$offset});\n")
73
        ;
74 1
    }
75
}
76