Completed
Pull Request — master (#1)
by Indra
02:41
created

AddMiddlewarePass   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 36
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 31 10
1
<?php
2
3
/*
4
 * This file is part of the MiddlewareBundle
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\MiddlewareBundle\DependencyInjection\Compiler;
13
14
use Indragunawan\MiddlewareBundle\Middleware\MiddlewareInterface;
15
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * @author Indra Gunawan <[email protected]>
23
 */
24
final class AddMiddlewarePass implements CompilerPassInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function process(ContainerBuilder $container)
30
    {
31
        if (!$container->hasDefinition('indragunawan_middleware.middleware')) {
32
            return;
33
        }
34
35
        $definition = $container->findDefinition('indragunawan_middleware.middleware');
36
        foreach ($container->findTaggedServiceIds('indragunawan.middleware') as $id => $attrs) {
37
            foreach ($attrs as $attr) {
38
                $class = $container->getDefinition($id)->getClass();
39
                if (!$r = $container->getReflectionClass($class)) {
40
                    throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
41
                } elseif (!$r->isSubclassOf(MiddlewareInterface::class)) {
42
                    throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, MiddlewareInterface::class));
43
                } elseif (empty($attr['event'])) {
44
                    throw new InvalidArgumentException('"event" tag attribute must be set');
45
                }
46
47
                $priority = 0;
48
                $name = null;
49
                $supports = forward_static_call([$class, 'supports']);
50
                if (\is_string($supports)) {
51
                    $name = $supports;
52
                } elseif (\is_string($supports[0])) {
53
                    $name = $supports[0];
54
                    $priority = isset($supports[1]) ? $supports[1] : 0;
55
                } else {
56
                    throw InvalidArgumentException(sprintf('Invalid return format "supports" of class "%s".', $class));
0 ignored issues
show
Bug introduced by
The function InvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
                    throw /** @scrutinizer ignore-call */ InvalidArgumentException(sprintf('Invalid return format "supports" of class "%s".', $class));
Loading history...
57
                }
58
59
                $definition->addMethodCall('addMiddleware', [$attr['event'], $name, new ServiceClosureArgument(new Reference($id)), $priority]);
60
            }
61
        }
62
    }
63
}
64