Passed
Pull Request — master (#3)
by Vincent
08:42
created

PrimeConnectionFactoryPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
namespace Bdf\PrimeBundle\DependencyInjection\Compiler;
4
5
use Bdf\Prime\Connection\Factory\ChainFactory;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
10
11
/**
12
 * Registers all service tag as loader into the serializer
13
 */
14
class PrimeConnectionFactoryPass implements CompilerPassInterface
15
{
16
    use PriorityTaggedServiceTrait;
17
18
    private $service;
19
    private $tag;
20
21 9
    public function __construct(string $service = ChainFactory::class, string $loaderTag = 'bdf_prime.connection_factory')
22
    {
23 9
        $this->service = $service;
24 9
        $this->tag = $loaderTag;
25 9
    }
26
27 8
    public function process(ContainerBuilder $container)
28
    {
29 8
        if (!$container->hasDefinition($this->service)) {
30
            return;
31
        }
32
33 8
        if (!$factories = $this->findAndSortTaggedServices($this->tag, $container)) {
34 1
            throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->tag, $this->service));
35
        }
36
37 7
        $connectionFactory = $container->getDefinition($this->service);
38 7
        $connectionFactory->replaceArgument(0, $factories);
39 7
    }
40
}
41