AddFacadePass::process()   B
last analyzed

Complexity

Conditions 8
Paths 13

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 13
nop 1
crap 8
1
<?php
2
3
/*
4
 * This file is part of the FacadeBundle
5
 *
6
 * (c) Indra Gunawan <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace  Indragunawan\FacadeBundle\DependencyInjection\Compiler;
13
14
use Indragunawan\FacadeBundle\AbstractFacade;
15
use Symfony\Component\DependencyInjection\Alias;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * @author Indra Gunawan <[email protected]>
24
 */
25
final class AddFacadePass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 4
    public function process(ContainerBuilder $container)
31
    {
32 4
        $facades = [];
33 4
        foreach ($container->findTaggedServiceIds('indragunawan.facade') as $id => $attr) {
34 3
            $class = $container->getDefinition($id)->getClass();
35 3
            $class = null !== $class ? $class : $id;
36
37 3
            if (!is_subclass_of($class, AbstractFacade::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Indragunawan\FacadeBundle\AbstractFacade::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
38 1
                throw new InvalidArgumentException(sprintf('The service "%s" must extend AbstractFacade.', $class));
39
            }
40
41 2
            $r = new \ReflectionMethod($class, 'getFacadeAccessor');
42 2
            $r->setAccessible(true);
43 2
            $ref = $r->invoke(null);
44
45 2
            if (!\is_string($ref)) {
46 1
                throw new InvalidArgumentException(sprintf('Facade accessor must be string, "%s" given.', \is_object($ref) ? \get_class($ref) : \gettype($ref)));
47
            }
48
49 1
            $ref = \is_string($ref) && 0 === strpos($ref, '@') ? substr($ref, 1) : $ref;
50 1
            $facades[$id] = new Reference($ref);
51
        }
52
53 2
        $container->setAlias('indragunawan.facade.container', new Alias(ServiceLocatorTagPass::register($container, $facades), true));
54 2
    }
55
}
56