Completed
Push — master ( e9cd97...23520c )
by Rafał
23:17
created

TransformersCompilerPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 6
c 1
b 1
f 1
lcom 0
cbo 3
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 2
A findAndSortTaggedServices() 0 13 4
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
namespace SWP\Bundle\BridgeBundle\DependencyInjection\Compiler;
15
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
class TransformersCompilerPass implements CompilerPassInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function process(ContainerBuilder $container)
26
    {
27
        if (!$container->hasDefinition('swp_bridge.http_push.transformer_chain')) {
28
            return;
29
        }
30
31
        $definition = $container->getDefinition('swp_bridge.http_push.transformer_chain');
32
        $transformers = $this->findAndSortTaggedServices('transformer.http_push_transformer', $container);
33
34
        $definition->replaceArgument(0, $transformers);
35
    }
36
37
    /**
38
     * Finds all services with the given tag name and order them by their priority.
39
     * Can be replaced by PriorityTaggedServiceTrait when upgraded to Symfony 3.0.
40
     *
41
     * @param string           $tagName
42
     * @param ContainerBuilder $container
43
     *
44
     * @return Reference[]
45
     */
46
    private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
47
    {
48
        $services = $container->findTaggedServiceIds($tagName);
49
        $queue = new \SplPriorityQueue();
50
        foreach ($services as $serviceId => $tags) {
51
            foreach ($tags as $attributes) {
52
                $priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
53
                $queue->insert(new Reference($serviceId), $priority);
54
            }
55
        }
56
57
        return iterator_to_array($queue, false);
58
    }
59
}
60