Passed
Push — master ( 786b21...4a8f5f )
by Stone
06:47 queued 42s
created

UserAccountSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber\User;
4
5
use App\Event\User\UserEvent;
6
use App\Event\User\UserForgotpasswordEvent;
7
use App\Event\User\UserRegisteredEvent;
8
use App\Event\User\UserResetpasswordEvent;
9
use App\FlashMessage\FlashMessageCategory;
10
use App\Mail\SendMail;
11
use App\Security\UserSetHash;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
14
15
16
class UserAccountSubscriber extends UserSubscriber implements EventSubscriberInterface
17
{
18
19
    /**
20
     * @var UserPasswordEncoderInterface
21
     */
22
    private $passwordEncoder;
23
    /**
24
     * @var UserSetHash
25
     */
26
    private $setHash;
27
    /**
28
     * @var SendMail
29
     */
30
    private $mail;
31
32
33
    public function __construct(
34
        UserPasswordEncoderInterface $passwordEncoder,
35
        UserSetHash $setHash,
36
        SendMail $mail
37
    ) {
38
        $this->passwordEncoder = $passwordEncoder;
39
        $this->setHash = $setHash;
40
        $this->mail = $mail;
41
    }
42
43
    public function setPassword(UserEvent $event)
44
    {
45
        $user = $event->getEntity();
46
        $password = $event->getPlainPassword();
0 ignored issues
show
Bug introduced by
The method getPlainPassword() does not exist on App\Event\User\UserEvent. It seems like you code against a sub-type of App\Event\User\UserEvent such as App\Event\User\UserRegisteredEvent or App\Event\User\UserResetpasswordEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $password = $event->getPlainPassword();
Loading history...
47
        // encode the plain password
48
        $user->setPassword(
49
            $this->passwordEncoder->encodePassword(
50
                $user,
51
                $password
52
            )
53
        );
54
55
        $this->persist($event);
56
    }
57
58
    public function registerHash(UserEvent $event)
59
    {
60
        $user = $event->getEntity();
61
        $this->setHash->set($user);
62
        $this->persist($event);
63
    }
64
65
    public function sendForgotpasswordMail(UserEvent $event)
66
    {
67
        $user = $event->getEntity();
68
        $this->mail->send(
69
            'Forgot Password',
70
            'emails/forgotpassword.html.twig',
71
            $user,
72
            $user->getEmail()
73
        );
74
    }
75
76
    public function sendResetpasswordMail(UserEvent $event)
77
    {
78
        $user = $event->getEntity();
79
        $this->mail->send(
80
            'Reset Password',
81
            'emails/resetpassword.html.twig',
82
            $user,
83
            $user->getEmail()
84
        );
85
    }
86
87
88
    public function sendValidationMail(UserEvent $event)
89
    {
90
        $user = $event->getEntity();
91
        $mailSent = $this->mail->send(
92
            'Email Validation',
93
            'emails/hash.html.twig',
94
            $user,
95
            $user->getEmail()
96
        );
97
98
        if ($mailSent) {
99
            $this->addFlash(FlashMessageCategory::SUCCESS, "A validation mail has been sent to " . $user->getEmail());
100
            return;
101
        }
102
103
        $this->addFlash(FlashMessageCategory::ERROR, "Error sending email");
104
    }
105
106
    /**
107
     * @return array The event names to listen to
108
     */
109
    public static function getSubscribedEvents()
110
    {
111
        return [
112
            UserRegisteredEvent::NAME => [
113
                ['setPassword', 50],
114
                ['registerHash', 40],
115
                ['flush', 20],
116
                ['sendValidationMail', 10],
117
            ],
118
            UserForgotpasswordEvent::NAME => [
119
                ['registerHash', 40],
120
                ['flush', 20],
121
                ['sendForgotpasswordMail', 10],
122
            ],
123
            UserResetpasswordEvent::NAME => [
124
                ['setPassword', 50],
125
                ['flush', 20],
126
                ['sendResetpasswordMail', 10],
127
            ],
128
        ];
129
    }
130
}