Completed
Push — master ( ff92f6...3ef1d3 )
by John
09:08
created

JwtAuthenticationFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 5
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\JwtBundle\DependencyInjection;
10
11
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
12
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\DefinitionDecorator;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class JwtAuthenticationFactory implements SecurityFactoryInterface
21
{
22
    public function getPosition()
23
    {
24
        return 'pre_auth';
25
    }
26
27
    public function getKey()
28
    {
29
        return 'jwt';
30
    }
31
32
    public function addConfiguration(NodeDefinition $node)
33
    {
34
        $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 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...
35
            ->children()
36
            ->scalarNode('header')->defaultValue('Authorization')->end()
37
            ->end();
38
    }
39
40
    public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
41
    {
42
        $providerId = 'security.authentication.provider.jwt.' . $id;
43
        $container
44
            ->setDefinition($providerId, new DefinitionDecorator('jwt.security.authentication.provider'))
45
            ->replaceArgument(0, new Reference($userProvider));
46
47
        $listenerId = 'security.authentication.listener.jwt.' . $id;
48
        $container->setDefinition($listenerId, new DefinitionDecorator('jwt.security.authentication.listener'));
49
50
        return [$providerId, $listenerId, $defaultEntryPoint];
51
    }
52
53
}
54