Passed
Pull Request — 1.x (#9)
by Marko
02:32
created

FindClassTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 19 3
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
 * @author Marko Kunic <[email protected]>
11
 */
12
trait FindClassTrait
13
{
14
    private function getClass(ContainerBuilder $container, string $id): ?string
15
    {
16
        $definition = $container->getDefinition($id);
17
18
        //Entity can be a class or a parameter
19
        $class = $definition->getArgument(1); //1 is Entity
20
21
        if ($class[0] !== '%') {
22
            return $class;
23
        }
24
25
        if ($container->hasParameter($class = trim($class, '%'))) {
26
            return $container->getParameter($class);
27
        }
28
29
        throw new \LogicException(sprintf(
30
            'Service "%s" has a parameter "%s" as an argument but it is not found.',
31
            $id,
32
            $class
33
        ));
34
    }
35
}
36