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

sendPasswordResetToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Command;
4
5
use SumoCoders\FrameworkMultiUserBundle\Event\OnPasswordResetTokenCreated;
6
use SumoCoders\FrameworkMultiUserBundle\Event\PasswordResetTokenCreated;
7
use SumoCoders\FrameworkMultiUserBundle\User\PasswordReset as UserPasswordReset;
8
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
9
use Swift_Mailer;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
class PasswordResetRequestHandler
13
{
14
    /**
15
     * @var UserRepositoryCollection
16
     */
17
    private $userRepositoryCollection;
18
19
    /**
20
     * @var Swift_Mailer
21
     */
22
    private $listener;
23
24
    /**
25
     * @var EventDispatcher
26
     */
27
    private $dispatcher;
28
29
    /**
30
     * PasswordResetRequestHandler constructor.
31
     *
32
     * @param UserRepositoryCollection $userRepositoryCollection
33
     * @param EventDispatcherInterface $dispatcher
34
     * @param OnPasswordResetTokenCreated $listener
35
     */
36
    public function __construct(
37
        UserRepositoryCollection $userRepositoryCollection,
38
        EventDispatcherInterface $dispatcher,
39
        OnPasswordResetTokenCreated $listener
40
    ) {
41
        $this->userRepositoryCollection = $userRepositoryCollection;
42
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dispatcher of type object<Symfony\Component...entDispatcherInterface> is incompatible with the declared type object<SumoCoders\Framew...ommand\EventDispatcher> of property $dispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
        $this->listener = $listener;
0 ignored issues
show
Documentation Bug introduced by
It seems like $listener of type object<SumoCoders\Framew...swordResetTokenCreated> is incompatible with the declared type object<Swift_Mailer> of property $listener.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    /**
47
     * Creates a password reset token and sends an email to the user.
48
     *
49
     * @param RequestPasswordReset $command
50
     *
51
     * @return int
52
     */
53
    public function handle(RequestPasswordReset $command)
54
    {
55
        $user = $command->getUser();
56
        $user->generatePasswordResetToken();
57
        $repository = $this->userRepositoryCollection->findRepositoryByClassName(get_class($user));
58
        $repository->save($user);
59
60
        $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...
61
    }
62
63
    /**
64
     * Sends the password reset token to the user.
65
     *
66
     * @param UserPasswordReset $user
67
     *
68
     * @return int
69
     */
70
    private function sendPasswordResetToken(UserPasswordReset $user)
71
    {
72
        $event = new PasswordResetTokenCreated($user);
73
        $this->dispatcher->addListener(PasswordResetTokenCreated::NAME, [$this->listener, 'onPasswordResetTokenCreated']);
74
        $this->dispatcher->dispatch('multi_user.event.password_reset_token_created', $event);
75
    }
76
}
77