Passed
Push — master ( 6a8589...f12964 )
by DeGracia
01:25
created

Manager::driver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
nc 2
cc 2
nop 1
crap 2
1
<?php
2
3
namespace DeGraciaMathieu\Manager;
4
5
use InvalidArgumentException;
6
7
abstract class Manager
8
{
9
    /**
10
     * Get the default driver name.
11
     *
12
     * @return string
13
     */
14
    abstract public function getDefaultDriver();
15
16
    /**
17
     * Dynamically call the default driver instance.
18
     *
19
     * @param  string  $method
20
     * @param  array  $parameters
21
     * @return mixed
22
     */
23 1
    public function __call($method, $parameters)
24
    {
25 1
        return $this->driver()->$method(...$parameters);
26
    }
27
28
    /**
29
     * Get a driver instance.
30
     *
31
     * @param  string  $name
32
     * @return mixed
33
     *
34
     * @throws \InvalidArgumentException
35
     */
36 2
    public function driver($name = null)
37
    {
38 2
        $name = $name ?: $this->getDefaultDriver();
39
40 2
        $driver = $this->load($name);
41
42 1
        return $driver;
43
    }
44
45
    /**
46
     * Load a new driver instance.
47
     *
48
     * @param  string  $name
49
     * @throws \InvalidArgumentException
50
     * 
51
     * @return mixed
52
     */
53 2
    protected function load(string $name)
54
    {
55 2
        $method = 'create' . ucfirst(strtolower($name)) . 'Driver';
56
57 2
        if (! method_exists($this, $method)) {
58 1
            throw new InvalidArgumentException('Driver [' . $name . '] not supported.');
59
        }
60
61 1
        return $this->$method();
62
    }
63
}
64