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

HttpBasicLdapFactory::createAuthProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 12
loc 12
ccs 0
cts 10
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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