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

DatabaseFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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