Completed
Pull Request — master (#3)
by Joao
06:17 queued 02:28
created

Factory::getKeyValueInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * User: jg
4
 * Date: 13/02/17
5
 * Time: 15:48
6
 */
7
8
namespace ByJG\AnyDataset;
9
10
use ByJG\AnyDataset\Store\PdoLiteral;
11
use ByJG\Util\Uri;
12
13
class Factory
14
{
15
    /**
16
     * @param $connectionString
17
     * @param $schemesAlternative
18
     * @return \ByJG\AnyDataset\DbDriverInterface
19
     */
20 28
    public static function getDbRelationalInstance($connectionString, $schemesAlternative = null)
21
    {
22 28
        $prefix = '\\ByJG\\AnyDataset\\Store\\';
23
24 28
        $instance = self::getInstance(
25 28
            $connectionString,
26 28
            array_merge(
27
                [
28 28
                    "oci8" => $prefix . "DbOci8Driver",
29 28
                    "dblib" => $prefix . "PdoDblib",
30 28
                    "mysql" => $prefix . "PdoMysql",
31 28
                    "pgsql" => $prefix . "PdoPgsql",
32 28
                    "oci" => $prefix . "PdoOci",
33 28
                    "odbc" => $prefix . "PdoOdbc",
34 28
                    "sqlite" => $prefix . "PdoSqlite",
35 28
                ],
36
                (array)$schemesAlternative
37 28
            ),
38
            DbDriverInterface::class
39 28
        );
40
41 28
        return $instance;
42
    }
43
44
    /**
45
     * @param $connectionString
46
     * @param $schemesAlternative
47
     * @return NoSqlInterface
48
     */
49
    public static function getNoSqlInstance($connectionString, $schemesAlternative = null)
50
    {
51
        $prefix = '\\ByJG\\AnyDataset\\Store\\';
52
53
        $instance = self::getInstance(
54
            $connectionString,
55
            array_merge(
56
                [
57
                    "mongodb" => $prefix . "MongoDbDriver",
58
                ],
59
                (array)$schemesAlternative
60
            ),
61
            NoSqlInterface::class
62
        );
63
64
        return $instance;
65
    }
66
67
    /**
68
     * @param string $connectionString
69
     * @param array $schemesAlternative
70
     * @return KeyValueInterface
71
     */
72
    public static function getKeyValueInstance($connectionString, $schemesAlternative = null)
73
    {
74
        $prefix = '\\ByJG\\AnyDataset\\Store\\';
75
76
        $instance = self::getInstance(
77
            $connectionString,
78
            array_merge(
79
                [
80
                    "s3" => $prefix . "AwsS3Driver",
81
                ],
82
                (array)$schemesAlternative
83
            ),
84
            KeyValueInterface::class
85
        );
86
87
        return $instance;
88
    }
89
90 28
    protected static function getInstance($connectionString, $validSchemes, $typeOf)
91
    {
92 28
        $connectionUri = new Uri($connectionString);
93
94 28
        $scheme = $connectionUri->getScheme();
95
96 28
        $class = isset($validSchemes[$scheme]) ? $validSchemes[$scheme] : PdoLiteral::class;
97
98 28
        $instance = new $class($connectionUri);
99
100 28
        if (!is_a($instance, $typeOf)) {
101
            throw new \InvalidArgumentException(
102
                "The class '$typeOf' is not a instance of DbDriverInterface"
103
            );
104
        }
105
106 28
        return $instance;
107
    }
108
109
    /**
110
     * Get a IDbFunctions class to execute Database specific operations.
111
     *
112
     * @param \ByJG\Util\Uri $connectionUri
113
     * @return \ByJG\AnyDataset\DbFunctionsInterface
114
     */
115 5
    public static function getDbFunctions(Uri $connectionUri)
116
    {
117
        $dbFunc = "\\ByJG\\AnyDataset\\Store\\Helpers\\Db"
118 5
            . ucfirst($connectionUri->getScheme())
119 5
            . "Functions";
120 5
        return new $dbFunc();
121
    }
122
}
123