TimeZoneResolverPass   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 43
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 23 4
B sort() 0 7 5
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DateBundle\DependencyInjection\Compiler;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
class TimeZoneResolverPass implements CompilerPassInterface
17
{
18
    /**
19
     * @param ContainerBuilder $container
20
     */
21
    public function process(ContainerBuilder $container)
22
    {
23
        if (!$container->has('gpslab.date.tz.resolver.collection')) {
24
            return;
25
        }
26
27
        $resolvers = [];
28
        foreach ($container->findTaggedServiceIds('gpslab.date.tz.resolver') as  $id => $attributes) {
29
            $resolvers[] = [
30
                'id' => $id,
31
                'attributes' => $attributes[0],
32
            ];
33
        }
34
35
        usort($resolvers, function ($a, $b) {
36
            return $this->sort($a, $b);
37
        });
38
39
        $definition = $container->findDefinition('gpslab.date.tz.resolver.collection');
40
        foreach ($resolvers as $resolver) {
41
            $definition->addMethodCall('addResolver', [new Reference($resolver['id'])]);
42
        }
43
    }
44
45
    /**
46
     * @param array $a
47
     * @param array $b
48
     *
49
     * @return int
50
     */
51
    protected function sort($a, $b)
52
    {
53
        $a = isset($a['attributes']['priority']) ? $a['attributes']['priority'] : 0;
54
        $b = isset($b['attributes']['priority']) ? $b['attributes']['priority'] : 0;
55
56
        return $a < $b ? -1 : ($a > $b ? 1 : 0);
57
    }
58
}
59