OperationFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialiseContainer() 0 10 1
A create() 0 7 2
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper\Operation\Factory;
14
15
use Sauls\Component\Helper\Operation\Operation;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19
20
class OperationFactory implements OperationFactoryInterface
21
{
22
    /**
23
     * @var ContainerBuilder
24
     */
25
    private static $container;
26
    private static $isContainerInitialised = false;
27
28
    /**
29
     * @throws \Exception
30
     */
31 150
    public static function create(string $operationClass): Operation
32
    {
33 150
        if (false === self::$isContainerInitialised) {
34 1
            self::initialiseContainer();
35
        }
36
37 150
        return self::$container->get($operationClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::container->get($operationClass) could return the type null which is incompatible with the type-hinted return Sauls\Component\Helper\Operation\Operation. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
40
    /**
41
     *
42
     * @throws \Exception
43
     */
44 1
    private static function initialiseContainer(): void
45
    {
46 1
        self::$container = new ContainerBuilder();
47
48 1
        $loader = new YamlFileLoader(self::$container, new FileLocator([__DIR__ . '/../../Resources/config']));
49 1
        $loader->load('operations.yml');
50
51 1
        self::$container->compile();
52
53 1
        self::$isContainerInitialised = true;
54 1
    }
55
}
56