InMemoryApiFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 64
ccs 29
cts 29
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 4 1
A addConfiguration() 0 23 1
A create() 0 15 2
1
<?php
2
3
/*
4
 * This file is part of the OsLabSecurityApiBundle package.
5
 *
6
 * (c) OsLab <https://github.com/OsLab>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace OsLab\SecurityApiBundle\DependencyInjection\Security\UserProvider;
13
14
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
15
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\DefinitionDecorator;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * InMemoryApiFactory creates services for the memory api provider.
22
 *
23
 * @author Michael COULLERET <[email protected]>
24
 * @author Florent DESPIERRES <[email protected]>
25
 */
26
class InMemoryApiFactory implements UserProviderFactoryInterface
27
{
28
    /**
29
     * Creates the userProvider.
30
     *
31
     * @param ContainerBuilder $container instance of container
32
     * @param string           $id        the service id for concrete user provider
33
     * @param array            $config    configuration of current factory
34
     */
35 3
    public function create(ContainerBuilder $container, $id, $config)
36
    {
37 3
        $definition = $container->setDefinition($id, new DefinitionDecorator('oslab_security_api.security.user.provider'));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
39 3
        foreach ($config['users'] as $username => $user) {
40 3
            $userId = $id.'_'.$username;
41
42
            $container
43 3
                ->setDefinition($userId, new DefinitionDecorator('security.user.provider.in_memory.user'))
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
44 3
                ->setArguments([$username, (string) $user['password'], $user['roles']])
45
            ;
46
47 3
            $definition->addMethodCall('createUser', [new Reference($userId)]);
48
        }
49 3
    }
50
51
    /**
52
     * Returns the key of current factory.
53
     *
54
     * @return string
55
     */
56 3
    public function getKey()
57
    {
58 3
        return 'memory_api';
59
    }
60
61
    /**
62
     * Adds configuration nodes to current user provider.
63
     *
64
     * @param NodeDefinition $node
65
     */
66 3
    public function addConfiguration(NodeDefinition $node)
67
    {
68
        $node
69 3
            ->fixXmlConfig('user')
70 3
            ->children()
71 3
                ->arrayNode('users')
72 3
                    ->useAttributeAsKey('name')
73 3
                    ->prototype('array')
74 3
                        ->children()
75 3
                            ->scalarNode('password')->defaultValue(uniqid('', true))->end()
76 3
                            ->arrayNode('roles')
77 3
                                ->beforeNormalization()->ifString()->then(function ($v) {
78
                                    return preg_split('/\s*,\s*/', $v);
79 3
                                })
80 3
                                ->end()
81 3
                                ->prototype('scalar')->end()
82 3
                            ->end()
83 3
                        ->end()
84 3
                    ->end()
85 3
                ->end()
86 3
            ->end()
87
        ;
88 3
    }
89
}
90