Total Complexity | 7 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
5 | class AdapterFactory |
||
6 | { |
||
7 | protected static $instance; |
||
8 | |||
9 | protected $adapters = [ |
||
10 | 'mysql' => 'devtoolboxuk\storage\Adapter\DoctrineAdapter' |
||
11 | ]; |
||
12 | |||
13 | public static function instance() |
||
14 | { |
||
15 | if (!static::$instance) { |
||
16 | static::$instance = new static(); |
||
17 | } |
||
18 | |||
19 | return static::$instance; |
||
20 | } |
||
21 | |||
22 | public function registerAdapter($name, $class) |
||
23 | { |
||
24 | if (!is_subclass_of($class, 'devtoolboxuk\storage\AdapterInterface')) { |
||
25 | throw new \RuntimeException(sprintf( |
||
26 | 'Adapter class "%s" must implement devtoolboxuk\\storage\\Adapter\\AdapterInterface', |
||
27 | $class |
||
28 | )); |
||
29 | } |
||
30 | $this->adapters[$name] = $class; |
||
31 | |||
32 | return $this; |
||
33 | } |
||
34 | |||
35 | public function getAdapter($name, array $options) |
||
36 | { |
||
37 | $class = $this->getClass($name); |
||
38 | return new $class($options); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get an adapter class by name. |
||
43 | * |
||
44 | * @throws \RuntimeException |
||
45 | * @param string $name |
||
46 | * @return string |
||
47 | */ |
||
48 | protected function getClass($name) |
||
58 | } |
||
59 | } |
||
60 |