Test Failed
Push — develop ( 01b725...e43081 )
by Stone
04:47
created

UserAccountSubscriber::updateUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber\User;
4
5
use App\Event\User\UserChangepasswordEvent;
6
use App\Event\User\UserEvent;
7
use App\Event\User\UserForgotpasswordEvent;
8
use App\Event\User\UserRegisteredEvent;
9
use App\Event\User\UserResetpasswordEvent;
10
use App\Event\User\UserUpdateAccountEvent;
11
use App\FlashMessage\FlashMessageCategory;
12
use App\Mail\SendMail;
13
use App\Security\UserSetHash;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
16
17
18
class UserAccountSubscriber extends UserSubscriber implements EventSubscriberInterface
19
{
20
21
    /**
22
     * @var UserPasswordEncoderInterface
23
     */
24
    private $passwordEncoder;
25
    /**
26
     * @var UserSetHash
27
     */
28
    private $setHash;
29
    /**
30
     * @var SendMail
31
     */
32
    private $mail;
33
34
35
    public function __construct(
36
        UserPasswordEncoderInterface $passwordEncoder,
37
        UserSetHash $setHash,
38
        SendMail $mail
39
    ) {
40
        $this->passwordEncoder = $passwordEncoder;
41
        $this->setHash = $setHash;
42
        $this->mail = $mail;
43
    }
44
45
    public function setPassword(UserEvent $event)
46
    {
47
        $user = $event->getEntity();
48
        $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\UserChangepasswordEvent 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

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