1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Command; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\PasswordReset as UserPasswordReset; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection; |
7
|
|
|
use Swift_Mailer; |
8
|
|
|
use Swift_Message; |
9
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
11
|
|
|
|
12
|
|
|
class PasswordResetRequestHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var UserRepositoryCollection |
16
|
|
|
*/ |
17
|
|
|
private $userRepositoryCollection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Swift_Mailer |
21
|
|
|
*/ |
22
|
|
|
private $mailer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var TranslatorInterface |
26
|
|
|
*/ |
27
|
|
|
private $translator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var UrlGeneratorInterface |
31
|
|
|
*/ |
32
|
|
|
private $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* PasswordResetRequestHandler constructor. |
36
|
|
|
* |
37
|
|
|
* @param UserRepositoryCollection $userRepositoryCollection |
38
|
|
|
* @param Swift_Mailer $mailer |
39
|
|
|
* @param TranslatorInterface $translator |
40
|
|
|
* @param UrlGeneratorInterface $router |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
UserRepositoryCollection $userRepositoryCollection, |
44
|
|
|
Swift_Mailer $mailer, |
45
|
|
|
TranslatorInterface $translator, |
46
|
|
|
UrlGeneratorInterface $router |
47
|
|
|
) { |
48
|
|
|
$this->userRepositoryCollection = $userRepositoryCollection; |
49
|
|
|
$this->mailer = $mailer; |
50
|
|
|
$this->translator = $translator; |
51
|
|
|
$this->router = $router; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates a password reset token and sends an email to the user. |
56
|
|
|
* |
57
|
|
|
* @param PasswordResetRequest $command |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
public function handle(PasswordResetRequest $command) |
62
|
|
|
{ |
63
|
|
|
$user = $command->getUser(); |
64
|
|
|
$user->generatePasswordResetToken(); |
65
|
|
|
$repository = $this->userRepositoryCollection->findRepositoryByClassName(get_class($user)); |
66
|
|
|
$repository->save($user); |
67
|
|
|
|
68
|
|
|
return $this->sendPasswordResetToken($user); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sends the password reset token to the user. |
73
|
|
|
* |
74
|
|
|
* @param UserPasswordReset $user |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
private function sendPasswordResetToken(UserPasswordReset $user) |
79
|
|
|
{ |
80
|
|
|
$messageBody = $this->getPasswordResetMessage($user); |
81
|
|
|
|
82
|
|
|
$message = Swift_Message::newInstance() |
83
|
|
|
->setSubject('Password reset requested') |
84
|
|
|
->setFrom('[email protected]') |
85
|
|
|
->setTo($user->getEmail()) |
86
|
|
|
->setBody($messageBody, 'text/plain'); |
87
|
|
|
|
88
|
|
|
return $this->mailer->send($message); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates the password reset message. |
93
|
|
|
* |
94
|
|
|
* @param UserPasswordReset $user |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function getPasswordResetMessage(UserPasswordReset $user) |
99
|
|
|
{ |
100
|
|
|
$token = $user->getPasswordResetToken(); |
101
|
|
|
$url = $this->router->generate('multi_user_reset_password', ['token' => $token], true); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return $this->translator->trans('sumocoders.multiuserbundle.mail.request_password', ['%link%' => $url]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: