Factory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 3 1
A getDialectClass() 0 16 4
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