Factory::getDialectClass()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 5
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database\Dialects;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use ReflectionClass;
8
use ReflectionException;
9
10
11
/**
12
 * Class Factory
13
 * @package kalanis\kw_mapper\Storage\Database\Dialects
14
 */
15
class Factory
16
{
17
    /** @var array<string, ADialect> */
18
    protected static array $instances = [];
19
20 28
    public static function getInstance(): self
21
    {
22 28
        return new self();
23
    }
24
25
    /**
26
     * @param string $path
27
     * @throws MapperException
28
     * @return ADialect
29
     */
30 28
    public function getDialectClass(string $path): ADialect
31
    {
32 28
        if (!isset(static::$instances[$path])) {
33
            try {
34
                /** @var class-string $path */
35 5
                $reflect = new ReflectionClass($path);
36 4
                $instance = $reflect->newInstance();
37 1
            } catch (ReflectionException $ex) {
38 1
                throw new MapperException(sprintf('Wanted class *%s* not exists!', $path), $ex->getCode(), $ex);
39
            }
40 4
            if (!$instance instanceof ADialect) {
41 1
                throw new MapperException(sprintf('Defined class *%s* is not instance of AMapper!', $path));
42
            }
43 3
            static::$instances[$path] = $instance;
44
        }
45 26
        return static::$instances[$path];
46
    }
47
}
48