Failed Conditions
Push — master ( d2ad41...5d048e )
by Florent
21:38 queued 15:06
created

X5U::getKeySet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Jose\Bundle\JoseFramework\DependencyInjection\Source\KeyManagement\JWKSetSource;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\AbstractSource;
17
use Jose\Component\Core\JWKSet;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Jose\Bundle\JoseFramewor...ent\JWKSetSource\JWKSet.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Jose\Component\KeyManagement\X5UFactory;
19
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
class X5U extends AbstractSource implements JWKSetSource
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function createDefinition(ContainerBuilder $container, array $config): Definition
30
    {
31
        $definition = new Definition(JWKSet::class);
32
        $definition->setFactory([
33
            new Reference(X5UFactory::class),
34
            'loadFromUrl',
35
        ]);
36
        $definition->setArguments([
37
            $config['url'],
38
            $config['headers'],
39
        ]);
40
        $definition->addTag('jose.jwkset');
41
42
        return $definition;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getKeySet(): string
49
    {
50
        return 'x5u';
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function addConfiguration(NodeDefinition $node)
57
    {
58
        parent::addConfiguration($node);
59
        $node
60
            ->children()
61
                ->scalarNode('url')
62
                    ->info('URL of the key set.')
63
                    ->isRequired()
64
                ->end()
65
                ->arrayNode('headers')
66
                    ->treatNullLike([])
67
                    ->treatFalseLike([])
68
                    ->info('Header key/value pairs added to the request.')
69
                    ->useAttributeAsKey('name')
70
                    ->variablePrototype()->end()
71
                ->end()
72
            ->end();
73
    }
74
}
75