Completed
Pull Request — master (#12)
by
unknown
02:58
created

getPasswordResetMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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\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);
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...
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);
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...
102
103
        return $this->translator->trans('sumocoders.multiuserbundle.mail.request_password', ['%link%' => $url]);
104
    }
105
}
106