Completed
Push — ezp26188-render_field_twig_glo... ( 6b91c6...a62256 )
by
unknown
37:57 queued 06:15
created

OutputVisitorPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 0 35 9
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
        foreach ($container->findTaggedServiceIds('ezpublish_rest.output.visitor') as $id => $attributes) {
36
            foreach ($attributes as $attribute) {
37
                if (!isset($attribute['regexps'])) {
38
                    throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute to identify the Accept header. None given.');
39
                }
40
41
                if (is_array($attribute['regexps'])) {
42
                    $regexps = $attribute['regexps'];
43
                } elseif (is_string($attribute['regexps'])) {
44
                    try {
45
                        $regexps = $container->getParameter($attribute['regexps']);
46
                    } catch (InvalidArgumentException $e) {
47
                        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.");
48
                    }
49
                } else {
50
                    throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute, either as an array or a string. Invalid value.');
51
                }
52
53
                foreach ($regexps as $regexp) {
54
                    $definition->addMethodCall(
55
                        'addVisitor',
56
                        array($regexp, new Reference($id))
57
                    );
58
                }
59
            }
60
        }
61
    }
62
}
63