ChangePasswordController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 20
rs 9.9666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\Controller\PasswordReset;
13
14
use FSi\Bundle\AdminBundle\Message\FlashMessages;
15
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
16
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent;
17
use FSi\Bundle\AdminSecurityBundle\Security\User\ResettablePasswordInterface;
18
use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface;
19
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\Form\FormFactoryInterface;
22
use Symfony\Component\HttpFoundation\RedirectResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Symfony\Component\Routing\RouterInterface;
27
28
class ChangePasswordController
29
{
30
    /**
31
     * @var EngineInterface
32
     */
33
    private $templating;
34
35
    /**
36
     * @var string
37
     */
38
    private $changePasswordActionTemplate;
39
40
    /**
41
     * @var UserRepositoryInterface
42
     */
43
    private $userRepository;
44
45
    /**
46
     * @var RouterInterface
47
     */
48
    private $router;
49
50
    /**
51
     * @var FormFactoryInterface
52
     */
53
    private $formFactory;
54
55
    /**
56
     * @var EventDispatcherInterface
57
     */
58
    private $eventDispatcher;
59
60
    /**
61
     * @var FlashMessages
62
     */
63
    private $flashMessages;
64
65
    /**
66
     * @var string
67
     */
68
    private $formType;
69
70
    /**
71
     * @var array
72
     */
73
    private $formValidationGroups;
74
75
    public function __construct(
76
        EngineInterface $templating,
77
        $changePasswordActionTemplate,
78
        UserRepositoryInterface $userRepository,
79
        RouterInterface $router,
80
        FormFactoryInterface $formFactory,
81
        EventDispatcherInterface $eventDispatcher,
82
        FlashMessages $flashMessages,
83
        $formType,
84
        array $formValidationGroups
85
    ) {
86
        $this->templating = $templating;
87
        $this->changePasswordActionTemplate = $changePasswordActionTemplate;
88
        $this->userRepository = $userRepository;
89
        $this->router = $router;
90
        $this->formFactory = $formFactory;
91
        $this->eventDispatcher = $eventDispatcher;
92
        $this->flashMessages = $flashMessages;
93
        $this->formType = $formType;
94
        $this->formValidationGroups = $formValidationGroups;
95
    }
96
97
    public function changePasswordAction(Request $request, string $token): Response
98
    {
99
        $user = $this->userRepository->findUserByPasswordResetToken($token);
100
101
        if (null === $user) {
102
            throw new NotFoundHttpException();
103
        }
104
105
        if (!$user->getPasswordResetToken()->isNonExpired()) {
106
            throw new NotFoundHttpException();
107
        }
108
109
        $form = $this->formFactory->create(
110
            $this->formType,
111
            $user,
112
            ['validation_groups' => $this->formValidationGroups]
113
        );
114
115
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
116
            $user->removePasswordResetToken();
117
118
            $this->eventDispatcher->dispatch(
119
                AdminSecurityEvents::CHANGE_PASSWORD,
0 ignored issues
show
Bug introduced by
FSi\Bundle\AdminSecurity...Events::CHANGE_PASSWORD of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
                /** @scrutinizer ignore-type */ AdminSecurityEvents::CHANGE_PASSWORD,
Loading history...
120
                new ChangePasswordEvent($user)
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with new FSi\Bundle\AdminSecu...ngePasswordEvent($user). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
121
                                    dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
121
            );
122
123
            $this->flashMessages->success(
124
                'admin.password_reset.change_password.message.success',
125
                [],
126
                'FSiAdminSecurity'
127
            );
128
129
            return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
130
        }
131
132
        return $this->templating->renderResponse(
133
            $this->changePasswordActionTemplate,
134
            ['form' => $form->createView()]
135
        );
136
    }
137
}
138