Failed Conditions
Pull Request — master (#125)
by Florent
21:10
created

OAuth2FrameworkSecurityExtension::load()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 4
nop 2
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\SecurityBundle\DependencyInjection;
15
16
use OAuth2Framework\Component\BearerTokenType\BearerToken;
17
use OAuth2Framework\Component\Core\AccessToken\AccessTokenHandler;
18
use OAuth2Framework\Component\MacTokenType\MacToken;
19
use OAuth2Framework\SecurityBundle\Annotation\Checker\Checker;
20
use Symfony\Component\Config\Definition\Processor;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
24
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
25
26
final class OAuth2FrameworkSecurityExtension extends Extension
27
{
28
    /**
29
     * @var string
30
     */
31
    private $alias;
32
33
    /**
34
     * OAuth2FrameworkSecurityExtension constructor.
35
     *
36
     * @param string $alias
37
     */
38
    public function __construct(string $alias)
39
    {
40
        $this->alias = $alias;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getAlias()
47
    {
48
        return $this->alias;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function load(array $configs, ContainerBuilder $container)
55
    {
56
        $processor = new Processor();
57
        $config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Bug introduced by
It seems like $this->getConfiguration($configs, $container) can be null; however, processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
59
        $container->setAlias('oauth2_security.psr7_message_factory', $config['psr7_message_factory']);
60
61
        $container->registerForAutoconfiguration(Checker::class)->addTag('oauth2_security_annotation_checker');
62
        $container->registerForAutoconfiguration(AccessTokenHandler::class)->addTag('oauth2_security_token_handler');
63
64
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/'));
65
        $loader->load('security.php');
66
67
        if (class_exists(BearerToken::class) && $config['bearer_token']['enabled']) {
68
            $container->setParameter('oauth2_security.token_type.bearer_token.realm', $config['bearer_token']['realm']);
69
            $container->setParameter('oauth2_security.token_type.bearer_token.authorization_header', $config['bearer_token']['authorization_header']);
70
            $container->setParameter('oauth2_security.token_type.bearer_token.request_body', $config['bearer_token']['request_body']);
71
            $container->setParameter('oauth2_security.token_type.bearer_token.query_string', $config['bearer_token']['query_string']);
72
            $loader->load('bearer_token.php');
73
        }
74
        if (class_exists(MacToken::class) && $config['mac_token']['enabled']) {
75
            $container->setParameter('oauth2_security.token_type.mac_token.min_length', $config['mac_token']['min_length']);
76
            $container->setParameter('oauth2_security.token_type.mac_token.max_length', $config['mac_token']['max_length']);
77
            $container->setParameter('oauth2_security.token_type.mac_token.algorithm', $config['mac_token']['algorithm']);
78
            $container->setParameter('oauth2_security.token_type.mac_token.timestamp_lifetime', $config['mac_token']['timestamp_lifetime']);
79
            $loader->load('mac_token.php');
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
87
    {
88
        return new Configuration($this->getAlias());
89
    }
90
}
91