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

TemplateBuffer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 69
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A execute() 0 15 3
A getPriority() 0 4 1
A getOutput() 0 8 2
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