Completed
Push — master ( 69416a...547cab )
by Nikola
03:28
created

TemplateBuffer::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
crap 3.0175
1
<?php
2
/*
3
 * This file is part of the Twig Bufferized Template package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Twig\BufferizedTemplate\Buffer;
11
12
/**
13
 * Class TemplateBuffer
14
 *
15
 * Template buffer is pointer to callable that wraps portion of compiled bufferized Twig template.
16
 *
17
 * @package RunOpenCode\Twig\BufferizedTemplate\Buffer
18
 */
19
final class TemplateBuffer
20
{
21
    /**
22
     * @var callable
23
     */
24
    private $callable;
25
26
    /**
27
     * @var int
28
     */
29
    private $priority;
30
31
    /**
32
     * @var string
33
     */
34
    private $output;
35
36 2
    public function __construct(callable $callable, $priority = 0)
37
    {
38 2
        $this->callable = $callable;
39 2
        $this->priority = $priority;
40 2
        $this->output = null;
41 2
    }
42
43
    /**
44
     * Execute template.
45
     *
46
     * @return TemplateBuffer $this
47
     */
48 2
    public function execute()
49
    {
50 2
        if (is_null($this->output)) {
51
52 2
            ob_start();
53 2
            call_user_func_array($this->callable, array());
54 2
            $this->output = ob_get_clean();
55
56 2
            if (is_null($this->output)) {
57
                $this->output = '';
58
            }
59
        }
60
61 2
        return $this;
62
    }
63
64
    /**
65
     * Get execution priority.
66
     *
67
     * @return int
68
     */
69 2
    public function getPriority()
70
    {
71 2
        return $this->priority;
72
    }
73
74
    /**
75
     * Get template buffer output.
76
     *
77
     * @return string
78
     */
79 2
    public function getOutput()
80
    {
81 2
        if (is_null($this->output)) {
82
            throw new \LogicException('TemplateBuffer output can not be acquired prior to its execution.');
83
        }
84
85 2
        return $this->output;
86
    }
87
}
88