Failed Conditions
Push — master ( 9e8534...2df391 )
by Florent
04:31
created

ScopePolicyCompilerPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 30 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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\ServerBundle\Component\Scope\Compiler;
15
16
use OAuth2Framework\Component\Scope\Policy\ScopePolicyManager;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class ScopePolicyCompilerPass implements CompilerPassInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function process(ContainerBuilder $container)
27
    {
28
        if (!$container->hasDefinition(ScopePolicyManager::class)) {
29
            return;
30
        }
31
32
        $definition = $container->getDefinition(ScopePolicyManager::class);
33
        $default = $container->getParameter('oauth2_server.scope.policy.by_default');
34
35
        $taggedServices = $container->findTaggedServiceIds('oauth2_server_scope_policy');
36
        $default_found = false;
37
        $policy_names = [];
38
        foreach ($taggedServices as $id => $tags) {
39
            foreach ($tags as $attributes) {
40
                if (!array_key_exists('policy_name', $attributes)) {
41
                    throw new \InvalidArgumentException(sprintf('The scope policy "%s" does not have any "policy_name" attribute.', $id));
42
                }
43
                $is_default = $default === $attributes['policy_name'];
44
                $policy_names[] = $attributes['policy_name'];
45
                if (true === $is_default) {
46
                    $default_found = true;
47
                }
48
                $definition->addMethodCall('add', [new Reference($id), $is_default]);
49
            }
50
        }
51
52
        if (!$default_found) {
53
            throw new \InvalidArgumentException(sprintf('Unable to find the scope policy "%s". Available policies are: %s.', $default, implode(', ', $policy_names)));
54
        }
55
    }
56
}
57