Passed
Push — master ( be0263...ea91dc )
by Evgenii
12:20
created

Renderable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 19
dl 0
loc 71
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 8 1
A capture() 0 15 2
A template() 0 12 3
1
<?php
2
3
namespace Helick\Blocks\Traits;
4
5
use Exception;
6
use Helick\Blocks\Exceptions\BlockException;
7
8
trait Renderable
9
{
10
    /**
11
     * Captures the output that is generated when a template is included.
12
     * This property is static to prevent object scope resolution.
13
     *
14
     * @param string $template
15
     * @param array  $data
16
     *
17
     * @return string
18
     *
19
     * @throws Exception
20
     */
21
    protected static function capture(string $template, array $data): string
22
    {
23
        extract($data, EXTR_SKIP);
24
25
        ob_start();
26
27
        try {
28
            include $template;
29
        } catch (Exception $e) {
30
            ob_end_clean();
31
32
            throw $e;
33
        }
34
35
        return ob_get_clean();
36
    }
37
38
    /**
39
     * Render the block.
40
     *
41
     * [!!] Global variables with the same key name as local variables will be
42
     * overwritten by the local variable.
43
     *
44
     * @param array $fields
45
     * @param array $attributes
46
     * @param array $blocks
47
     *
48
     * @return void
49
     *
50
     * @throws Exception
51
     */
52
    public function render(array $fields, array $attributes, array $blocks): void
53
    {
54
        $globalVariables = compact('fields', 'attributes', 'blocks');
55
        $localVariables  = $this->with($fields);
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        /** @scrutinizer ignore-call */ 
56
        $localVariables  = $this->with($fields);
Loading history...
56
57
        $data = array_merge($globalVariables, $localVariables);
58
59
        echo static::capture($this->template(), $data);
60
    }
61
62
    /**
63
     * Get the block's template.
64
     *
65
     * @return string
66
     */
67
    protected function template(): string
68
    {
69
        if (empty($this->template)) {
70
            throw BlockException::forEmptyTemplate();
71
        }
72
73
        $template = locate_template($this->template);
0 ignored issues
show
Bug introduced by
The function locate_template was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $template = /** @scrutinizer ignore-call */ locate_template($this->template);
Loading history...
74
        if ($template === '') {
75
            throw BlockException::forNotFoundTemplate($this->template);
76
        }
77
78
        return $template;
79
    }
80
}
81