Completed
Push — 1.4-password-hashing-2 ( b79be0 )
by Kamil
16:31
created

UpdateUserEncoderListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B onSecurityInteractiveLogin() 0 29 7
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UserBundle\EventListener;
15
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Component\User\Model\UserInterface;
18
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
19
20
final class UpdateUserEncoderListener
21
{
22
    /** @var ObjectManager */
23
    private $objectManager;
24
25
    /** @var string */
26
    private $recommendedEncoderName;
27
28
    /** @var string */
29
    private $className;
30
31
    /** @var string */
32
    private $interfaceName;
33
34
    /** @var string */
35
    private $passwordParameter;
36
37
    public function __construct(
38
        ObjectManager $objectManager,
39
        string $recommendedEncoderName,
40
        string $className,
41
        string $interfaceName,
42
        string $passwordParameter
43
    ) {
44
        $this->objectManager = $objectManager;
45
        $this->recommendedEncoderName = $recommendedEncoderName;
46
        $this->className = $className;
47
        $this->interfaceName = $interfaceName;
48
        $this->passwordParameter = $passwordParameter;
49
    }
50
51
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
52
    {
53
        $user = $event->getAuthenticationToken()->getUser();
54
55
        if (!$user instanceof UserInterface) {
56
            return;
57
        }
58
59
        if (!$user instanceof $this->className || !$user instanceof $this->interfaceName) {
60
            return;
61
        }
62
63
        if ($user->getEncoderName() === $this->recommendedEncoderName) {
64
            return;
65
        }
66
67
        $request = $event->getRequest();
68
69
        $plainPassword = $request->request->get($this->passwordParameter);
70
        if (null === $plainPassword || '' === $plainPassword) {
71
            return;
72
        }
73
74
        $user->setEncoderName($this->recommendedEncoderName);
75
        $user->setPlainPassword($plainPassword);
76
77
        $this->objectManager->persist($user);
78
        $this->objectManager->flush();
79
    }
80
}
81