Failed Conditions
Push — master ( 9eeb29...881d26 )
by Florent
16:44
created

OAuth2FrameworkExtension::prependDoctrineTypes()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
cc 6
nc 6
nop 1
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\DependencyInjection;
15
16
use OAuth2Framework\ServerBundle\Component\Component;
17
use OAuth2Framework\ServerBundle\Doctrine\Type as DbalType;
18
use Symfony\Component\Config\Definition\Processor;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22
23
final class OAuth2FrameworkExtension extends Extension implements PrependExtensionInterface
24
{
25
    /**
26
     * @var Component[]
27
     */
28
    private $components;
29
30
    private $alias;
31
32
    /**
33
     * @param Component[] $components
34
     */
35
    public function __construct(string $alias, array $components)
36
    {
37
        $this->alias = $alias;
38
        $this->components = $components;
39
    }
40
41
    public function getAlias()
42
    {
43
        return $this->alias;
44
    }
45
46
    public function load(array $configs, ContainerBuilder $container)
47
    {
48
        $processor = new Processor();
49
        $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...
50
51
        foreach ($this->components as $component) {
52
            $component->load($config, $container);
53
        }
54
    }
55
56
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
57
    {
58
        return new Configuration($this->getAlias(), $this->components);
59
    }
60
61
    public function prepend(ContainerBuilder $container)
62
    {
63
        $this->prependComponents($container);
64
        $this->prependDoctrineTypes($container);
65
    }
66
67
    private function prependComponents(ContainerBuilder $container): void
68
    {
69
        $configs = $container->getExtensionConfig($this->getAlias());
70
        $config = $this->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...
71
72
        foreach ($this->components as $component) {
73
            $result = $component->prepend($container, $config);
74
            if (!empty($result)) {
75
                $container->prependExtensionConfig($this->getAlias(), $result);
76
            }
77
        }
78
    }
79
80
    private function prependDoctrineTypes(ContainerBuilder $container): void
81
    {
82
        $bundles = $container->getParameter('kernel.bundles');
83
        if (!\is_array($bundles) || !array_key_exists('DoctrineBundle', $bundles)) {
84
            return;
85
        }
86
        $configs = $container->getExtensionConfig('doctrine');
87
        if (empty($configs)) {
88
            return;
89
        }
90
91
        $config = current($configs);
92
        if (!isset($config['dbal'])) {
93
            $config['dbal'] = [];
94
        }
95
        if (!isset($config['dbal']['types'])) {
96
            $config['dbal']['types'] = [];
97
        }
98
        $config['dbal']['types'] += [
99
            'client_id' => DbalType\ClientIdType::class,
100
            'access_token_id' => DbalType\AccessTokenIdType::class,
101
            'user_account_id' => DbalType\UserAccountIdType::class,
102
            'resourc_server_id' => DbalType\ResourceServerIdType::class,
103
            'databag' => DbalType\DataBagType::class,
104
        ];
105
106
        $container->prependExtensionConfig('doctrine', $config);
107
    }
108
}
109