Passed
Branch master (022a1c)
by Darwin
03:39
created

HttpBasicLdapFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 31.08 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 23
loc 74
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createEntryPoint() 0 13 2
A getPosition() 0 3 1
A addConfiguration() 0 7 1
A createListener() 9 9 1
A createAuthProvider() 12 12 1
A create() 0 12 1
A getKey() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace DoL\LdapBundle\Security\Factory;
4
5
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\ChildDefinition;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
/**
12
 * HTTP Basic factory for Ldap.
13
 *
14
 * @author DarwinOnLine
15
 * @author Maks3w
16
 *
17
 * @see https://github.com/DarwinOnLine/DoLLdapBundle
18
 */
19
class HttpBasicLdapFactory implements SecurityFactoryInterface
20
{
21
    public function create(ContainerBuilder $container, $id, $config, $userProviderId, $defaultEntryPointId)
22
    {
23
        // authentication provider
24
        $authProviderId = $this->createAuthProvider($container, $id, $userProviderId);
25
26
        // entry point
27
        $entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPointId);
28
29
        // authentication listener
30
        $listenerId = $this->createListener($container, $id, $entryPointId);
31
32
        return [$authProviderId, $listenerId, $entryPointId];
33
    }
34
35
    public function getPosition()
36
    {
37
        return 'http';
38
    }
39
40
    public function getKey()
41
    {
42
        return 'dol_ldap_httpbasic';
43
    }
44
45
    public function addConfiguration(NodeDefinition $node)
46
    {
47
        $node
48
            ->children()
49
            ->scalarNode('provider')->end()
50
            ->scalarNode('realm')->defaultValue('Secured Area')->end()
51
            ->end()
52
        ;
53
    }
54
55 View Code Duplication
    protected function createAuthProvider(ContainerBuilder $container, $id, $userProviderId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $provider = 'dol_ldap.security.authentication.provider';
58
        $providerId = $provider.'.'.$id;
59
60
        $container
61
            ->setDefinition($providerId, new ChildDefinition($provider))
62
            ->replaceArgument(1, $id) // Provider Key
63
            ->replaceArgument(2, new Reference($userProviderId)) // User Provider
64
        ;
65
66
        return $providerId;
67
    }
68
69 View Code Duplication
    protected function createListener(ContainerBuilder $container, $id, $entryPointId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        // listener
72
        $listenerId = 'security.authentication.listener.basic.'.$id;
73
        $listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.basic'));
74
        $listener->replaceArgument(2, $id);
75
        $listener->replaceArgument(3, new Reference($entryPointId));
76
77
        return $listenerId;
78
    }
79
80
    protected function createEntryPoint(ContainerBuilder $container, $id, $config, $defaultEntryPoint)
81
    {
82
        if (null !== $defaultEntryPoint) {
83
            return $defaultEntryPoint;
84
        }
85
86
        $entryPointId = 'security.authentication.basic_entry_point.'.$id;
87
        $container
88
            ->setDefinition($entryPointId, new ChildDefinition('security.authentication.basic_entry_point'))
89
            ->addArgument($config['realm'])
90
        ;
91
92
        return $entryPointId;
93
    }
94
}
95