DeleteController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
A deleteAction() 0 23 3

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\DeleteUserHandler;
6
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\BaseUserDataTransferObject;
7
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser;
8
use SumoCoders\FrameworkMultiUserBundle\Form\DeleteType;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
13
use Symfony\Component\Routing\Router;
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
final class DeleteController
17
{
18
    /** @var FormFactoryInterface */
19
    private $formFactory;
20
21
    /** @var DeleteUserHandler */
22
    private $handler;
23
24
    /** @var FlashBagInterface */
25
    private $flashBag;
26
27
    /** @var TranslatorInterface */
28
    private $translator;
29
30
    /** @var Router */
31
    private $router;
32
33
    /** @var string */
34
    private $redirectRoute;
35
36 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...
37
        FormFactoryInterface $formFactory,
38
        DeleteUserHandler $handler,
39
        FlashBagInterface $flashBag,
40
        TranslatorInterface $translator,
41
        Router $router,
42
        string $redirectRoute
43
    ) {
44
        $this->formFactory = $formFactory;
45
        $this->handler = $handler;
46
        $this->flashBag = $flashBag;
47
        $this->translator = $translator;
48
        $this->router = $router;
49
        $this->redirectRoute = $redirectRoute;
50
    }
51
52
    /**
53
     * @param Request $request
54
     * @param BaseUser $user
55
     *
56
     * @return array|RedirectResponse
57
     */
58
    public function deleteAction(Request $request, BaseUser $user)
59
    {
60
        $userDataTransferObject = BaseUserDataTransferObject::fromUser($user);
61
62
        $form = $this->formFactory->create(DeleteType::class, $userDataTransferObject);
63
        $form->handleRequest($request);
64
65
        if ($form->isSubmitted() && $form->isValid()) {
66
            $this->handler->handle($form->getData());
67
68
            $this->flashBag->add(
69
                'success',
70
                $this->translator->trans(
71
                    'sumocoders.multiuserbundle.flash.deleted',
72
                    ['%user%' => $form->getData()->getEntity()->getDisplayName()]
73
                )
74
            );
75
76
            return new RedirectResponse($this->router->generate($this->redirectRoute));
77
        }
78
79
        return ['form' => $form->createView()];
80
    }
81
}
82