Completed
Push — symfony3-fqcn-sylius-3 ( 1d8d47...2ae323 )
by Kamil
38:58 queued 18:19
created

SyliusUserExtension::getMetadataDefinitionFromAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\UserBundle\DependencyInjection;
13
14
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
15
use Sylius\Bundle\UserBundle\EventListener\UserDeleteListener;
16
use Sylius\Bundle\UserBundle\EventListener\UserLastLoginSubscriber;
17
use Sylius\Bundle\UserBundle\EventListener\UserReloaderListener;
18
use Sylius\Bundle\UserBundle\Provider\AbstractUserProvider;
19
use Sylius\Bundle\UserBundle\Provider\EmailProvider;
20
use Sylius\Bundle\UserBundle\Provider\UsernameOrEmailProvider;
21
use Sylius\Bundle\UserBundle\Provider\UsernameProvider;
22
use Sylius\Bundle\UserBundle\Reloader\UserReloader;
23
use Sylius\Component\User\Security\Checker\TokenUniquenessChecker;
24
use Sylius\Component\User\Security\Generator\UniquePinGenerator;
25
use Sylius\Component\User\Security\Generator\UniqueTokenGenerator;
26
use Symfony\Component\Config\FileLocator;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\DependencyInjection\Definition;
29
use Symfony\Component\DependencyInjection\DefinitionDecorator;
30
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
31
use Symfony\Component\DependencyInjection\Reference;
32
33
/**
34
 * @author Łukasz Chruściel <[email protected]>
35
 */
