Passed
Branch master (046a17)
by Mathieu
02:22
created

FindClassTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\DependencyInjection\Compiler;
6
7
use LogicException;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
/**
11
 * Find class function trait.
12
 *
13
 * @internal
14
 *
15
 * @author Marko Kunic <[email protected]>
16
 * @author Mathieu Wambre <[email protected]>
17
 */
18
trait FindClassTrait
19
{
20
21
    /**
22
     * Get the class associated to the given admin service id.
23
     *
24
     * @param ContainerBuilder $container Kernel container.
25
     * @param string           $id        Admin service id.
26
     *
27
     * @return string|null
28
     */
29
    private function getClass(ContainerBuilder $container, string $id): ?string
30
    {
31
        $definition = $container->getDefinition($id);
32
33
        //Entity can be a class or a parameter
34
        $tag = current($definition->getTag('sonata.admin'));
35
        $class = $tag['model_class'] ?? '';
36
37
        if ($class[0] !== '%') {
38
            return $class;
39
        }
40
41
        if ($container->hasParameter($class = trim($class, '%'))) {
42
            return $container->getParameter($class);
43
        }
44
45
        throw new LogicException(sprintf(
46
            'Service "%s" has a parameter "%s" as an argument but it is not found.',
47
            $id,
48
            $class
49
        ));
50
    }
51
}
52