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

WidgetFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 34
ccs 10
cts 10
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A initialize() 0 3 1
A createWidget() 0 8 2
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