Completed
Pull Request — master (#24)
by Emanuele
07:08
created

AeFeatureExtension::canLoadSonataAdmin()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 1
crap 3.009
1
<?php
2
3
namespace Ae\FeatureBundle\DependencyInjection;
4
5
use Ae\FeatureBundle\Admin\FeatureAdmin;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration.
14
 *
15
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
16
 */
17
class AeFeatureExtension extends Extension
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 18
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 18
        $loader = new XmlFileLoader(
25 18
            $container,
26 18
            new FileLocator(__DIR__.'/../Resources/config')
27 18
        );
28
29 18
        $loader->load('services.xml');
30
31 18
        if ($this->canLoadSonataAdmin($container)) {
32 17
            $loader->load('sonata.xml');
33 17
        }
34 18
    }
35
36
    /**
37
     * Checks if the Admin class can be loaded without conflicts.
38
     *
39
     * @param ContainerBuilder $container
40
     *
41
     * @return bool
42
     */
43 18
    private function canLoadSonataAdmin(ContainerBuilder $container)
44
    {
45 18
        if (!class_exists('Sonata\AdminBundle\Admin\Admin')) {
46
            return false;
47
        }
48
49 18
        $conflicts = array_filter(
50 18
            $container->getDefinitions(),
51 18
            function (Definition $definition) {
52 18
                return FeatureAdmin::class === $definition->getClass() &&
53 18
                    $definition->hasTag('sonata.admin');
54
            }
55 18
        );
56
57 18
        return empty($conflicts);
58
    }
59
}
60