Passed
Push — master ( e07c0b...0963ba )
by Iman
02:31
created

ConditionsFacade::alias()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 4
c 1
b 1
f 0
nc 2
nop 3
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
1
<?php
2
3
namespace Imanghafoori\HeyMan\Conditions;
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 exists as a Heyman condition');
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 2
            return true;
44
        }
45
46
        return false;
47
    }
48
}
49