RenderContext   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 23
cts 31
cp 0.7419
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A __get() 0 7 2
A __set() 0 3 1
A __call() 0 7 2
A __invoke() 0 7 2
A invokeFuncStack() 0 8 1
A factory() 0 9 1
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