UserEntityEventListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
ccs 20
cts 20
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A prePersist() 0 4 1
A preUpdate() 0 4 1
A process() 0 10 2
A changePassword() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Users\EventListener;
6
7
use App\Users\Entity\User;
8
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
9
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
10
11
class UserEntityEventListener
12
{
13
    private $userPasswordEncoder;
14
15 40
    public function __construct(UserPasswordEncoderInterface $userPasswordEncoder)
16
    {
17 40
        $this->userPasswordEncoder = $userPasswordEncoder;
18 40
    }
19
20 34
    public function prePersist(LifecycleEventArgs $event): void
21
    {
22 34
        $this->process($event);
23 34
    }
24
25 12
    public function preUpdate(LifecycleEventArgs $event): void
26
    {
27 12
        $this->process($event);
28 12
    }
29
30 40
    private function process(LifecycleEventArgs $event): void
31
    {
32
        // Get user entity object
33 40
        $user = $event->getObject();
34
35
        // Valid user so lets change password
36 40
        if ($user instanceof User) {
37 9
            $this->changePassword($user);
38
        }
39 40
    }
40
41 9
    private function changePassword(User $user): void
42
    {
43 9
        $plainPassword = $user->getPlainPassword();
44
45 9
        if (!empty($plainPassword)) {
46 8
            $user->setPassword($plainPassword, $this->userPasswordEncoder);
47 8
            $user->eraseCredentials();
48
        }
49 9
    }
50
}
51