Completed
Push — master ( b362c6...da680b )
by
unknown
11s
created

UserController::baseAction()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 5.3846
cc 8
eloc 25
nc 15
nop 2
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
use SumoCoders\FrameworkCoreBundle\BreadCrumb\BreadCrumbBuilder;
7
use SumoCoders\FrameworkMultiUserBundle\Command\Handler;
8
use SumoCoders\FrameworkMultiUserBundle\Form\DeleteType;
9
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository;
10
use Symfony\Component\Form\Form;
11
use Symfony\Component\Form\FormFactoryInterface;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
15
use Symfony\Component\Routing\Router;
16
use Symfony\Component\Translation\TranslatorInterface;
17
18
/**
19
 * This class handles all the user actions.
20
 * Register a service for each action.
21
 */
22
class UserController
23
{
24
    /** @var FormFactoryInterface */
25
    private $formFactory;
26
27
    /** @var Router */
28
    private $router;
29
30
    /** @var FlashBagInterface */
31
    private $flashBag;
32
33
    /** @var TranslatorInterface */
34
    private $translator;
35
36
    /** @var Handler */
37
    private $handler;
38
39
    /** @var string */
40
    private $form;
41
42
    /** @var UserRepository */
43
    private $userRepository;
44
45
    /** @var BreadCrumbBuilder */
46
    private $breadcrumbBuilder;
47
48
    /** @var array */
49
    private $breadcrumbs;
50
51
    /** @var string */
52
    private $redirectRoute;
53
54
    /**
55
     * @param FormFactoryInterface $formFactory
56
     * @param Router $router
57
     * @param FlashBagInterface $flashBag
58
     * @param TranslatorInterface $translator
59
     * @param string $form
60
     * @param Handler $handler
61
     * @param UserRepository $userRepository
62
     * @param string $redirectRoute = null
63
     */
64
    public function __construct(
65
        FormFactoryInterface $formFactory,
66
        Router $router,
67
        FlashBagInterface $flashBag,
68
        TranslatorInterface $translator,
69
        string $form,
70
        Handler $handler,
71
        UserRepository $userRepository,
72
        BreadCrumbBuilder $breadcrumbBuilder,
73
        array $breadcrumbs,
74
        $redirectRoute = null
75
    ) {
76
        $this->formFactory = $formFactory;
77
        $this->router = $router;
78
        $this->flashBag = $flashBag;
79
        $this->translator = $translator;
80
        $this->form = $form;
81
        $this->handler = $handler;
82
        $this->userRepository = $userRepository;
83
        $this->breadcrumbBuilder = $breadcrumbBuilder;
84
        $this->breadcrumbs = $breadcrumbs;
85
        $this->redirectRoute = $redirectRoute;
86
    }
87
88
    /**
89
     * @param Request $request
90
     * @param int $id
91
     *
92
     * @Template()
93
     *
94
     * @return array
95
     */
96
    public function baseAction(Request $request, $id = null)
97
    {
98
        foreach ($this->breadcrumbs as $breadcrumb) {
99
            $uri = '';
100
            if ($breadcrumb['route'] !== '') {
101
                $uri = $this->router->generate($breadcrumb['route']);
102
            }
103
104
            $this->breadcrumbBuilder->addSimpleItem(ucfirst($this->translator->trans($breadcrumb['label'])), $uri);
105
        }
106
107
        $form = $this->getFormForId($id);
108
        $form->handleRequest($request);
109
110
        if ($form->isSubmitted() && $form->isValid()) {
111
            $command = $form->getData();
112
            $this->handler->handle($command);
113
114
            $this->flashBag->add(
115
                'success',
116
                $this->translator->trans(
117
                    $id === null ? 'sumocoders.multiuserbundle.flash.added' : 'sumocoders.multiuserbundle.flash.edited',
118
                    ['%user%' => $command->getEntity()->getDisplayName()]
119
                )
120
            );
121
122
            if ($this->redirectRoute !== null) {
123
                return new RedirectResponse($this->router->generate($this->redirectRoute));
124
            }
125
        }
126
127
        if ($id !== null) {
128
            $deleteForm = $this->formFactory->create(DeleteType::class, $form->getData());
129
130
            return [
131
                'form' => $form->createView(),
132
                'deleteForm' => $deleteForm->createView(),
133
                'user' => $form->getData()->getEntity(),
134
            ];
135
        }
136
137
        return ['form' => $form->createView()];
138
    }
139
140
    /**
141
     * @param int $id = null
142
     *
143
     * @return Form
144
     */
145
    private function getFormForId($id = null)
146
    {
147
        if ($id === null) {
148
            return $this->formFactory->create($this->form);
149
        }
150
151
        $user = $this->userRepository->find((int) $id);
152
        $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...
153
        $dataTransferObject = $dataTransferObjectClass::fromUser($user);
154
155
        return $this->formFactory->create($this->form, $dataTransferObject);
156
    }
157
}
158