UserAccountSubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 26
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber\User;
4
5
use App\Entity\User;
6
use App\Event\User\UserChangepasswordEvent;
7
use App\Event\User\AbstractUserEvent;
8
use App\Event\User\UserForgotpasswordEvent;
9
use App\Event\User\AbstractUserPasswordEvent;
10
use App\Event\User\UserRegisteredEvent;
11
use App\Event\User\UserResetpasswordEvent;
12
use App\Event\User\UserUpdateAccountEvent;
13
use App\FlashMessage\FlashMessageCategory;
14
use App\Mail\SendMail;
15
use App\Security\UserSetHash;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
18
19
20
class UserAccountSubscriber extends AbstractUserSubscriber implements EventSubscriberInterface
21
{
22
23
    /**
24
     * @var UserPasswordEncoderInterface
25
     */
26
    private $passwordEncoder;
27
    /**
28
     * @var UserSetHash
29
     */
30
    private $setHash;
31
    /**
32
     * @var SendMail
33
     */
34
    private $mail;
35
36
37
    public function __construct(
38
        UserPasswordEncoderInterface $passwordEncoder,
39
        UserSetHash $setHash,
40
        SendMail $mail
41
    ) {
42
        $this->passwordEncoder = $passwordEncoder;
43
        $this->setHash = $setHash;
44
        $this->mail = $mail;
45
    }
46
47
    public function setPassword(AbstractUserPasswordEvent $event)
48
    {
49
        /** @var User $user */
50
        $user = $event->getEntity();
51
        $password = $event->getPlainPassword();
52
        // encode the plain password
53
        $user->setPassword(
54
            $this->passwordEncoder->encodePassword(
55
                $user,
56
                $password
57
            )
58
        );
59
60
        $this->persist($event);
61
    }
62
63
    public function registerHash(AbstractUserEvent $event)
64
    {
65
        /** @var User $user */
66
        $user = $event->getEntity();
67
        $this->setHash->set($user);
68
        $this->persist($event);
69
    }
70
71
    public function updateUser(AbstractUserEvent $event)
72
    {
73
        $this->persist($event);
74
    }
75
76
    public function sendForgotpasswordMail(AbstractUserEvent $event)
77
    {
78
        /** @var User $user */
79
        $user = $event->getEntity();
80
        $this->mail->send(
81
            'Forgot Password',
82
            'emails/forgotpassword.html.twig',
83
            $user,
84
            $user->getEmail()
85
        );
86
    }
87
88
    public function sendResetpasswordMail(AbstractUserEvent $event)
89
    {
90
        /** @var User $user */
91
        $user = $event->getEntity();
92
        $this->mail->send(
93
            'Reset Password',
94
            'emails/resetpassword.html.twig',
95
            $user,
96
            $user->getEmail()
97
        );
98
    }
99
100
101
    public function sendValidationMail(AbstractUserEvent $event)
102
    {
103
        /** @var User $user */
104
        $user = $event->getEntity();
105
        $mailSent = $this->mail->send(
106
            'Email Validation',
107
            'emails/hash.html.twig',
108
            $user,
109
            $user->getEmail()
110
        );
111
112
        if ($mailSent) {
113
            $this->addFlashMessage(FlashMessageCategory::SUCCESS, "A validation mail has been sent to " . $user->getEmail());
114
            return;
115
        }
116
117
        $this->addFlashMessage(FlashMessageCategory::ERROR, "Error sending email");
118
    }
119
120
    /**
121
     * @return array The event names to listen to
122
     */
123
    public static function getSubscribedEvents()
124
    {
125
        return [
126
            UserRegisteredEvent::NAME => [
127
                ['setPassword', 50],
128
                ['registerHash', 40],
129
                ['flush', 20],
130
                ['sendValidationMail', 10],
131
            ],
132
            UserForgotpasswordEvent::NAME => [
133
                ['registerHash', 40],
134
                ['flush', 20],
135
                ['sendForgotpasswordMail', 10],
136
            ],
137
            UserResetpasswordEvent::NAME => [
138
                ['setPassword', 50],
139
                ['flush', 20],
140
                ['sendResetpasswordMail', 10],
141
            ],
142
            UserChangepasswordEvent::NAME => [
143
                ['setPassword', 50],
144
                ['flush', 20],
145
            ],
146
            UserUpdateAccountEvent::NAME => [
147
                ['updateUser', 50],
148
                ['flush', 20],
149
            ],
150
        ];
151
    }
152
}