Completed
Pull Request — master (#20)
by Paulo Rodrigues
13:07 queued 31s
created

BasePackagesCompilerPass::process()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 6.7273
cc 7
eloc 18
nc 7
nop 1
crap 7
1
<?php
2
3
namespace Rj\FrontendBundle\DependencyInjection\Compiler\Packages;
4
5
use Rj\FrontendBundle\DependencyInjection\Compiler\BaseCompilerPass;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
abstract class BasePackagesCompilerPass extends BaseCompilerPass
10
{
11
    abstract protected function getTaggedPackages($container);
12
    abstract protected function getPackagesService($container);
13
14 40
    public function process(ContainerBuilder $container)
15
    {
16 40
        $packages = array();
17 40
        $registeredPackages = $this->getRegisteredPackages($container);
18
19 38
        foreach ($this->getTaggedPackages($container) as $id => $tags) {
20 20
            if (empty($tags) || !isset($tags[0]['alias'])) {
21 2
                throw new \LogicException(
22 2
                    "The tag for the service with id '$id' must define an 'alias' attribute"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
23
                );
24
            }
25
26 18
            $packageName = $tags[0]['alias'];
27
28 18
            if (isset($registeredPackages[$packageName])) {
29 2
                throw new \LogicException(
30 2
                    "A package named '$packageName' has already been registered"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
31
                );
32
            }
33
34 16
            if (isset($packages[$packageName])) {
35 2
                throw new \LogicException(
36 2
                    "Multiple packages were found with alias '$packageName'. Package alias' must be unique"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
37
                );
38
            }
39
40 16
            $packages[$packageName] = $id;
41
        }
42
43 32
        $this->addPackages($packages, $container);
44
45 32
        if ($container->hasDefinition($this->namespaceService('package.fallback'))) {
46 26
            $this->setDefaultPackage($container);
47
        }
48 32
    }
49
50 32
    protected function addPackages($packages, $container)
51
    {
52 32
        $packagesService = $this->getPackagesService($container);
53
54 32
        foreach ($packages as $name => $id) {
55 14
            $packagesService->addMethodCall(
56 14
                'addPackage',
57 14
                array($name, new Reference($id))
58
            );
59
        }
60 32
    }
61
62 26
    protected function setDefaultPackage($container)
63
    {
64 26
        $packagesService = $this->getPackagesService($container);
65 26
        $defaultPackage = $this->getRegisteredDefaultPackage($container);
66 26
        $fallbackPackageId = $this->namespaceService('package.fallback');
67 26
        $fallbackPackageDefinition = $container->getDefinition($fallbackPackageId);
68
69 26
        if ($packagesService->getScope() === 'request') {
70 11
            $fallbackPackageDefinition->setScope('request');
71
        }
72
73 26
        $fallbackPackageDefinition->addMethodCall('setFallback', array($defaultPackage));
74
75 26
        $packagesService->replaceArgument(0, new Reference($fallbackPackageId));
76 26
    }
77
78
    /**
79
     * Retrieve packages that have already been registered.
80
     *
81
     * @return array with the packages' name as keys
82
     */
83 40
    protected function getRegisteredPackages($container)
84
    {
85 40
        $arguments = $this->getPackagesService($container)->getArguments();
86
87 38
        if (!isset($arguments[1]) || count($arguments[1]) < 2) {
88 34
            return array();
89
        }
90
91 4
        $argPackages = $arguments[1];
92
93 4
        $packages = array();
94 4
        $argCount = count($argPackages);
95 4
        for ($i = 0; $i < $argCount; $i++) {
96 4
            $packages[$argPackages[$i]] = $argPackages[++$i];
97
        }
98
99 4
        return $packages;
100
    }
101
102 26
    protected function getRegisteredDefaultPackage($container)
103
    {
104 26
        $arguments = $this->getPackagesService($container)->getArguments();
105
106 26
        if (!isset($arguments[0])) {
107
            return;
108
        }
109
110 26
        return $arguments[0];
111
    }
112
}
113