CallableComplexFileBasedTemplateManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 14 2
A registerCallable() 0 8 1
A callableIsRegistered() 0 4 1
A getCallable() 0 4 1
A validateCallableOrThrowInvalidArgumentException() 0 18 3
A validateStringOrThrowInvalidArgumentException() 0 10 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-10-28
5
 */
6
namespace Net\Bazzline\Component\Template;
7
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
class CallableComplexFileBasedTemplateManager extends ComplexFileBasedTemplate
12
{
13
    /** @var array|callable[] */
14
    private $nameToCallableCollection = array();
15
16
    /**
17
     * @param string $name
18
     * @param array $arguments
19
     * @return mixed
20
     */
21
    public function __call($name, array $arguments)
22
    {
23
        $callableIsNotRegistered = (!$this->callableIsRegistered($name));
24
25
        if ($callableIsNotRegistered) {
26
            throw new RuntimeException(
27
                'no callable found for name "' . $name . '"'
28
            );
29
        }
30
31
        $callable = $this->getCallable($name);
32
33
        return call_user_func_array($callable, $arguments);
34
    }
35
36
    /**
37
     * @param string $name
38
     * @param callable $callable
39
     * @return $this
40
     * @throws InvalidArgumentException
41
     */
42
    public function registerCallable($name, $callable)
43
    {
44
        $this->validateStringOrThrowInvalidArgumentException($name, 'name');
45
        $this->validateCallableOrThrowInvalidArgumentException($callable, $name);
46
        $this->nameToCallableCollection[$name] = $callable;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $name
53
     * @return bool
54
     */
55
    private function callableIsRegistered($name)
56
    {
57
        return (isset($this->nameToCallableCollection[$name]));
58
    }
59
60
    /**
61
     * @param string $name
62
     * @return callable
63
     */
64
    private function getCallable($name)
65
    {
66
        return $this->nameToCallableCollection[$name];
67
    }
68
69
    /**
70
     * @param callable $callable
71
     * @param string $name
72
     * @throws InvalidArgumentException
73
     */
74
    private function validateCallableOrThrowInvalidArgumentException($callable, $name)
75
    {
76
        $isNotCallable = (!is_callable($callable));
77
78
        if ($isNotCallable) {
79
            throw new InvalidArgumentException(
80
                'callable must be callable'
81
            );
82
        }
83
84
        $callableAlreadyRegistered = $this->callableIsRegistered($name);
85
86
        if ($callableAlreadyRegistered) {
87
            throw new InvalidArgumentException(
88
                'a callable with the name "' . $name . '" is already registered'
89
            );
90
        }
91
    }
92
93
    /**
94
     * @param string $string
95
     * @param string $name
96
     * @throws InvalidArgumentException
97
     */
98
    private function validateStringOrThrowInvalidArgumentException($string, $name)
0 ignored issues
show
Unused Code introduced by
The parameter $string is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        $nameIsNotAString = (!is_string($name));
101
102
        if ($nameIsNotAString) {
103
            throw new InvalidArgumentException(
104
                $name . ' must be a valid string'
105
            );
106
        }
107
    }
108
}
109