Completed
Pull Request — master (#52)
by
unknown
01:47
created

UserController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 17
nc 1
nop 8

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
namespace SumoCoders\FrameworkMultiUserBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
use SumoCoders\FrameworkMultiUserBundle\Command\Handler;
7
use SumoCoders\FrameworkMultiUserBundle\Form\Interfaces\FormWithDataTransferObject;
8
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository;
9
use Symfony\Component\Form\Form;
10
use Symfony\Component\Form\FormFactoryInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
14
use Symfony\Component\Routing\Router;
15
use Symfony\Component\Translation\TranslatorInterface;
16
17
/**
18
 * This class handles all the user actions.
19
 * Register a service for each action.
20
 */
21
class UserController
22
{
23
    /** @var FormFactoryInterface */
24
    private $formFactory;
25
26
    /** @var Router */
27
    private $router;
28
29
    /** @var FlashBagInterface */
30
    private $flashBag;
31
32
    /** @var TranslatorInterface */
33
    private $translator;
34
35
    /** @var Handler */
36
    private $handler;
37
38
    /** @var string */
39
    private $form;
40
41
    /** @var UserRepository */
42
    private $userRepository;
43
44
    /** @var string */
45
    private $redirectRoute;
46
47
    /**
48
     * @param FormFactoryInterface $formFactory
49
     * @param Router $router
50
     * @param FlashBagInterface $flashBag
51
     * @param TranslatorInterface $translator
52
     * @param string $form
53
     * @param Handler $handler
54
     * @param UserRepository $userRepository
55
     * @param string $redirectRoute = null
56
     */
57
    public function __construct(
58
        FormFactoryInterface $formFactory,
59
        Router $router,
60
        FlashBagInterface $flashBag,
61
        TranslatorInterface $translator,
62
        string $form,
63
        Handler $handler,
64
        UserRepository $userRepository,
65
        $redirectRoute = null
66
    ) {
67
        $this->formFactory = $formFactory;
68
        $this->router = $router;
69
        $this->flashBag = $flashBag;
70
        $this->translator = $translator;
71
        $this->form = $form;
72
        $this->handler = $handler;
73
        $this->userRepository = $userRepository;
74
        $this->redirectRoute = $redirectRoute;
75
    }
76
77
    /**
78
     * @param Request $request
79
     * @param int $id
80
     *
81
     * @Template()
82
     *
83
     * @return array
84
     */
85
    public function baseAction(Request $request, $id = null)
86
    {
87
        $form = $this->getFormForId($id);
88
        $form->handleRequest($request);
89
90
        if ($form->isSubmitted() && $form->isValid()) {
91
            $command = $form->getData();
92
            $this->handler->handle($command);
93
94
            $this->flashBag->add(
95
                'success',
96
                $this->translator->trans(
97
                    $id === null ? 'sumocoders.multiuserbundle.flash.added' : 'sumocoders.multiuserbundle.flash.edited',
98
                    ['%user%' => $command->getEntity()->getDisplayName()]
99
                )
100
            );
101
102
            if ($this->redirectRoute !== null) {
103
                return new RedirectResponse($this->router->generate($this->redirectRoute));
104
            }
105
        }
106
107
        return ['form' => $form->createView()];
108
    }
109
110
    /**
111
     * @param int $id = null
112
     *
113
     * @return Form
114
     */
115
    private function getFormForId($id = null)
116
    {
117
        if ($id === null) {
118
            return $this->formFactory->create($this->form);
119
        }
120
121
        $user = $this->userRepository->find((int) $id);
122
        $dataTransferObjectClass = $this->form::getDataTransferObjectClass();
0 ignored issues
show
Bug introduced by
The method getDataTransferObjectClass cannot be called on $this->form (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
123
        $dataTransferObject = $dataTransferObjectClass::fromUser($user);
124
125
        return $this->formFactory->create($this->form, $dataTransferObject);
126
    }
127
}
128