|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Entity\UserInterface; |
|
6
|
|
|
use Kunstmaan\AdminBundle\Event\ChangePasswordSuccessEvent; |
|
7
|
|
|
use Kunstmaan\AdminBundle\Event\Events; |
|
8
|
|
|
use Kunstmaan\AdminBundle\Service\AuthenticationMailer\AuthenticationMailerInterface; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
14
|
|
|
|
|
15
|
|
|
class PasswordResetService |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var UserManager */ |
|
18
|
|
|
private $userManager; |
|
19
|
|
|
/** @var UrlGeneratorInterface */ |
|
20
|
|
|
private $urlGenerator; |
|
21
|
|
|
/** @var EventDispatcherInterface */ |
|
22
|
|
|
private $eventDispatcher; |
|
23
|
|
|
/** @var AuthenticationMailerInterface */ |
|
24
|
|
|
private $authenticationMailer; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(UserManager $userManager, UrlGeneratorInterface $urlGenerator, EventDispatcherInterface $eventDispatcher, AuthenticationMailerInterface $authenticationMailer) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->userManager = $userManager; |
|
29
|
|
|
$this->urlGenerator = $urlGenerator; |
|
30
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
31
|
|
|
$this->authenticationMailer = $authenticationMailer; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function processResetRequest(string $email, string $locale): void |
|
35
|
|
|
{ |
|
36
|
|
|
$user = $this->userManager->findUserByUsernameOrEmail($email); |
|
37
|
|
|
|
|
38
|
|
|
if (null === $user) { |
|
39
|
|
|
//TODO: throw user not found exception |
|
40
|
|
|
throw new \Exception('User not found'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->userManager->setResetToken($user); |
|
44
|
|
|
$this->authenticationMailer->sendPasswordResetEmail($user, $locale); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function resetPassword(UserInterface $user, string $newPassword): Response |
|
48
|
|
|
{ |
|
49
|
|
|
$this->userManager->changePassword($user, $newPassword); |
|
50
|
|
|
|
|
51
|
|
|
$response = new RedirectResponse($this->urlGenerator->generate('KunstmaanAdminBundle_homepage')); |
|
52
|
|
|
$this->dispatch(new ChangePasswordSuccessEvent($user, $response), Events::CHANGE_PASSWORD_COMPLETED); |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param object $event |
|
59
|
|
|
* @param string $eventName |
|
60
|
|
|
* |
|
61
|
|
|
* @return object |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
private function dispatch($event, string $eventName) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
if (class_exists(LegacyEventDispatcherProxy::class)) { |
|
66
|
|
|
$eventDispatcher = LegacyEventDispatcherProxy::decorate($this->eventDispatcher); |
|
67
|
|
|
|
|
68
|
|
|
return $eventDispatcher->dispatch($event, $eventName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->eventDispatcher->dispatch($eventName, $event); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.