Completed
Push — master ( c7ec68...dd7588 )
by
unknown
44s
created

RequestPasswordResetController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 17.24 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 6
dl 15
loc 87
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
B requestAction() 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\RequestPasswordResetHandler;
6
use SumoCoders\FrameworkMultiUserBundle\Exception\UserNotFound;
7
use SumoCoders\FrameworkMultiUserBundle\Form\RequestPasswordType;
8
use Symfony\Bundle\FrameworkBundle\Routing\Router;
9
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
10
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
11
use Symfony\Component\Form\FormError;
12
use Symfony\Component\Form\FormFactoryInterface;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
17
18
final class RequestPasswordResetController
19
{
20
    /** @var EngineInterface */
21
    private $templating;
22
23
    /** @var FormFactoryInterface */
24
    private $formFactory;
25
26
    /** @var RequestPasswordResetHandler */
27
    private $requestPasswordResetHandler;
28
29
    /** @var Router */
30
    private $router;
31
32
    /** @var Translator */
33
    private $translator;
34
35
    /** @var FlashBagInterface */
36
    private $flashBag;
37
38
    /**
39
     * @param EngineInterface $templating
40
     * @param FormFactoryInterface $formFactory
41
     * @param RequestPasswordResetHandler $requestPasswordResetHandler
42
     * @param Router $router
43
     * @param Translator $translator
44
     * @param FlashBagInterface $flashBag
45
     */
46 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...
47
        EngineInterface $templating,
48
        FormFactoryInterface $formFactory,
49
        RequestPasswordResetHandler $requestPasswordResetHandler,
50
        Router $router,
51
        Translator $translator,
52
        FlashBagInterface $flashBag
53
    ) {
54
        $this->templating = $templating;
55
        $this->formFactory = $formFactory;
56
        $this->requestPasswordResetHandler = $requestPasswordResetHandler;
57
        $this->router = $router;
58
        $this->translator = $translator;
59
        $this->flashBag = $flashBag;
60
    }
61
62
    /**
63
     * @param Request $request
64
     *
65
     * @return array
66
     */
67
    public function requestAction(Request $request)
68
    {
69
        $form = $this->formFactory->create(RequestPasswordType::class);
70
71
        $form->handleRequest($request);
72
73
        if ($form->isSubmitted() && $form->isValid()) {
74
            try {
75
                $this->requestPasswordResetHandler->handle($form->getData());
76
77
                $this->flashBag->add(
78
                    'success',
79
                    $this->translator->trans(
80
                        'sumocoders.multiuserbundle.flash.password_reset_request_success'
81
                    )
82
                );
83
84
                return new RedirectResponse($this->router->generate('multi_user_login'));
85
            } catch (UserNotFound $exception) {
86
                $errorMessage = $this->translator->trans(
87
                    'sumocoders.multiuserbundle.request_password_reset.username_not_found',
88
                    [
89
                        '%username%' => $form->getData()->userName
90
                    ],
91
                    'validators'
92
                );
93
                $form->addError(new FormError($errorMessage));
94
            }
95
        }
96
97
        return $this->templating->renderResponse(
98
            'SumoCodersFrameworkMultiUserBundle:PasswordReset:request.html.twig',
99
            [
100
                'form' => $form->createView(),
101
            ]
102
        );
103
    }
104
}
105