Completed
Push — master ( 0dd64c...9ed0b2 )
by Adam
08:02 queued 02:35
created

AbstractWellcommerceKernel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 66
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C initializeBundles() 0 63 11
1
<?php
2
3
namespace WellCommerce\Bundle\StandardEditionBundle\HttpKernel;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Symfony\Component\HttpKernel\Kernel;
7
use WellCommerce\Bundle\CoreBundle\Loader\BundleLoader;
8
use WellCommerce\Bundle\StandardEditionBundle\WellCommerceStandardEditionBundle;
9
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
10
11
abstract class AbstractWellcommerceKernel extends Kernel
12
{
13
    protected function initializeBundles()
14
    {
15
        $loader  = new BundleLoader($this);
16
        $bundles = new ArrayCollection();
17
18
19
        WellCommerceStandardEditionBundle::registerBundles($bundles, $this->getEnvironment());
20
        $loader->registerBundles($bundles, $this->getEnvironment());
21
22
        $bundles = array_merge($this->registerBundles(), $bundles->toArray());
23
24
        // init bundles
25
        $this->bundles = array();
26
        $topMostBundles = array();
27
        $directChildren = array();
28
29
        foreach ($bundles as $bundle) {
30
31
            /** @var  BundleInterface $bundle */
32
            $name = $bundle->getName();
33
            if (isset($this->bundles[$name])) {
34
                throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name));
35
            }
36
            $this->bundles[$name] = $bundle;
37
38
            if ($parentName = $bundle->getParent()) {
39
                if (isset($directChildren[$parentName])) {
40
                    throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName]));
41
                }
42
                if ($parentName == $name) {
43
                    throw new \LogicException(sprintf('Bundle "%s" can not extend itself.', $name));
44
                }
45
                $directChildren[$parentName] = $name;
46
            } else {
47
                $topMostBundles[$name] = $bundle;
48
            }
49
        }
50
51
        // look for orphans
52
        if (!empty($directChildren) && count($diff = array_diff_key($directChildren, $this->bundles))) {
53
            $diff = array_keys($diff);
54
55
            throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0]));
56
        }
57
58
        // inheritance
59
        $this->bundleMap = array();
60
        foreach ($topMostBundles as $name => $bundle) {
61
            $bundleMap = array($bundle);
62
            $hierarchy = array($name);
63
64
            while (isset($directChildren[$name])) {
65
                $name = $directChildren[$name];
66
                array_unshift($bundleMap, $this->bundles[$name]);
67
                $hierarchy[] = $name;
68
            }
69
70
            foreach ($hierarchy as $hierarchyBundle) {
71
                $this->bundleMap[$hierarchyBundle] = $bundleMap;
72
                array_pop($bundleMap);
73
            }
74
        }
75
    }
76
}