TransformersCompilerPass::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Bridge Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\BridgeBundle\DependencyInjection\Compiler;
16
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class TransformersCompilerPass implements CompilerPassInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function process(ContainerBuilder $container)
27
    {
28
        if (!$container->hasDefinition('swp_bridge.http_push.transformer_chain')) {
29
            return;
30
        }
31
32
        $definition = $container->getDefinition('swp_bridge.http_push.transformer_chain');
33
        $transformers = $this->findAndSortTaggedServices('transformer.http_push_transformer', $container);
34
35
        $definition->replaceArgument(0, $transformers);
36
    }
37
38
    /**
39
     * Finds all services with the given tag name and order them by their priority.
40
     * Can be replaced by PriorityTaggedServiceTrait when upgraded to Symfony 3.0.
41
     *
42
     * @param string           $tagName
43
     * @param ContainerBuilder $container
44
     *
45
     * @return Reference[]
46
     */
47
    private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
48
    {
49
        $services = $container->findTaggedServiceIds($tagName);
50
        $queue = new \SplPriorityQueue();
51
        foreach ($services as $serviceId => $tags) {
52
            foreach ($tags as $attributes) {
53
                $priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
54
                $queue->insert(new Reference($serviceId), $priority);
55
            }
56
        }
57
58
        return iterator_to_array($queue, false);
59
    }
60
}
61