Factory::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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