Completed
Push — master ( 920f25...f45e9b )
by Ruben
04:03
created

CachePoolPass::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MovingImage\Bundle\VMProApiBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
class CachePoolPass implements CompilerPassInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function process(ContainerBuilder $container)
15
    {
16
        $clientDefinition = $container->getDefinition('vmpro_api.client');
17
18
        $clientDefinition->setArgument(2, $this->getServiceReference($container, 'vm_pro_api_logger'));
19
        $clientDefinition->setArgument(3, $this->getServiceReference($container, 'vm_pro_api_cache_pool'));
20
        $clientDefinition->setArgument(4, $container->getParameter('vm_pro_api_cache_ttl'));
21
    }
22
23
    /**
24
     * Returns a reference to a service, if that service exists in the container.
25
     * The service ID is obtained by fetching the value of the provided $parameterName.
26
     * This allows us to inject a service into the bundle by supplying the service ID in the bundle configuration.
27
     *
28
     * If the specified service does not exist in the container, an exception is thrown.
29
     *
30
     * @param ContainerBuilder $container
31
     * @param string           $parameterName
32
     *
33
     * @return Reference|null
34
     *
35
     * @throws \Exception
36
     */
37
    private function getServiceReference(ContainerBuilder $container, $parameterName)
38
    {
39
        $serviceId = $container->getParameter($parameterName);
40
        if (empty($serviceId)) {
41
            return null;
42
        }
43
44
        if ($container->hasDefinition($serviceId) || $container->hasAlias($serviceId)) {
45
            return new Reference($serviceId);
46
        }
47
48
        throw new \Exception(sprintf(
49
            'Service "%s" specified in parameter "%s" does not exist',
50
            $serviceId,
51
            $parameterName
52
        ));
53
    }
54
}
55