Passed
Push — master ( 0074b5...431277 )
by Alexander
02:14
created

WidgetFactory::createWidget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Widget;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Factory;
9
use Yiisoft\Factory\FactoryInterface;
10
11
final class WidgetFactory extends Factory
12
{
13
    private static ?FactoryInterface $factory = null;
14
15 8
    private function __construct(ContainerInterface $container = null, array $definitions = [])
16
    {
17 8
        parent::__construct($container, $definitions);
18 8
    }
19
20 8
    public static function initialize(ContainerInterface $container = null, array $definitions = []): void
21
    {
22 8
        self::$factory = new self($container, $definitions);
23 8
    }
24
25
    /**
26
     * Creates a widget defined by config passed
27
     *
28
     * @param string|array|callable $config parameters for creating a widget
29
     * @throws \RuntimeException if factory was not initialized
30
     * @throws \Yiisoft\Factory\Exceptions\InvalidConfigException
31
     */
32 7
    public static function createWidget($config): Widget
33
    {
34 7
        if (static::$factory === null) {
0 ignored issues
show
Bug introduced by
Since $factory is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $factory to at least protected.
Loading history...
35
            throw new \RuntimeException('Widget factory should be initialized with WidgetFactory::initialize() call.');
36
        }
37
38 7
        return static::$factory->create($config);
39
    }
40
}
41