Completed
Push — master ( 547cab...942530 )
by Nikola
02:51
created

TemplateBuffer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 63
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 10 2
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
use RunOpenCode\Twig\BufferizedTemplate\Exception\LogicException;
13
14
/**
15
 * Class TemplateBuffer
16
 *
17
 * Template buffer is pointer to callable that wraps portion of compiled bufferized Twig template.
18
 *
19
 * @package RunOpenCode\Twig\BufferizedTemplate\Buffer
20
 *
21
 * {@internal}
22
 */
23
final class TemplateBuffer
24
{
25
    /**
26
     * @var callable
27
     */
28
    private $callable;
29
30
    /**
31
     * @var int
32
     */
33
    private $priority;
34
35
    /**
36
     * @var string
37
     */
38
    private $output;
39
40 5
    public function __construct(callable $callable, $priority = 0)
41
    {
42 5
        $this->callable = $callable;
43 5
        $this->priority = $priority;
44 5
    }
45
46
    /**
47
     * Execute template.
48
     *
49
     * @return TemplateBuffer $this
50
     */
51 4
    public function execute()
52
    {
53 4
        if (null === $this->output) {
54 4
            ob_start();
55 4
            call_user_func($this->callable);
56 4
            $this->output = ob_get_clean();
57
        }
58
59 4
        return $this;
60
    }
61
62
    /**
63
     * Get execution priority.
64
     *
65
     * @return int
66
     */
67 4
    public function getPriority()
68
    {
69 4
        return $this->priority;
70
    }
71
72
    /**
73
     * Get template buffer output.
74
     *
75
     * @return string
76
     */
77 5
    public function getOutput()
78
    {
79 5
        if (null === $this->output) {
80 1
            throw new LogicException('TemplateBuffer output can not be acquired prior to its execution.');
81
        }
82
83 4
        return $this->output;
84
    }
85
}
86