Bindable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 10 2
A hasMethod() 0 4 1
A __call() 0 8 2
A getBind() 0 4 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