36
final class SyliusUserExtension extends AbstractResourceExtension
37
{
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function load(array $config, ContainerBuilder $container)
42
    {
43
        $config = $this->processConfiguration($this->getConfiguration($config, $container), $config);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($config, $container) is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
45
46
        $loader->load(sprintf('services/integrations/%s.xml', $config['driver']));
47
48
        $this->registerResources('sylius', $config['driver'], $this->resolveResources($config['resources'], $container), $container);
49
50
        $loader->load('services.xml');
51
52
        $this->createServices($config['resources'], $container);
53
    }
54
55
    /**
56
     * @param array $resources
57
     * @param ContainerBuilder $container
58
     *
59
     * @return array
60
     */
61
    private function resolveResources(array $resources, ContainerBuilder $container)
62
    {
63
        $container->setParameter('sylius.user.users', $resources);
64
65
        $resolvedResources = [];
66
        foreach ($resources as $variableName => $variableConfig) {
67
            foreach ($variableConfig as $resourceName => $resourceConfig) {
68
                if (is_array($resourceConfig)) {
69
                    $resolvedResources[$variableName.'_'.$resourceName] = $resourceConfig;
70
                }
71
            }
72
        }
73
74
        return $resolvedResources;
75
    }
76
77
    /**
78
     * @param array $resources
79
     * @param ContainerBuilder $container
80
     */
81
    private function createServices(array $resources, ContainerBuilder $container)
82
    {
83
        foreach ($resources as $userType => $config) {
84
            $this->createTokenGenerators($userType, $config['user'], $container);
85
            $this->createReloaders($userType, $container);
86
            $this->createLastLoginListeners($userType, $container);
87
            $this->createProviders($userType, $config['user']['classes']['model'], $container);
88
            $this->createUserDeleteListeners($userType, $container);
89
        }
90
    }
91
92
    /**
93
     * @param string $userType
94
     * @param array $config
95
     * @param ContainerBuilder $container
96
     */
97
    private function createTokenGenerators($userType, array $config, ContainerBuilder $container)
98
    {
99
        $this->createUniquenessCheckers($userType, $config, $container);
100
101
        $container->setDefinition(
102
            sprintf('sylius.%s_user.token_generator.password_reset', $userType),
103
            $this->createTokenGeneratorDefinition(
104
                UniqueTokenGenerator::class,
105
                [
106
                    new Reference(sprintf('sylius.%s_user.token_uniqueness_checker.password_reset', $userType)),
107
                    $config['resetting']['token']['length']
108
                ]
109
            )
110
        );
111
112
        $container->setDefinition(
113
            sprintf('sylius.%s_user.pin_generator.password_reset', $userType),
114
            $this->createTokenGeneratorDefinition(
115
                UniquePinGenerator::class,
116
                [
117
                    new Reference(sprintf('sylius.%s_user.pin_uniqueness_checker.password_reset', $userType)),
118
                    $config['resetting']['pin']['length']
119
                ]
120
            )
121
        );
122
123
        $container->setDefinition(
124
            sprintf('sylius.%s_user.token_generator.email_verification', $userType),
125
            $this->createTokenGeneratorDefinition(
126
                UniqueTokenGenerator::class,
127
                [
128
                    new Reference(sprintf('sylius.%s_user.token_uniqueness_checker.email_verification', $userType)),
129
                    $config['verification']['token']['length']
130
                ]
131
            )
132
        );
133
    }
134
135
    /**
136
     * @param string $generatorClass
137
     * @param array $arguments
138
     *
139
     * @return Definition
140
     */
141
    private function createTokenGeneratorDefinition($generatorClass, array $arguments)
142
    {
143
        $generatorDefinition = new Definition($generatorClass);
144
        $generatorDefinition->setArguments($arguments);
145
146
        return $generatorDefinition;
147
    }
148
149
    /**
150
     * @param string $userType
151
     * @param array $config
152
     * @param ContainerBuilder $container
153
     */
154
    private function createUniquenessCheckers($userType, array $config, ContainerBuilder $container)
155
    {
156
        $repositoryServiceId = sprintf('sylius.repository.%s_user', $userType);
157
158
        $resetPasswordTokenUniquenessCheckerDefinition = new Definition(TokenUniquenessChecker::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resetPasswordTokenUniquenessCheckerDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
159
        $resetPasswordTokenUniquenessCheckerDefinition->addArgument(new Reference($repositoryServiceId));
160
        $resetPasswordTokenUniquenessCheckerDefinition->addArgument($config['resetting']['token']['field_name']);
161
        $container->setDefinition(
162
            sprintf('sylius.%s_user.token_uniqueness_checker.password_reset', $userType),
163
            $resetPasswordTokenUniquenessCheckerDefinition
164
        );
165
166
        $resetPasswordPinUniquenessCheckerDefinition = new Definition(TokenUniquenessChecker::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resetPasswordPinUniquenessCheckerDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
167
        $resetPasswordPinUniquenessCheckerDefinition->addArgument(new Reference($repositoryServiceId));
168
        $resetPasswordPinUniquenessCheckerDefinition->addArgument($config['resetting']['pin']['field_name']);
169
        $container->setDefinition(
170
            sprintf('sylius.%s_user.pin_uniqueness_checker.password_reset', $userType),
171
            $resetPasswordPinUniquenessCheckerDefinition
172
        );
173
174
        $emailVerificationTokenUniquenessCheckerDefinition = new Definition(TokenUniquenessChecker::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $emailVerificationTokenUniquenessCheckerDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
175
        $emailVerificationTokenUniquenessCheckerDefinition->addArgument(new Reference($repositoryServiceId));
176
        $emailVerificationTokenUniquenessCheckerDefinition->addArgument($config['verification']['token']['field_name']);
177
        $container->setDefinition(
178
            sprintf('sylius.%s_user.token_uniqueness_checker.email_verification', $userType),
179
            $emailVerificationTokenUniquenessCheckerDefinition
180
        );
181
    }
182
183
    /**
184
     * @param string $userType
185
     * @param ContainerBuilder $container
186
     */
187
    private function createReloaders($userType, ContainerBuilder $container)
188
    {
189
        $managerServiceId = sprintf('sylius.manager.%s_user', $userType);
190
        $reloaderServiceId = sprintf('sylius.%s_user.reloader', $userType);
191
        $reloaderListenerServiceId = sprintf('sylius.listener.%s_user.reloader', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $reloaderListenerServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
192
193
        $userReloaderDefinition = new Definition(UserReloader::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $userReloaderDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
194
        $userReloaderDefinition->addArgument(new Reference($managerServiceId));
195
        $container->setDefinition($reloaderServiceId, $userReloaderDefinition);
196
197
        $userReloaderListenerDefinition = new Definition(UserReloaderListener::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $userReloaderListenerDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
198
        $userReloaderListenerDefinition->addArgument(new Reference($reloaderServiceId));
199
        $userReloaderListenerDefinition->addTag('kernel.event_listener', ['event' => sprintf('sylius.%s_user.post_create', $userType), 'method' => 'reloadUser']);
200
        $userReloaderListenerDefinition->addTag('kernel.event_listener', ['event' => sprintf('sylius.%s_user.post_update', $userType), 'method' => 'reloadUser']);
201
        $container->setDefinition($reloaderListenerServiceId, $userReloaderListenerDefinition);
202
    }
203
204
    /**
205
     * @param string $userType
206
     * @param ContainerBuilder $container
207
     */
208
    private function createLastLoginListeners($userType, ContainerBuilder $container)
209
    {
210
        $managerServiceId = sprintf('sylius.manager.%s_user', $userType);
211
        $lastLoginListenerServiceId = sprintf('sylius.listener.%s_user_last_login', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $lastLoginListenerServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
212
213
        $lastLoginListenerDefinition = new Definition(UserLastLoginSubscriber::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $lastLoginListenerDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
214
        $lastLoginListenerDefinition->addArgument(new Reference($managerServiceId));
215
        $lastLoginListenerDefinition->addTag('kernel.event_subscriber');
216
        $container->setDefinition($lastLoginListenerServiceId, $lastLoginListenerDefinition);
217
    }
218
219
    /**
220
     * @param string $userType
221
     * @param ContainerBuilder $container
222
     */
223
    public function createUserDeleteListeners($userType, ContainerBuilder $container)
224
    {
225
        $userDeleteListenerServiceId = sprintf('sylius.listener.%s_user_delete', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $userDeleteListenerServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
226
        $userPreDeleteEventName = sprintf('sylius.%s_user.pre_delete', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $userPreDeleteEventName exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
227
228
        $userDeleteListenerDefinition = new Definition(UserDeleteListener::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $userDeleteListenerDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
229
        $userDeleteListenerDefinition->addArgument(new Reference('security.token_storage'));
230
        $userDeleteListenerDefinition->addArgument(new Reference('session'));
231
        $userDeleteListenerDefinition->addTag('kernel.event_listener', ['event' => $userPreDeleteEventName, 'method' => 'deleteUser']);
232
        $container->setDefinition($userDeleteListenerServiceId, $userDeleteListenerDefinition);
233
    }
234
235
    /**
236
     * @param string $userType
237
     * @param string $userModel
238
     * @param ContainerBuilder $container
239
     */
240
    private function createProviders($userType, $userModel, ContainerBuilder $container)
241
    {
242
        $repositoryServiceId = sprintf('sylius.repository.%s_user', $userType);
243
        $abstractProviderServiceId = sprintf('sylius.%s_user_provider', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $abstractProviderServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
244
        $providerEmailBasedServiceId = sprintf('sylius.%s_user_provider.email_based', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $providerEmailBasedServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
245
        $providerNameBasedServiceId = sprintf('sylius.%s_user_provider.name_based', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $providerNameBasedServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
246
        $providerEmailOrNameBasedServiceId = sprintf('sylius.%s_user_provider.email_or_name_based', $userType);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $providerEmailOrNameBasedServiceId exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
247
248
        $abstractProviderDefinition = new Definition(AbstractUserProvider::class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $abstractProviderDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
249
        $abstractProviderDefinition->setAbstract(true);
250
        $abstractProviderDefinition->setLazy(true);
251
        $abstractProviderDefinition->addArgument($userModel);
252
        $abstractProviderDefinition->addArgument(new Reference($repositoryServiceId));
253
        $abstractProviderDefinition->addArgument(new Reference('sylius.canonicalizer'));
254
        $container->setDefinition($abstractProviderServiceId, $abstractProviderDefinition);
255
256
        $emailBasedProviderDefinition = new DefinitionDecorator($abstractProviderServiceId);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $emailBasedProviderDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
257
        $emailBasedProviderDefinition->setClass(EmailProvider::class);
258
        $container->setDefinition($providerEmailBasedServiceId, $emailBasedProviderDefinition);
259
260
        $nameBasedProviderDefinition = new DefinitionDecorator($abstractProviderServiceId);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $nameBasedProviderDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
261
        $nameBasedProviderDefinition->setClass(UsernameProvider::class);
262
        $container->setDefinition($providerNameBasedServiceId, $nameBasedProviderDefinition);
263
264
        $emailOrNameBasedProviderDefinition = new DefinitionDecorator($abstractProviderServiceId);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $emailOrNameBasedProviderDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
265
        $emailOrNameBasedProviderDefinition->setClass(UsernameOrEmailProvider::class);
266
        $container->setDefinition($providerEmailOrNameBasedServiceId, $emailOrNameBasedProviderDefinition);
267
    }
268
}
269