Completed
Push — master ( b86bbe...5e765d )
by wen
02:51
created

ElementFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 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
4
namespace Sco\Admin\Elements;
5
6
use Illuminate\Foundation\Application;
7
use Sco\Admin\Contracts\ElementFactory as FieldFactoryContract;
8
use Sco\Admin\Exceptions\BadMethodCallException;
9
10
/**
11
 * Class ElementFactory
12
 *
13
 *
14
 * @method Text text($name, $title)
15
 * @method Select select($name, $title)
16
 */
17
class ElementFactory implements FieldFactoryContract
18
{
19
    protected $app;
20
21
    protected $aliases = [
22
        'text' => Text::class,
23
    ];
24
25
    public function __construct(Application $app)
26
    {
27
        $this->app = $app;
28
    }
29
30
    public function getAlias($key)
31
    {
32
        return $this->aliases[$key];
33
    }
34
35
    /**
36
     * Check if alias is registered.
37
     *
38
     * @param string $alias
39
     *
40
     * @return bool
41
     */
42
    public function hasAlias($alias)
43
    {
44
        return array_key_exists($alias, $this->aliases);
45
    }
46
47
    /**
48
     * @param string $alias
49
     * @param array $arguments
50
     *
51
     * @return object
52
     */
53
    public function makeClass($alias, array $arguments)
54
    {
55
        $reflection = new \ReflectionClass($this->getAlias($alias));
56
57
        return $reflection->newInstanceArgs($arguments);
58
    }
59
60
    public function __call($method, $parameters)
61
    {
62
        if (!$this->hasAlias($method)) {
63
            throw new BadMethodCallException("Not Found {$method} Element");
64
        }
65
66
        return $this->makeClass($method, $parameters);
67
    }
68
}
69