Completed
Push — master ( c45904...04afc3 )
by Iman
03:46
created

ConditionsFacade::_call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

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