CachePoolPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 19 1
A getServiceReference() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MovingImage\Bundle\VMProApiBundle\DependencyInjection\Compiler;
6
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class CachePoolPass implements CompilerPassInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function process(ContainerBuilder $container): void
17
    {
18
        // inject cache pool to API client
19
        $clientDefinition = $container->getDefinition('vmpro_api.client');
20
        $clientDefinitionArguments = $clientDefinition->getArguments();
21
22
        $clientDefinitionArguments[2] = $this->getServiceReference($container, 'vm_pro_api_logger');
23
        $clientDefinitionArguments[3] = $this->getServiceReference($container, 'vm_pro_api_cache_pool');
24
        $clientDefinitionArguments[4] = $container->getParameter('vm_pro_api_cache_ttl');
25
26
        $clientDefinition->setArguments($clientDefinitionArguments);
27
28
        // inject cache pool to TokenManager
29
        $tokenManagerDefinition = $container->getDefinition('vmpro_api.token_manager');
30
        $tokenManagerDefinitionArguments = $tokenManagerDefinition->getArguments();
31
        $tokenManagerDefinitionArguments[3] = $this->getServiceReference($container, 'vm_pro_api_cache_pool');
32
33
        $tokenManagerDefinition->setArguments($tokenManagerDefinitionArguments);
34
    }
35
36
    /**
37
     * Returns a reference to a service, if that service exists in the container.
38
     * The service ID is obtained by fetching the value of the provided $parameterName.
39
     * This allows us to inject a service into the bundle by supplying the service ID in the bundle configuration.
40
     *
41
     * If the specified service does not exist in the container, an exception is thrown.
42
     *
43
     * @throws \Exception
44
     */
45
    private function getServiceReference(ContainerBuilder $container, string $parameterName): ?Reference
46
    {
47
        $serviceId = $container->getParameter($parameterName);
48
        if (empty($serviceId)) {
49
            return null;
50
        }
51
52
        if ($container->hasDefinition($serviceId) || $container->hasAlias($serviceId)) {
53
            return new Reference($serviceId);
54
        }
55
56
        throw new \Exception(sprintf('Service "%s" specified in parameter "%s" does not exist', $serviceId, $parameterName));
57
    }
58
}
59