KeyCollectorCompilerPass::collectServices()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\JoseFramework\DependencyInjection\Compiler;
15
16
use Jose\Bundle\JoseFramework\DataCollector\KeyCollector;
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
final class KeyCollectorCompilerPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container): void
28
    {
29
        if (!$container->hasDefinition(KeyCollector::class)) {
30
            return;
31
        }
32
33
        $definition = $container->getDefinition(KeyCollector::class);
34
35
        $services = [
36
            'addJWK' => 'jose.jwk',
37
            'addJWKSet' => 'jose.jwkset',
38
        ];
39
        foreach ($services as $method => $tag) {
40
            $this->collectServices($method, $tag, $definition, $container);
41
        }
42
    }
43
44
    private function collectServices(string $method, string $tag, Definition $definition, ContainerBuilder $container): void
45
    {
46
        $taggedJWSServices = $container->findTaggedServiceIds($tag);
47
        foreach ($taggedJWSServices as $id => $tags) {
48
            $definition->addMethodCall($method, [$id, new Reference($id)]);
49
        }
50
    }
51
}
52