RenderContext::__get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\RenderContext;
4
5
use League\Plates;
6
use BadMethodCallException;
7
8
final class RenderContext
9
{
10
    private $render;
11
    private $ref;
12
    private $func_stack;
13
14 24
    public function __construct(
15
        Plates\RenderTemplate $render,
16
        Plates\TemplateReference $ref,
17
        $func_stack = null
18
    ) {
19 24
        $this->render = $render;
20 24
        $this->ref = $ref;
21 24
        $this->func_stack = $func_stack ?: Plates\Util\stack([platesFunc()]);
22 24
    }
23
24
    public function __get($name) {
25
        if (!$this->func_stack) {
26
            throw new BadMethodCallException('Cannot access ' . $name . ' because no func stack has been setup.');
27
        }
28
29
        return $this->invokeFuncStack($name, []);
30
    }
31
32
    public function __set($name, $value) {
33
        throw new BadMethodCallException('Cannot set ' . $name . ' on this render context.');
34
    }
35
36 16
    public function __call($name, array $args) {
37 16
        if (!$this->func_stack) {
38
            throw new BadMethodCallException('Cannot call ' . $name . ' because no func stack has been setup.');
39
        }
40
41 16
        return $this->invokeFuncStack($name, $args);
42
    }
43
44 4
    public function __invoke(...$args) {
45 4
        if (!$this->func_stack) {
46
            throw new BadMethodCallException('Cannot invoke the render context because no func stack has been setup.');
47
        }
48
49 4
        return $this->invokeFuncStack('__invoke', $args);
50
    }
51
52 16
    private function invokeFuncStack($name, array $args) {
53 16
        return ($this->func_stack)(new FuncArgs(
54 16
            $this->render,
55 16
            $this->ref,
56 16
            $name,
57 16
            $args
58
        ));
59
    }
60
61
    public static function factory(callable $create_render, $func_stack = null) {
62 24
        return function(Plates\TemplateReference $ref) use ($create_render, $func_stack) {
63 24
            return new self(
64 24
                $create_render(),
65 24
                $ref,
66 24
                $func_stack
67
            );
68 24
        };
69
    }
70
}
71