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

CachePoolPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 46
ccs 0
cts 22
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 8 1
A getServiceReference() 0 17 4
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