Completed
Pull Request — master (#1)
by one
09:53
created

UserSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 22
dl 0
loc 78
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onFlush() 0 10 3
A __construct() 0 3 1
A getSubscribedEvents() 0 6 1
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\EventSubscriber;
13
14
use App\Entity\User;
15
use Doctrine\Common\EventSubscriber;
16
use Doctrine\ORM\Event\LifecycleEventArgs;
17
use Doctrine\ORM\Event\OnFlushEventArgs;
18
use Doctrine\ORM\Events;
19
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
20
21
class UserSubscriber implements EventSubscriber
22
{
23
    /**
24
     * @var UserPasswordEncoderInterface
25
     */
26
    protected $encoder;
27
28
    /**
29
     * UserSubscriber constructor.
30
     *
31
     * @param UserPasswordEncoderInterface $encoder
32
     */
33
    public function __construct(UserPasswordEncoderInterface $encoder)
34
    {
35
        $this->encoder = $encoder;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getSubscribedEvents(): array
42
    {
43
        return [
44
            Events::prePersist,
45
            Events::postUpdate,
46
            Events::onFlush,
47
        ];
48
    }
49
50
    /**
51
     * @param LifecycleEventArgs $args
52
     */
53
    public function prePersist(LifecycleEventArgs $args): void
54
    {
55
        $user = $args->getEntity();
56
57
        if ($user instanceof User) {
58
            $this->encodePassword($user);
59
        }
60
    }
61
62
    /**
63
     * @param LifecycleEventArgs $args
64
     */
65
    public function postUpdate(LifecycleEventArgs $args): void
66
    {
67
        $user = $args->getEntity();
68
69
        if ($user instanceof User) {
70
            $this->encodePassword($user);
71
        }
72
    }
73
74
    /**
75
     * @param OnFlushEventArgs $eventArgs
76
     */
77
    public function onFlush(OnFlushEventArgs $eventArgs)
78
    {
79
        $em = $eventArgs->getEntityManager();
80
        $uow = $em->getUnitOfWork();
81
        $classMetadata = $em->getClassMetadata(User::class);
82
83
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
84
            if ($entity instanceof User) {
85
                $entity->setUsername($entity->getEmail());
86
                $uow->recomputeSingleEntityChangeSet($classMetadata, $entity);
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param User $user
93
     */
94
    protected function encodePassword(User $user): void
95
    {
96
        $encoded = $this->encoder->encodePassword($user, $user->getPlainPassword());
97
98
        $user->setPassword($encoded);
99
    }
100
}
101