Completed
Push — master ( 17c0c5...99eb02 )
by Iman
05:05
created

ConditionsFacade::alias()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
namespace Imanghafoori\HeyMan\Conditions;
4
5
class ConditionsFacade
6
{
7
    private $methods = [];
8
9
    private $aliases = [];
10
11 103
    public function _call($method, $param)
12
    {
13 103
        $method = $this->aliases[$method] ?? $method;
14
15 103
        if (! isset($this->methods[$method])) {
16
            throw new \BadMethodCallException($method.' does not exists as a Heyman condition');
17
        }
18 103
        $condition = $this->methods[$method];
19
20 103
        if (is_callable($condition)) {
21 1
            return $condition(...$param);
22
        }
23
24 102
        [$class, $method] = explode('@', $condition);
25
26 102
        return call_user_func_array([new $class, $method], $param);
27
    }
28
29 113
    public function define($methodName, $callable)
30
    {
31 113
        if (is_callable($callable) || (is_string($callable) and mb_strpos($callable, '@'))) {
32 113
            $this->methods[$methodName] = $callable;
33
        } else {
34
            throw new \InvalidArgumentException("$callable should be string Class@method or a php callable");
35
        }
36 113
    }
37
38 2
    public function alias(string $currentName, string $newName)
39
    {
40 2
        if (isset($this->methods[$currentName]) && ! isset($this->aliases[$newName])) {
41 2
            $this->aliases[$newName] = $currentName;
42
        }
43 2
    }
44
}
45