TimeZoneResolverPass::process()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 17
cp 0
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 1
crap 20
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