Completed
Pull Request — master (#10)
by Tomáš
04:48 queued 01:50
created

PathConfiguratorPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 54
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B process() 0 22 6
A antify() 0 15 2
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\SculpinBundle\DependencyInjection\Compiler;
13
14
use dflydev\util\antPathMatcher\AntPathMatcher;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
final class PathConfiguratorPass implements CompilerPassInterface
19
{
20
    /**
21
     * @var AntPathMatcher
22
     */
23
    private $matcher;
24
25
    public function __construct()
26
    {
27
        $this->matcher = new AntPathMatcher();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function process(ContainerBuilder $container)
34
    {
35
        foreach ($container->findTaggedServiceIds('sculpin.path_configurator') as $tagAttributes) {
36
            foreach ($tagAttributes as $attributes) {
37
                $typeParameter = 'sculpin.'.$attributes['type'];
38
                $parameter = $attributes['parameter'];
39
40
                if ($container->hasParameter($parameter)) {
41
                    $value = $container->getParameter($parameter);
42
                    if (!is_array($value)) {
43
                        $value = [$value];
44
                    }
45
46
                    if ($container->hasParameter($typeParameter)) {
47
                        $container->setParameter($typeParameter, array_unique(array_merge($container->getParameter($typeParameter), $this->antify($value))));
48
                    } else {
49
                        $container->setParameter($typeParameter, array_unique($this->antify($value)));
50
                    }
51
                }
52
            }
53
        }
54
    }
55
56
    private function antify($paths)
57
    {
58
        $matcher = $this->matcher;
59
60
        return array_map(
61
            function ($path) use ($matcher) {
62
                if ($matcher->isPattern($path)) {
63
                    return $path;
64
                }
65
66
                return $path.'/**';
67
            },
68
            $paths
69
        );
70
    }
71
}
72