Bindable::hasMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Resilient\Traits;
4
5
use Closure;
6
use InvalidArgumentException;
7
use RunTimeException;
8
9
trait Bindable
10
{
11
    protected $binded;
12
13
    public function bind(string $name, callable $callable)
14
    {
15
        if (!is_callable($callable)) {
16
            throw new InvalidArgumentException('Second param must be callable');
17
        }
18
19
        $this->binded[$name] = Closure::bind($callable, $this, get_class());
20
21
        return $this;
22
    }
23
24
    public function hasMethod(string $name)
25
    {
26
        return isset($this->binded[$name]);
27
    }
28
29
    public function __call($name, array $args)
30
    {
31
        if ($this->hasMethod($name)) {
32
            return $this->getBind($name, $args);
33
        }
34
35
        throw new RunTimeException('There is no method with the given name to call');
36
    }
37
38
    public function getBind($name, $args)
39
    {
40
        return $this->binded[$name](...$args);
41
    }
42
}
43