CreateDbRegenerationServiceLocatorCompilerPass   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 32
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 27 3
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