SegmentFactoryPass::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 9.7
cc 2
nc 2
nop 1
crap 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