Failed Conditions
Pull Request — master (#37)
by Florent
08:00 queued 03:46
created

PKCEMethodCompilerPass::process()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 7
nop 1
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 OAuth2Framework\Bundle\Server\DependencyInjection\Compiler;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Bundle\Server\Service\MetadataBuilder;
18
use OAuth2Framework\Component\Server\GrantType\PKCEMethod\PKCEMethodManager;
19
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
final class PKCEMethodCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        if (!$container->hasDefinition(PKCEMethodManager::class)) {
31
            return;
32
        }
33
34
        $definition = $container->getDefinition(PKCEMethodManager::class);
35
36
        $taggedServices = $container->findTaggedServiceIds('oauth2_server_pkce_method');
37
        $loaded = [];
38
        foreach ($taggedServices as $id => $tags) {
39
            foreach ($tags as $attributes) {
40
                Assertion::keyExists($attributes, 'alias', sprintf('The PKCE method  "%s" does not have any "alias" attribute.', $id));
41
                $loaded[] = $attributes['alias'];
42
                $definition->addMethodCall('add', [new Reference($id)]);
43
            }
44
        }
45
46
        if (!$container->hasDefinition(MetadataBuilder::class)) {
47
            return;
48
        }
49
50
        $definition = $container->getDefinition(MetadataBuilder::class);
51
        $definition->addMethodCall('setCodeChallengeMethodsSupported', [new Reference(PKCEMethodManager::class)]);
52
    }
53
}
54