Completed
Push — master ( 76c18a...59b186 )
by Thomas
24s
created

ValidateContainerDefinitionTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateContainerDefinitionClassImplements() 0 13 3
A validateContainerDefinitionExists() 0 7 2
A validateContainerDefinitionClassIs() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Action;
15
16
use Ekino\Drupal\Debug\Exception\NotSupportedException;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
20
trait ValidateContainerDefinitionTrait
21
{
22 20
    private function validateContainerDefinitionExists(ContainerBuilder $containerBuilder, string $name): Definition
23
    {
24 20
        if (!$containerBuilder->hasDefinition($name)) {
25 4
            throw new NotSupportedException(\sprintf('The "%s" service should already be set in the container.', $name));
26
        }
27
28 18
        return $containerBuilder->getDefinition($name);
29
    }
30
31 9
    private function validateContainerDefinitionClassIs(ContainerBuilder $containerBuilder, string $name, string $expectedClass): Definition
32
    {
33 9
        $definition = $this->validateContainerDefinitionExists($containerBuilder, $name);
34 8
        if ($expectedClass !== $definition->getClass()) {
35 1
            throw new NotSupportedException(\sprintf('The "%s" service class should be "%s".', $name, $expectedClass));
36
        }
37
38 7
        return $definition;
39
    }
40
41 18
    private function validateContainerDefinitionClassImplements(ContainerBuilder $containerBuilder, string $name, string $expectedClass): Definition
42
    {
43 18
        $definition = $this->validateContainerDefinitionExists($containerBuilder, $name);
44 16
        $definitionClass = $definition->getClass();
45 16
        if (!\is_string($definitionClass)) {
46 3
            throw new NotSupportedException(\sprintf('The "%s" service class should be a string.', $name));
47
        }
48
49 14
        if (!(new \ReflectionClass($definitionClass))->implementsInterface($expectedClass)) {
50 3
            throw new NotSupportedException(\sprintf('The "%s" service class should implement the "%s" interface.', $name, $expectedClass));
51
        }
52
53 12
        return $definition;
54
    }
55
}
56