Failed Conditions
Push — v7 ( 477009...5356df )
by Florent
03:33
created

AbstractJWKSetSource   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 2
A addConfiguration() 0 15 1
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 Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSetSource;
13
14
use Jose\Bundle\JoseFramework\Controller\JWKSetController;
15
use Jose\Bundle\JoseFramework\Controller\JWKSetControllerFactory;
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\AbstractSource;
17
use Jose\Bundle\JoseFramework\Routing\JWKSetLoader;
18
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
abstract class AbstractJWKSetSource extends AbstractSource implements JWKSetSourceInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function create(ContainerBuilder $container, string $type, string $name, array $config)
29
    {
30
        parent::create($container, $type, $name, $config);
31
32
        if (null !== $config['path']) {
33
            $jwkset_id = sprintf('jose.key_set.%s', $name);
34
            $controller_definition = new Definition(JWKSetController::class);
35
            $controller_definition->setFactory([new Reference(JWKSetControllerFactory::class), 'create']);
36
            $controller_definition->setArguments([new Reference($jwkset_id), $config['max_age']]);
37
            $controller_id = sprintf('jose.controller.%s', $name);
38
            $container->setDefinition($controller_id, $controller_definition);
39
40
            $jwkset_loader_definition = $container->getDefinition(JWKSetLoader::class);
41
            $jwkset_loader_definition->addMethodCall('add', [$config['path'], $config['max_age'], $name]);
42
        }
43
    }
44
45
    /**
46
     * @param NodeDefinition $node
47
     */
48
    public function addConfiguration(NodeDefinition $node)
49
    {
50
        parent::addConfiguration($node);
51
        $node
1 ignored issue
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 children() 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...
52
            ->children()
53
                ->scalarNode('path')
54
                    ->info('To share the JWKSet, then set a valid path (e.g. "/jwkset.json").')
55
                    ->defaultNull()
56
                ->end()
57
                ->integerNode('max_age')
58
                    ->info('When share, this value indicates when the HTTP client should keep the key in cache. Default is 21600 = 6 hours.')
59
                    ->defaultValue(21600)
60
                ->end()
61
            ->end();
62
    }
63
}
64