Failed Conditions
Push — master ( 28d61e...98c33a )
by Florent
03:29
created

Source/KeyManagement/JWKSetSource/JKU.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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\JKUFactory;
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 JKU 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(JKUFactory::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 'jku';
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