Completed
Push — ezp26001-permissions_lookup_re... ( 2e76c0...5b8460 )
by
unknown
25:19
created

OutputVisitorPass::process()   C

Complexity

Conditions 11
Paths 25

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 30
c 1
b 0
f 0
nc 25
nop 1
dl 0
loc 52
rs 5.9999

How to fix   Long Method    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
 * File containing the OutputVisitorPass class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishRestBundle\DependencyInjection\Compiler;
12
13
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Reference;
16
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17
18
/**
19
 * Compiler pass for the ezpublish_rest.output.visitor tag.
20
 *
21
 * Maps an output visitor (json, xml...) to an accept-header
22
 *
23
 * @todo The tag is much more limited in scope than the name shows. Refactor. More ways to map ?
24
 */
25
class OutputVisitorPass implements CompilerPassInterface
26
{
27
    public function process(ContainerBuilder $container)
28
    {
29
        if (!$container->hasDefinition('ezpublish_rest.output.visitor.dispatcher')) {
30
            return;
31
        }
32
33
        $definition = $container->getDefinition('ezpublish_rest.output.visitor.dispatcher');
34
35
        $visitors = [];
36
37
        foreach ($container->findTaggedServiceIds('ezpublish_rest.output.visitor') as $id => $attributes) {
38
            foreach ($attributes as $attribute) {
39
                $priority = isset($attribute['priority']) ? $attribute['priority'] : 0;
40
41
                if (!isset($attribute['regexps'])) {
42
                    throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute to identify the Accept header. None given.');
43
                }
44
45
                if (is_array($attribute['regexps'])) {
46
                    $regexps = $attribute['regexps'];
47
                } elseif (is_string($attribute['regexps'])) {
48
                    try {
49
                        $regexps = $container->getParameter($attribute['regexps']);
50
                    } catch (InvalidArgumentException $e) {
51
                        throw new \LogicException("The regexps attribute of the ezpublish_rest.output.visitor service tag can be a string matching a container parameter name. No parameter {$attribute['regexps']} could be found.");
52
                    }
53
                } else {
54
                    throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute, either as an array or a string. Invalid value.');
55
                }
56
57
                $visitors[$priority][] = [
58
                    'regexps' => $regexps,
59
                    'reference' => new Reference($id),
60
                ];
61
            }
62
        }
63
64
        // sort by priority and flatten
65
        krsort($visitors);
66
        $visitors = call_user_func_array('array_merge', $visitors);
67
68
        foreach ($visitors as $visitor) {
69
            foreach ($visitor['regexps'] as $regexp) {
70
                $definition->addMethodCall(
71
                    'addVisitor', [
72
                        $regexp,
73
                        $visitor['reference'],
74
                    ]
75
                );
76
            }
77
        }
78
    }
79
}
80