AssetCompilerPass   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.21%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 147
ccs 55
cts 56
cp 0.9821
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
C process() 0 35 7
A getTaggedPackages() 0 4 1
A getPackagesService() 0 8 2
A addPackages() 0 11 2
A setDefaultPackage() 0 10 1
A getRegisteredPackages() 0 18 4
A getRegisteredDefaultPackage() 0 10 2
A namespaceService() 0 4 1
1
<?php
2
3
namespace Rj\FrontendBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class AssetCompilerPass implements CompilerPassInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 19
    public function process(ContainerBuilder $container)
16
    {
17 19
        $packages = [];
18 19
        $registeredPackages = $this->getRegisteredPackages($container);
19
20 18
        foreach ($this->getTaggedPackages($container) as $id => $tags) {
21 10
            if (empty($tags) || !isset($tags[0]['alias'])) {
22 1
                throw new \LogicException(
23 1
                    "The tag for the service with id '$id' must define an 'alias' attribute"
24
                );
25
            }
26
27 9
            $packageName = $tags[0]['alias'];
28
29 9
            if (isset($registeredPackages[$packageName])) {
30 1
                throw new \LogicException(
31 1
                    "A package named '$packageName' has already been registered"
32
                );
33
            }
34
35 8
            if (isset($packages[$packageName])) {
36 1
                throw new \LogicException(
37 1
                    "Multiple packages were found with alias '$packageName'. Package alias' must be unique"
38
                );
39
            }
40
41 8
            $packages[$packageName] = $id;
42
        }
43
44 15
        $this->addPackages($packages, $container);
45
46 15
        if ($container->hasDefinition($this->namespaceService('package.fallback'))) {
47 12
            $this->setDefaultPackage($container);
48
        }
49 15
    }
50
51
    /**
52
     * @param ContainerBuilder $container
53
     *
54
     * @return Definition[]
55
     */
56 18
    private function getTaggedPackages(ContainerBuilder $container)
57
    {
58 18
        return $container->findTaggedServiceIds($this->namespaceService('package.asset'));
59
    }
60
61
    /**
62
     * @param ContainerBuilder $container
63
     *
64
     * @return Definition
65
     */
66 19
    private function getPackagesService(ContainerBuilder $container)
67
    {
68 19
        if (!$container->hasDefinition('assets.packages')) {
69 1
            throw new \LogicException('The Asset component is not registered in the container');
70
        }
71
72 18
        return $container->getDefinition('assets.packages');
73
    }
74
75
    /**
76
     * @param Definition[]     $packages
77
     * @param ContainerBuilder $container
78
     */
79 15
    private function addPackages($packages, ContainerBuilder $container)
80
    {
81 15
        $packagesService = $this->getPackagesService($container);
82
83 15
        foreach ($packages as $name => $id) {
84 7
            $packagesService->addMethodCall(
85 7
                'addPackage',
86 7
                [$name, new Reference($id)]
0 ignored issues
show
Documentation introduced by Paulo Rodrigues Pinto
$id is of type object<Symfony\Component...cyInjection\Definition>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
            );
88
        }
89 15
    }
90
91
    /**
92
     * @param ContainerBuilder $container
93
     */
94 12
    private function setDefaultPackage(ContainerBuilder $container)
95
    {
96 12
        $packagesService = $this->getPackagesService($container);
97 12
        $defaultPackage = $this->getRegisteredDefaultPackage($container);
98 12
        $fallbackPackageId = $this->namespaceService('package.fallback');
99
100 12
        $container->getDefinition($fallbackPackageId)->addMethodCall('setFallback', [$defaultPackage]);
101
102 12
        $packagesService->replaceArgument(0, new Reference($fallbackPackageId));
103 12
    }
104
105
    /**
106
     * Retrieve packages that have already been registered.
107
     *
108
     * @param ContainerBuilder $container
109
     *
110
     * @return array with the packages' name as keys
111
     */
112 19
    private function getRegisteredPackages(ContainerBuilder $container)
113
    {
114 19
        $arguments = $this->getPackagesService($container)->getArguments();
115
116 18
        if (!isset($arguments[1]) || count($arguments[1]) < 2) {
117 16
            return [];
118
        }
119
120 2
        $argPackages = $arguments[1];
121
122 2
        $packages = [];
123 2
        $argCount = count($argPackages);
124 2
        for ($i = 0; $i < $argCount; ++$i) {
125 2
            $packages[$argPackages[$i]] = $argPackages[++$i];
126
        }
127
128 2
        return $packages;
129
    }
130
131
    /**
132
     * @param ContainerBuilder $container
133
     *
134
     * @return Definition|null
135
     */
136 12
    private function getRegisteredDefaultPackage(ContainerBuilder $container)
137
    {
138 12
        $arguments = $this->getPackagesService($container)->getArguments();
139
140 12
        if (!isset($arguments[0])) {
141
            return null;
142
        }
143
144 12
        return $arguments[0];
145
    }
146
147
    /**
148
     * @param string $id
149
     *
150
     * @return string
151
     */
152 18
    private function namespaceService($id)
153
    {
154 18
        return "rj_frontend.$id";
155
    }
156
}
157