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

AddMiddlewarePass::process()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 10
nop 1
dl 0
loc 31
ccs 0
cts 28
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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