Completed
Pull Request — master (#23)
by
unknown
03:39
created

PasswordResetController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 17.71 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 8
dl 17
loc 96
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
B resetAction() 0 37 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Controller;
4
5
use SumoCoders\FrameworkMultiUserBundle\Command\ResetPasswordHandler;
6
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\ChangePasswordDataTransferObject;
7
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidPasswordConfirmationException;
8
use SumoCoders\FrameworkMultiUserBundle\Form\ChangePasswordType;
9
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
10
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
11
use Symfony\Bundle\FrameworkBundle\Routing\Router;
12
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
13
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
18
19
class PasswordResetController
20
{
21
    /** @var UserRepositoryCollection */
22
    private $userRepositoryCollection;
23
24
    /** @var ResetPasswordHandler */
25
    private $resetPasswordHandler;
26
27
    /** @var Router */
28
    private $router;
29
30
    /** @var FormFactoryInterface */
31
    private $formFactory;
32
33
    /** @var EngineInterface */
34
    private $templating;
35
36
    /** @var Translator */
37
    private $translator;
38
39
    /** @var FlashBagInterface */
40
    private $flashBag;
41
42
    /**
43
     * @param UserRepositoryCollection $userRepositoryCollection
44
     * @param ResetPasswordHandler $resetPasswordHandler
45
     * @param Router $router
46
     * @param FormFactoryInterface $formFactory
47
     * @param EngineInterface $templating
48
     * @param Translator $translator
49
     * @param FlashBagInterface $flashBag
50
     */
51 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
        UserRepositoryCollection $userRepositoryCollection,
53
        ResetPasswordHandler $resetPasswordHandler,
54
        Router $router,
55
        FormFactoryInterface $formFactory,
56
        EngineInterface $templating,
57
        Translator $translator,
58
        FlashBagInterface $flashBag
59
    ) {
60
        $this->userRepositoryCollection = $userRepositoryCollection;
61
        $this->resetPasswordHandler = $resetPasswordHandler;
62
        $this->router = $router;
63
        $this->formFactory = $formFactory;
64
        $this->templating = $templating;
65
        $this->translator = $translator;
66
        $this->flashBag = $flashBag;
67
    }
68
69
    /**
70
     * @param Request $request
71
     * @param PasswordResetToken $token
72
     *
73
     * @throws InvalidPasswordConfirmationException
74
     *
75
     * @return RedirectResponse
76
     */
77
    public function resetAction(Request $request, PasswordResetToken $token)
78
    {
79
        $user = $this->userRepositoryCollection->findUserByToken($token);
80
81
        if ($user === null) {
82
            return new RedirectResponse(
83
                $this->router->generate('multi_user_login')
84
            );
85
        }
86
87
        $changePasswordTransferObject = ChangePasswordDataTransferObject::forUser($user);
88
        $form = $this->formFactory->create(ChangePasswordType::class, $changePasswordTransferObject);
89
90
        $form->handleRequest($request);
91
92
        if ($form->isSubmitted() && $form->isValid()) {
93
            $this->resetPasswordHandler->handle($form->getData());
94
95
            $this->flashBag->add(
96
                'success',
97
                $this->translator->trans(
98
                    'sumocoders.multiuserbundle.flash.password_reset_success'
99
                )
100
            );
101
102
            return new RedirectResponse(
103
                $this->router->generate('multi_user_login')
104
            );
105
        }
106
107
        return $this->templating->renderResponse(
108
            'SumoCodersFrameworkMultiUserBundle:PasswordReset:reset.html.twig',
109
            [
110
                'form' => $form->createView(),
111
            ]
112
        );
113
    }
114
}
115