Failed Conditions
Push — master ( e23556...5738b4 )
by Florent
01:58
created

HeaderCheckerCompilerPass::addHeaderCheckers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\Checker\DependencyInjection\Compiler;
15
16
use Jose\Component\Checker\HeaderCheckerManagerFactory;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * Class HeaderCheckerCompilerPass.
24
 */
25
final class HeaderCheckerCompilerPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        if (!$container->hasDefinition(HeaderCheckerManagerFactory::class)) {
33
            return;
34
        }
35
36
        $definition = $container->getDefinition(HeaderCheckerManagerFactory::class);
37
        $this->addHeaderCheckers($definition, $container);
38
        $this->addTokenType($definition, $container);
39
    }
40
41
    /**
42
     * @param Definition $definition
43
     * @param ContainerBuilder $container
44
     */
45
    private function addHeaderCheckers(Definition $definition, ContainerBuilder $container)
46
    {
47
        $taggedHeaderCheckerServices = $container->findTaggedServiceIds('jose.checker.header');
48
        foreach ($taggedHeaderCheckerServices as $id => $tags) {
49
            foreach ($tags as $attributes) {
50
                if (!array_key_exists('alias', $attributes)) {
51
                    throw new \InvalidArgumentException(sprintf("The header checker '%s' does not have any 'alias' attribute.", $id));
52
                }
53
                $definition->addMethodCall('add', [$attributes['alias'], new Reference($id)]);
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param Definition $definition
60
     * @param ContainerBuilder $container
61
     */
62
    private function addTokenType(Definition $definition, ContainerBuilder $container)
63
    {
64
        $taggedHeaderCheckerServices = $container->findTaggedServiceIds('jose.checker.token_type');
65
        foreach ($taggedHeaderCheckerServices as $id => $tags) {
66
            $definition->addMethodCall('addTokenTypeSupport', [new Reference($id)]);
67
        }
68
    }
69
}
70