Passed
Push — master ( 125f6d...0fede7 )
by Alexander
02:05
created

WidgetFactory::createWidget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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 8
ccs 4
cts 4
cp 1
crap 2
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 13
    private function __construct(ContainerInterface $container = null, array $definitions = [])
16
    {
17 13
        parent::__construct($container, $definitions);
18 13
    }
19
20 13
    public static function initialize(ContainerInterface $container = null, array $definitions = []): void
21
    {
22 13
        self::$factory = new self($container, $definitions);
23 13
    }
24
25
    /**
26
     * Creates a widget defined by config passed
27
     *
28
     * @param array|callable|string $config parameters for creating a widget
29
     *
30
     * @throws \RuntimeException if factory was not initialized
31
     * @throws \Yiisoft\Factory\Exceptions\InvalidConfigException
32
     *
33
     * @psalm-suppress MoreSpecificReturnType
34
     *
35
     * @return Widget
36
     */
37 13
    public static function createWidget($config): Widget
38
    {
39 13
        if (self::$factory === null) {
40 1
            throw new \RuntimeException('Widget factory should be initialized with WidgetFactory::initialize() call.');
41
        }
42
43
        /** @psalm-suppress LessSpecificReturnStatement */
44 12
        return self::$factory->create($config);
45
    }
46
}
47