Completed
Pull Request — master (#12)
by
unknown
15:27 queued 12:16
created

PasswordResetRequestHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Command;
4
5
use SumoCoders\FrameworkMultiUserBundle\User\PasswordResetInterface;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
7
use Swift_Mailer;
8
use Swift_Message;
9
use Symfony\Bundle\FrameworkBundle\Routing\Router;
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 Router
31
     */
32
    private $router;
33
34
    public function __construct(UserRepositoryCollection $userRepositoryCollection, Swift_Mailer $mailer, TranslatorInterface $translator, Router $router)
35
    {
36
        $this->userRepositoryCollection = $userRepositoryCollection;
37
        $this->mailer = $mailer;
38
        $this->translator = $translator;
39
        $this->router = $router;
40
    }
41
42
    /**
43
     * Creates a password reset token and sends an email to the user.
44
     *
45
     * @param PasswordResetRequest $command
46
     *
47
     * @return int
48
     */
49
    public function handle(PasswordResetRequest $command)
50
    {
51
        $user = $command->getUser();
52
        $user->generatePasswordResetToken();
53
        $repository = $this->userRepositoryCollection->findRepositoryByClassName(get_class($user));
54
        $repository->save($user);
55
56
        return $this->sendPasswordResetToken($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<SumoCoders\Framew...dle\User\UserInterface>, but the function expects a object<SumoCoders\Framew...PasswordResetInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
    }
58
59
    /**
60
     * Sends the password reset token to the user.
61
     *
62
     * @param PasswordResetInterface $user
63
     *
64
     * @return int
65
     */
66
    private function sendPasswordResetToken(PasswordResetInterface $user)
67
    {
68
        $messageBody = $this->getPasswordResetMessage($user);
69
70
        $message = Swift_Message::newInstance()
71
            ->setSubject('Password reset requested')
72
            ->setFrom('[email protected]')
73
            ->setTo($user->getEmail())
74
            ->setBody($messageBody, 'text/plain');
75
76
        return $this->mailer->send($message);
77
    }
78
79
    /**
80
     * Creates the password reset message.
81
     *
82
     * @param PasswordResetInterface $user
83
     *
84
     * @return string
85
     */
86
    private function getPasswordResetMessage(PasswordResetInterface $user)
87
    {
88
        $url = $this->router->generate('multi_user_reset_password');
89
        $token = '?token='.$user->getPasswordResetToken();
90
91
        return $this->translator->trans('sumocoders.multiuserbundle.mail.request_password', ['%link%' => $url.$token]);
92
    }
93
}
94