Test Setup Failed
Branch 0.x (5da7af)
by Pavel
13:48
created

ProxyLocatorPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\DependencyInjection\Compiler;
17
18
use Ds\PriorityQueue;
19
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Veslo\AppBundle\Http\Proxy\LocatorChain;
24
use Veslo\AppBundle\Http\Proxy\LocatorInterface;
25
26
/**
27
 * Aggregates available locators into locator chain for proxy list resolving
28
 *
29
 * There are at least two handy ways of managing service priority while processing tags:
30
 * `PriorityTaggedServiceTrait` and `Ds\PriorityQueue`
31
 *
32
 * @see PriorityTaggedServiceTrait
33
 * @see PriorityQueue
34
 * @see LocatorChain
35
 */
36
class ProxyLocatorPass implements CompilerPassInterface
37
{
38
    /**
39
     * Service id for aggregation
40
     *
41
     * @const string
42
     */
43
    private const SERVICE_ID_AGGREGATOR = 'veslo.app.http.proxy.locator_chain';
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function process(ContainerBuilder $container)
49
    {
50
        $proxyLocatorServiceIds = $container->findTaggedServiceIds(LocatorInterface::TAG);
51
        $locatorChainDefinition = $container->getDefinition(self::SERVICE_ID_AGGREGATOR);
52
53
        foreach ($proxyLocatorServiceIds as $serviceId => $tags) {
54
            foreach ($tags as $attributes) {
55
                $priority    = $attributes['priority'] ?? 0;
56
                $isImportant = !empty($attributes['isImportant']);
57
58
                $proxyLocatorReference = new Reference($serviceId);
59
                $locatorChainDefinition->addMethodCall('addLocator', [$proxyLocatorReference, $priority, $isImportant]);
60
            }
61
        }
62
    }
63
}
64