Passed
Push — master ( 2cd04b...24ffba )
by DeGracia
02:10
created

Manager::loadWithoutCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace DeGraciaMathieu\Manager;
4
5
use InvalidArgumentException;
6
7
abstract class Manager
8
{
9
    /**
10
     * @var boolean
11
     */
12
    protected $cached = false;
13
14
    /**
15
     * @var \DeGraciaMathieu\Manager\Aggregator
16
     */
17
    protected $aggregator;
18
19
    /**
20
     * Welcome
21
     */
22 3
    public function __construct()
23
    {
24 3
        $this->aggregator = new Aggregator();
25 3
    }
26
27
    /**
28
     * Get the default driver name.
29
     *
30
     * @return string
31
     */
32
    abstract public function getDefaultDriver();
33
34
    /**
35
     * Dynamically call the default driver instance.
36
     *
37
     * @param  string  $method
38
     * @param  array  $parameters
39
     * @return mixed
40
     */
41 1
    public function __call($method, $parameters)
42
    {
43 1
        return $this->driver()->$method(...$parameters);
44
    }
45
46
    /**
47
     * Get a driver instance.
48
     *
49
     * @param  string  $name
50
     * @return mixed
51
     *
52
     * @throws \InvalidArgumentException
53
     */
54 3
    public function driver($name = null)
55
    {
56 3
        $name = $name ?: $this->getDefaultDriver();
57
58 3
        $driver = $this->load($name);
59
60 2
        return $driver;
61
    }
62
63
    /**
64
     * Load a driver instance.
65
     *
66
     * @param  string  $name
67
     * 
68
     * @return mixed
69
     */
70 3
    protected function load(string $name)
71
    {
72 3
        if ($this->cached) {
73 1
            return $this->loadWithCache($name);
74
        }
75
76 2
        return $this->loadWithoutCache($name);
77
    }
78
79
    /**
80
     * Load a cached driver instance.
81
     * 
82
     * @param  string $name
83
     * 
84
     * @return mixed
85
     */
86 1
    protected function loadWithCache(string $name)
87
    {
88 1
        $alreadyLoad = $this->aggregator->has($name);
89
90 1
        if ($alreadyLoad) {
91 1
            return $this->aggregator->get($name);
92
        }
93
94 1
        $driver = $this->makeDriverInstance($name);
95
96 1
        $this->aggregator->set($name, $driver);
97
98 1
        return $driver;
99
    }
100
101
    /**
102
     * Load a driver instance.
103
     * 
104
     * @param  string $name
105
     * 
106
     * @return mixed
107
     */
108 2
    protected function loadWithoutCache(string $name)
109
    {
110 2
        return $this->makeDriverInstance($name);
111
    }    
112
113
    /**
114
     * Make a new driver instance.
115
     *
116
     * @param  string  $name
117
     * @throws \InvalidArgumentException
118
     * 
119
     * @return mixed
120
     */
121 3
    protected function makeDriverInstance(string $name)
122
    {
123 3
        $method = 'create' . ucfirst(strtolower($name)) . 'Driver';
124
125 3
        if (! method_exists($this, $method)) {
126 1
            throw new InvalidArgumentException('Driver [' . $name . '] not supported.');
127
        }
128
129 2
        return $this->$method();
130
    }
131
}
132