Completed
Push — master ( d64bdd...237512 )
by wen
13:09
created

AliasBinder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 63
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAliases() 0 8 2
A bind() 0 5 1
A getAliases() 0 4 1
A getAlias() 0 4 1
A hasAlias() 0 4 1
A makeClass() 0 6 1
A __call() 0 8 2
1
<?php
2
3
namespace Sco\Admin\Traits;
4
5
use BadMethodCallException;
6
7
trait AliasBinder
8
{
9
    protected $aliases = [];
10
11
    public function registerAliases(array $aliases)
12
    {
13
        foreach ($aliases as $alias => $class) {
14
            $this->bind($alias, $class);
15
        }
16
17
        return $this;
18
    }
19
20
    public function bind($alias, $class)
21
    {
22
        $this->aliases[$alias] = $class;
23
        return $this;
24
    }
25
26
    public function getAliases()
27
    {
28
        return $this->aliases;
29
    }
30
31
    public function getAlias($key)
32
    {
33
        return $this->aliases[$key];
34
    }
35
36
    /**
37
     * Check if alias is registered.
38
     *
39
     * @param string $alias
40
     *
41
     * @return bool
42
     */
43
    public function hasAlias($alias)
44
    {
45
        return array_key_exists($alias, $this->aliases);
46
    }
47
48
    /**
49
     * @param string $alias
50
     * @param array  $arguments
51
     *
52
     * @return object
53
     */
54
    public function makeClass($alias, array $arguments)
55
    {
56
        $reflection = new \ReflectionClass($this->getAlias($alias));
57
58
        return $reflection->newInstanceArgs($arguments);
59
    }
60
61
    public function __call($method, $parameters)
62
    {
63
        if (!$this->hasAlias($method)) {
64
            throw new BadMethodCallException("Not Found {$method}");
65
        }
66
67
        return $this->makeClass($method, $parameters);
68
    }
69
}
70