Factory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 52
ccs 15
cts 15
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabase() 0 20 5
A getInstance() 0 3 1
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database;
4
5
6
use kalanis\kw_mapper\Interfaces\IDriverSources;
7
use kalanis\kw_mapper\MapperException;
8
use ReflectionClass;
9
use ReflectionException;
10
11
12
/**
13
 * Class Factory
14
 * @package kalanis\kw_mapper\Storage\Database\Dialects
15
 */
16
class Factory
17
{
18
    /** @var array<string, string> */
19
    protected static array $map = [
20
        IDriverSources::TYPE_PDO_MYSQL => PDO\MySQL::class,
21
        IDriverSources::TYPE_PDO_MSSQL => PDO\MSSQL::class,
22
        IDriverSources::TYPE_PDO_MSSQL_T => PDO\MSSQLTrusting::class,
23
        IDriverSources::TYPE_PDO_ORACLE => PDO\Oracle::class,
24
        IDriverSources::TYPE_PDO_POSTGRES => PDO\PostgreSQL::class,
25
        IDriverSources::TYPE_PDO_SQLITE => PDO\SQLite::class,
26
        IDriverSources::TYPE_RAW_MYSQLI => Raw\MySQLi::class,
27
        IDriverSources::TYPE_RAW_ORACLE => Raw\Oracle::class,
28
        IDriverSources::TYPE_RAW_MONGO => Raw\MongoDb::class,
29
        IDriverSources::TYPE_RAW_LDAP => Raw\Ldap::class,
30
        IDriverSources::TYPE_RAW_WINREG => Raw\WinRegistry::class,
31
        IDriverSources::TYPE_RAW_WINREG2 => Raw\WinRegistry2::class,
32
        IDriverSources::TYPE_RAW_DBA => Raw\Dba::class,
33
    ];
34
35
    /** @var array<string, ADatabase> */
36
    protected static array $instances = [];
37
38 5
    public static function getInstance(): self
39
    {
40 5
        return new self();
41
    }
42
43
    /**
44
     * @param Config $config
45
     * @throws MapperException
46
     * @return ADatabase
47
     */
48 9
    public function getDatabase(Config $config): ADatabase
49
    {
50 9
        if (empty(static::$instances[$config->getDriver()])) {
51 6
            if (empty(static::$map[$config->getDriver()])) {
52 1
                throw new MapperException(sprintf('Wanted source *%s* not exists!', $config->getDriver()));
53
            }
54 5
            $path = static::$map[$config->getDriver()];
55
            try {
56
                /** @var class-string $path */
57 5
                $reflect = new ReflectionClass($path);
58 4
                $instance = $reflect->newInstance($config);
59 1
            } catch (ReflectionException $ex) {
60 1
                throw new MapperException($ex->getMessage(), $ex->getCode(), $ex);
61
            }
62 4
            if (!$instance instanceof ADatabase) {
63 1
                throw new MapperException(sprintf('Defined class *%s* is not instance of Storage\ADatabase!', $path));
64
            }
65 3
            static::$instances[$config->getDriver()] = $instance;
66
        }
67 6
        return static::$instances[$config->getDriver()];
68
    }
69
}
70