JWKSetSource::prepend()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\DependencyInjection\Source;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
19
final class JWKSetSource implements SourceInterface
20
{
21
    /**
22
     * @var null|\SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource\JWKSetSourceInterface[]
23
     */
24
    private $jwkset_sources = null;
25
26
    /**
27
     * @var string
28
     */
29
    private $bundle_path = null;
30
31
    /**
32
     * JWKSetSource constructor.
33
     *
34
     * @param string $bundle_path
35
     */
36
    public function __construct($bundle_path)
37
    {
38
        $this->bundle_path = $bundle_path;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getName()
45
    {
46
        return 'key_sets';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function createService($name, array $config, ContainerBuilder $container)
53
    {
54
        foreach ($config as $key => $adapter) {
55
            if (array_key_exists($key, $this->getJWKSetSources())) {
56
                $this->getJWKSetSources()[$key]->create($container, 'key_set', $name, $adapter);
57
58
                return;
59
            }
60
        }
61
62
        throw new \LogicException(sprintf('The JWKSet definition "%s" is not configured.', $name));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getNodeDefinition(ArrayNodeDefinition $node)
69
    {
70
        $sourceNodeBuilder = $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method performNoDeepMerging() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
71
            ->children()
72
                ->arrayNode('key_sets')
73
                    ->useAttributeAsKey('name')
74
                    ->prototype('array')
75
                        ->performNoDeepMerging()
76
                        ->children();
77
        foreach ($this->getJWKSetSources() as $name => $source) {
78
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
79
            $source->addConfiguration($sourceNode);
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function prepend(ContainerBuilder $container, array $config)
87
    {
88
    }
89
90
    /**
91
     * @return array|\SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource\JWKSetSourceInterface[]
92
     */
93
    private function getJWKSetSources()
94
    {
95
        if (null !== $this->jwkset_sources) {
96
            return $this->jwkset_sources;
97
        }
98
99
        // load bundled adapter factories
100
        $tempContainer = new ContainerBuilder();
101
        $loader = new XmlFileLoader($tempContainer, new FileLocator($this->bundle_path.'/Resources/config'));
102
        $loader->load('jwkset_sources.xml');
103
104
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_set_source');
105
        $jwkset_sources = [];
106
        foreach (array_keys($services) as $id) {
107
            $factory = $tempContainer->get($id);
108
            $jwkset_sources[str_replace('-', '_', $factory->getKeySet())] = $factory;
109
        }
110
111
        return $this->jwkset_sources = $jwkset_sources;
112
    }
113
}
114