RegisterFailerDriverPass::process()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
ccs 11
cts 13
cp 0.8462
crap 4.0582
1
<?php
2
3
namespace Bdf\QueueBundle\DependencyInjection\Compiler;
4
5
use Bdf\Dsn\Dsn;
6
use Bdf\Queue\Failer\FailedJobRepositoryInterface;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
/**
11
 * Register the failer repository on the container and configure the alias to point to the configured failer
12
 * This compiler pass will parse the failer dsn, and check for configurator drivers registered
13
 * with tag "bdf_queue.failer.driver_configurator".
14
 */
15
final class RegisterFailerDriverPass implements CompilerPassInterface
16
{
17
    public const CONFIGURATOR_TAG_NAME = 'bdf_queue.failer.driver_configurator';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 9
    public function process(ContainerBuilder $container)
23
    {
24 9
        $dsn = Dsn::parse($container->getParameter('bdf_queue.failer_dsn'));
25 9
        $scheme = $dsn->getScheme();
26 9
        $availableScheme = [];
27
28 9
        foreach ($container->findTaggedServiceIds(self::CONFIGURATOR_TAG_NAME) as $serviceId => $tag) {
29
            /** @var FailerDriverConfiguratorInterface $configurator */
30 9
            $configurator = $container->get($serviceId);
31
32 9
            if (!$configurator->available($container)) {
33
                continue;
34
            }
35
36 9
            $availableScheme[] = $configurator->scheme();
37 9
            if ($scheme === $configurator->scheme()) {
38 9
                $container->setAlias(FailedJobRepositoryInterface::class, $configurator->configure($dsn, $container));
39
40 7
                return;
41
            }
42
        }
43
44
        throw new \InvalidArgumentException('Unsupported failer DSN scheme '.$scheme.'. Available : '.implode(', ', $availableScheme));
45
    }
46
}
47