Passed
Push — master ( 87e63f...d6a1bd )
by Darío
02:40
created

DriverFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 17
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.9
c 0
b 0
f 0
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
     * @return AbstractDriver
26
     *
27
     * @throws \RuntimeException
28
     */
29 5
    public static function create($connection_options)
30
    {
31
        $drivers = [
32 5
            "Oci8"   => "Drone\Db\Driver\Oracle",
33
            "Mysqli" => "Drone\Db\Driver\MySQL",
34
            "Sqlsrv" => "Drone\Db\Driver\SQLServer",
35
        ];
36
37 5
        if (!array_key_exists('driver', $connection_options))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
38
            throw new \RuntimeException("The database driver key has not been declared");
39
40 5
        $drv = $connection_options["driver"];
41
42 5
        if (array_key_exists($drv, $drivers))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
43 4
            return new $drivers[$drv]($connection_options);
44
        else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
45 1
            throw new \RuntimeException("The database driver does not exists");
46
    }
47
}