process()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 1
nop 1
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 3
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hgraca\DoctrineTestDbRegenerationBundle\DependencyInjection;
6
7
use Hgraca\DoctrineTestDbRegenerationBundle\DependencyInjection\Exception\AbstractServiceException;
8
use Hgraca\DoctrineTestDbRegenerationBundle\DependencyInjection\Exception\ServiceNotFoundException;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Symfony\Component\DependencyInjection\ServiceLocator;
13
14
final class CreateDbRegenerationServiceLocatorCompilerPass implements CompilerPassInterface
15
{
16
    public const TAG_SERVICE_LOCATOR = 'container.service_locator';
17
    public const TEST_DB_REGENERATION_SERVICE_LOCATOR = 'test_db_regeneration.service_locator';
18
19 6
    public function process(ContainerBuilder $containerBuilder): void
20
    {
21
        /** @var HgracaDoctrineTestDbRegenerationExtension $config */
22 6
        $config = $containerBuilder->getExtension('hgraca_doctrine_test_db_regeneration');
23
24 6
        $serviceIdList = $config->getServicesIdList();
25
26 6
        $containerBuilder->register(self::TEST_DB_REGENERATION_SERVICE_LOCATOR, ServiceLocator::class)
27 6
            ->setPublic(true)
28 6
            ->addTag(self::TAG_SERVICE_LOCATOR)
29 6
            ->setArguments(
30
                [
31 6
                    array_combine(
32 6
                        $serviceIdList,
33 6
                        array_map(
34 6
                            function (string $id) use ($containerBuilder): Reference {
35 6
                                if (!$containerBuilder->has($id)) {
36 2
                                    throw new ServiceNotFoundException("Service with ID '$id' was not found.");
37
                                }
38
39 4
                                if ($containerBuilder->findDefinition($id)->isAbstract()) {
40 2
                                    throw new AbstractServiceException();
41
                                }
42
43 2
                                return new Reference($id);
44 6
                            },
45 6
                            $serviceIdList
46
                        )
47
                    ),
48
                ]
49
            );
50 2
    }
51
}
52