Completed
Push — master ( dbea24...30460b )
by wen
04:20
created

ColumnManager::driver()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 1
1
<?php
2
3
4
namespace Sco\Admin\Column;
5
6
use Sco\Admin\Exceptions\InvalidArgumentException;
7
8
class ColumnManager
9
{
10
    protected $app;
11
12
    protected $initialDrivers = [
13
        'el' => 'ElColumn',
14
    ];
15
16
    protected $drivers = [];
17
18
    public function __construct($app)
19
    {
20
        $this->app = $app;
21
    }
22
23
    public function make($option, $driver = null)
24
    {
25
        $driverName = $this->driver($driver);
26
        return new $driverName($option);
27
    }
28
29
    public function driver($driver = null)
30
    {
31
        $driver = $driver ?: $this->getDefaultDriver();
32
        if (!isset($this->drivers[$driver])) {
33
            $this->drivers[$driver] = $this->createDriver($driver);
34
        }
35
36
        return $this->drivers[$driver];
37
    }
38
39
    protected function getDefaultDriver()
40
    {
41
        return 'el';
42
    }
43
44
    protected function createDriver($driver)
45
    {
46
        if (isset($this->initialDrivers[$driver])) {
47
            return __NAMESPACE__ . '\\' . $this->initialDrivers[$driver];
48
        }
49
        throw new InvalidArgumentException("column driver({$driver}) not found");
50
    }
51
}
52