ValidateContainerDefinitionTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 15
c 1
b 1
f 0
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 10
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 14
    private function validateContainerDefinitionExists(ContainerBuilder $containerBuilder, string $name): Definition
23
    {
24 14
        if (!$containerBuilder->hasDefinition($name)) {
25 2
            throw new NotSupportedException(\sprintf('The "%s" service should already be set in the container.', $name));
26
        }
27
28 12
        return $containerBuilder->getDefinition($name);
29
    }
30
31 3
    private function validateContainerDefinitionClassIs(ContainerBuilder $containerBuilder, string $name, string $expectedClass): Definition
32
    {
33 3
        $definition = $this->validateContainerDefinitionExists($containerBuilder, $name);
34 2
        if ($expectedClass !== $definition->getClass()) {
35 1
            throw new NotSupportedException(\sprintf('The "%s" service class should be "%s".', $name, $expectedClass));
36
        }
37
38 1
        return $definition;
39
    }
40
41 11
    private function validateContainerDefinitionClassImplements(ContainerBuilder $containerBuilder, string $name, string $expectedClass): Definition
42
    {
43 11
        $definition = $this->validateContainerDefinitionExists($containerBuilder, $name);
44 10
        $definitionClass = $definition->getClass();
45 10
        if (!\is_string($definitionClass)) {
46 1
            throw new NotSupportedException(\sprintf('The "%s" service class should be a string.', $name));
47
        }
48
49 9
        if (!(new \ReflectionClass($definitionClass))->implementsInterface($expectedClass)) {
50 1
            throw new NotSupportedException(\sprintf('The "%s" service class should implement the "%s" interface.', $name, $expectedClass));
51
        }
52
53 8
        return $definition;
54
    }
55
}
56