Passed
Pull Request — master (#41)
by Mathieu
03:42
created

FindClassTrait::getClass()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 23
rs 8.8333
nc 4
nop 2
cc 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\DependencyInjection\Compiler;
6
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * @internal
11
 *
12
 * @author Marko Kunic <[email protected]>
13
 */
14
trait FindClassTrait
15
{
16
    private function getClass(ContainerBuilder $container, string $id): ?string
17
    {
18
        $definition = $container->getDefinition($id);
19
20
        //Entity can be a class or a parameter
21
        $class = $definition->getArgument(1); //1 is Entity
22
23
        if ($class && $container->hasDefinition($class)) {
24
            return $container->getDefinition($class)->getClass();
25
        }
26
27
        if ($class === null || (is_string($class) && $class[0] !== '%')) {
28
            return $class;
29
        }
30
31
        if ($container->hasParameter($class = trim($class, '%'))) {
32
            return $container->getParameter($class);
33
        }
34
35
        throw new \LogicException(sprintf(
36
            'Service "%s" has a parameter "%s" as an argument but it is not found.',
37
            $id,
38
            $class
39
        ));
40
    }
41
}
42