Factory::buildClassName()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8.3844

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
rs 8.4444
cc 8
nc 8
nop 3
crap 8.3844
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EngineWorks\DBAL;
6
7
use LogicException;
8
use Psr\Log\LoggerInterface;
9
10
class Factory
11
{
12
    /** @var string */
13
    private $namespace;
14
15
    /** @var string */
16
    private $dbalName;
17
18
    /** @var string */
19
    private $settingsName;
20
21
    /**
22
     * @param string $namespace in example EngineWorks\DBAL\Mysqli
23
     * @param string $dbalName
24
     * @param string $settingsName
25
     */
26 392
    public function __construct(string $namespace, string $dbalName = 'DBAL', string $settingsName = 'Settings')
27
    {
28 392
        $this->namespace = $namespace;
29 392
        $this->dbalName = $dbalName;
30 392
        $this->settingsName = $settingsName;
31
    }
32
33
    /**
34
     * Return a valid class name (namespace + class),
35
     * optionally checks if the class extends or implements other classes
36
     *
37
     * @param string $class
38
     * @param string $extends
39
     * @param string $implements
40
     * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
41
     */
42 392
    protected function buildClassName(string $class, string $extends = '', string $implements = ''): string
43
    {
44 392
        $classname = $this->namespace . '\\' . $class;
45 392
        if (! class_exists($classname)) {
46 2
            throw new LogicException("Class $classname does not exists");
47
        }
48 390
        if ('' !== $extends) {
49
            if (! in_array($extends, class_parents($classname) ?: [])) {
50
                throw new LogicException("Class $classname does not extends $extends");
51
            }
52
        }
53 390
        if ('' !== $implements) {
54 390
            if (! in_array($implements, class_implements($classname) ?: [])) {
55 2
                throw new LogicException("Class $classname does not implements $implements");
56
            }
57
        }
58 388
        return $classname;
59
    }
60
61
    /**
62
     * @param Settings $settings
63
     * @param LoggerInterface|null $logger
64
     * @return DBAL
65
     */
66 390
    public function dbal(Settings $settings, LoggerInterface $logger = null): DBAL
67
    {
68 390
        $classname = $this->buildClassName($this->dbalName, '', DBAL::class);
69 388
        $dbal = new $classname($settings, $logger);
70 388
        if (! $dbal instanceof DBAL) {
71
            throw new LogicException(sprintf('The object with class %s was created but is not a DBAL', $classname));
72
        }
73 388
        return $dbal;
74
    }
75
76
    /**
77
     * @param mixed[] $settings
78
     * @return Settings
79
     */
80 390
    public function settings(array $settings = []): Settings
81
    {
82 390
        $classname = $this->buildClassName($this->settingsName, '', Settings::class);
83 388
        $settings = new $classname($settings);
84 388
        if (! $settings instanceof Settings) {
85
            throw new LogicException(sprintf('The object with class %s was created but is not a Settings', $classname));
86
        }
87 388
        return $settings;
88
    }
89
}
90