DriverFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 29
ccs 8
cts 8
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 18 3
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Db\Driver;
12
13
/**
14
 * DriverFactory Class
15
 *
16
 * This class makes a database connection with a specified driver, i.e. creates the driver instance.
17
 */
18
class DriverFactory
19
{
20
    /**
21
     * Constructor
22
     *
23
     * @param array $connection_options
24
     *
25
     * @throws \RuntimeException
26
     * @return AbstractDriver
27
     *
28
     */
29 6
    public static function create($connection_options)
30
    {
31
        $drivers = [
32 6
            "Oci8"   => "Drone\Db\Driver\Oracle",
33
            "Mysqli" => "Drone\Db\Driver\MySQL",
34
            "Sqlsrv" => "Drone\Db\Driver\SQLServer",
35
        ];
36
37 6
        if (!array_key_exists('driver', $connection_options)) {
38 1
            throw new \RuntimeException("The database driver key has not been declared");
39
        }
40
41 5
        $drv = $connection_options["driver"];
42
43 5
        if (array_key_exists($drv, $drivers)) {
44 4
            return new $drivers[$drv]($connection_options);
45
        } else {
46 1
            throw new \RuntimeException("The database driver does not exists");
47
        }
48
    }
49
}
50