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

PathConfiguratorPass::process()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 20
cp 0
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 13
nc 7
nop 1
crap 42
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