SegmentFactoryPass   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 18
c 1
b 0
f 0
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 24 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Community\Symfony\DependencyInjection;
6
7
use Pheature\Core\Toggle\Read\ChainSegmentFactory;
8
use Pheature\Core\Toggle\Read\SegmentFactory;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Webmozart\Assert\Assert;
13
14
final class SegmentFactoryPass implements CompilerPassInterface
15 1
{
16
    public function process(ContainerBuilder $container): void
17
    {
18 1
        /** @var array<array<mixed>> $pheatureFlagsConfig */
19 1
        $pheatureFlagsConfig = $container->getExtensionConfig('pheature_flags');
20
        $mergedConfig = array_merge(...$pheatureFlagsConfig);
21 1
22 1
        $segmentFactory = $container->register(SegmentFactory::class, SegmentFactory::class)
23 1
            ->setAutowired(false)
24 1
            ->setLazy(true)
25
            ->setClass(ChainSegmentFactory::class);
26 1
27 1
        Assert::keyExists($mergedConfig, 'segment_types');
28 1
        Assert::isArray($mergedConfig['segment_types']);
29 1
30
        foreach ($mergedConfig['segment_types'] as $segmentDefinition) {
31 1
            Assert::keyExists($segmentDefinition, 'type');
32
            Assert::string($segmentDefinition['type']);
33 1
            Assert::keyExists($segmentDefinition, 'factory_id');
34
            Assert::string($segmentDefinition['factory_id']);
35
            $container->register($segmentDefinition['type'], $segmentDefinition['factory_id'])
36
                ->setAutowired(false)
37
                ->setLazy(true);
38
39
            $segmentFactory->addArgument(new Reference($segmentDefinition['type']));
40
        }
41
    }
42
}
43