Passed
Push — master ( 265dd0...621dba )
by Alexander
09:31
created

DatabaseFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 32
ccs 6
cts 11
cp 0.5455
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A __construct() 0 3 1
A createClass() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Factory;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Factory;
9
10
final class DatabaseFactory
11
{
12
    private static ?Factory $factory = null;
13
14
    public function __construct(Factory $factory)
15
    {
16
        $this->factory = $factory;
17
    }
18
19 2060
    public static function initialize(ContainerInterface $container = null, array $definitions = []): void
20
    {
21 2060
        self::$factory = new Factory($container, $definitions);
22 2060
    }
23
24
    /**
25
     * Creates a Class defined by config passed.
26
     *
27
     * @param array $config parameters for creating a class.
28
     *
29
     * @throws \RuntimeException if factory was not initialized
30
     *
31
     * @return object
32
     */
33 19
    public static function createClass(array $config): object
34
    {
35 19
        if (self::$factory === null) {
36
            throw new \RuntimeException(
37
                'Database factory should be initialized with DatabaseFactory::initialize() call.'
38
            );
39
        }
40
41 19
        return self::$factory->create($config);
42
    }
43
}
44