DependentBenGorFileBundle::checkDependencies()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\FileBundle;
14
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17
use Symfony\Component\HttpKernel\Bundle\Bundle;
18
19
/**
20
 * Trait that allows to check bundle dependencies.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
trait DependentBenGorFileBundle
25
{
26
    /**
27
     * Checks the given bundles are enabled in the container.
28
     *
29
     * @param array            $requiredBundles The required bundles
30
     * @param ContainerBuilder $container       The container builder
31
     */
32
    public function checkDependencies(array $requiredBundles, ContainerBuilder $container)
33
    {
34
        if (false === ($this instanceof Bundle)) {
35
            throw new RuntimeException('It is a bundle trait, you shouldn\'t have to use in other instances');
36
        }
37
38
        $enabledBundles = $container->getParameter('kernel.bundles');
39
        foreach ($requiredBundles as $requiredBundle) {
40
            if (!isset($enabledBundles[$requiredBundle])) {
41
                throw new RuntimeException(
42
                    sprintf(
43
                        'In order to use "%s" you also need to enable and configure the "%s"',
44
                        $this->getName(),
45
                        $requiredBundle
46
                    )
47
                );
48
            }
49
        }
50
    }
51
}
52