Completed
Pull Request — master (#12)
by
unknown
03:18
created

getPasswordResetMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\RouterInterface;
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 RouterInterface
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 RouterInterface $router
41
     */
42
    public function __construct(UserRepositoryCollection $userRepositoryCollection, Swift_Mailer $mailer, TranslatorInterface $translator, RouterInterface $router)
43
    {
44
        $this->userRepositoryCollection = $userRepositoryCollection;
45
        $this->mailer = $mailer;
46
        $this->translator = $translator;
47
        $this->router = $router;
48
    }
49
50
    /**
51
     * Creates a password reset token and sends an email to the user.
52
     *
53
     * @param PasswordResetRequest $command
54
     *
55
     * @return int
56
     */
57
    public function handle(PasswordResetRequest $command)
58
    {
59
        $user = $command->getUser();
60
        $user->generatePasswordResetToken();
61
        $repository = $this->userRepositoryCollection->findRepositoryByClassName(get_class($user));
62
        $repository->save($user);
63
64
        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...dle\User\PasswordReset>.

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...
65
    }
66
67
    /**
68
     * Sends the password reset token to the user.
69
     *
70
     * @param UserPasswordReset $user
71
     *
72
     * @return int
73
     */
74
    private function sendPasswordResetToken(UserPasswordReset $user)
75
    {
76
        $messageBody = $this->getPasswordResetMessage($user);
77
78
        $message = Swift_Message::newInstance()
79
            ->setSubject('Password reset requested')
80
            ->setFrom('[email protected]')
81
            ->setTo($user->getEmail())
82
            ->setBody($messageBody, 'text/plain');
83
84
        return $this->mailer->send($message);
85
    }
86
87
    /**
88
     * Creates the password reset message.
89
     *
90
     * @param UserPasswordReset $user
91
     *
92
     * @return string
93
     */
94
    private function getPasswordResetMessage(UserPasswordReset $user)
95
    {
96
        $url = $this->router->generate('multi_user_reset_password', ['token' => $user->getPasswordResetToken()], true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
97
98
        return $this->translator->trans('sumocoders.multiuserbundle.mail.request_password', ['%link%' => $url]);
99
    }
100
}
101