UserController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 10.88 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 13
Bugs 2 Features 1
Metric Value
wmc 17
lcom 1
cbo 3
dl 21
loc 193
c 13
b 2
f 1
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 0 4 1
A createAction() 0 4 1
A editAction() 0 4 1
A viewAction() 0 4 1
A deleteAction() 0 4 1
A crudListRole() 0 4 1
A crudCreateRole() 0 4 1
A crudCreateFormType() 0 4 1
A crudCreatePrePersist() 0 4 1
A crudEditRole() 0 4 1
A crudEditFormType() 0 4 1
A crudEditPrePersist() 0 4 1
A crudViewRole() 0 4 1
A crudDeleteRole() 0 4 1
A crudName() 0 4 1
A crudObjectClass() 0 4 1
A __construct() 21 21 1

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 Dominikzogg\EnergyCalculator\Controller;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Dominikzogg\EnergyCalculator\Entity\User;
7
use Dominikzogg\EnergyCalculator\Form\UserType;
8
use Knp\Component\Pager\Paginator;
9
use Saxulum\Crud\Listing\ListingFactory;
10
use Saxulum\RouteController\Annotation\DI;
11
use Saxulum\RouteController\Annotation\Route;
12
use Saxulum\UserProvider\Manager\UserManager;
13
use Symfony\Component\Form\FormFactory;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\Form\FormTypeInterface;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
21
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
22
23
/**
24
 * @Route("/{_locale}/user", asserts={"_locale"="([a-z]{2}|[a-z]{2}_[A-Z]{2})"})
25
 * @DI(serviceIds={
26
 *      "security.authorization_checker",
27
 *      "security.token_storage",
28
 *      "saxulum.crud.listing.factory",
29
 *      "doctrine",
30
 *      "form.factory",
31
 *      "knp_paginator",
32
 *      "url_generator",
33
 *      "twig",
34
 *      "saxulum.userprovider.manager"
35
 * })
36
 */
37
class UserController extends AbstractCRUDController
38
{
39
    /**
40
     * @var UserManager
41
     */
42
    protected $userManager;
43
44
    /**
45
     * @param AuthorizationCheckerInterface $authorizationChecker
46
     * @param TokenStorageInterface $tokenStorage
47
     * @param ListingFactory $listingFactory
48
     * @param ManagerRegistry          $doctrine
49
     * @param FormFactory              $formFactory
50
     * @param Paginator                $paginator
51
     * @param UrlGeneratorInterface    $urlGenerator
52
     * @param \Twig_Environment        $twig
53
     * @param UserManager              $userManager
54
     */
55 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...
56
        AuthorizationCheckerInterface $authorizationChecker,
57
        TokenStorageInterface $tokenStorage,
58
        ListingFactory $listingFactory,
59
        ManagerRegistry $doctrine,
60
        FormFactory $formFactory,
61
        Paginator $paginator,
62
        UrlGeneratorInterface $urlGenerator,
63
        \Twig_Environment $twig,
64
        UserManager $userManager
65
    ) {
66
        $this->authorizationChecker = $authorizationChecker;
67
        $this->tokenStorage = $tokenStorage;
68
        $this->listingFactory = $listingFactory;
69
        $this->doctrine = $doctrine;
70
        $this->formFactory = $formFactory;
71
        $this->paginator = $paginator;
72
        $this->urlGenerator = $urlGenerator;
73
        $this->twig = $twig;
74
        $this->userManager = $userManager;
75
    }
76
77
    /**
78
     * @Route("/", bind="user_list", method="GET")
79
     * @param  Request  $request
80
     * @return Response
81
     */
82
    public function listAction(Request $request)
83
    {
84
        return parent::crudListObjects($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudListObjects() instead of listAction()). Are you sure this is correct? If so, you might want to change this to $this->crudListObjects().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
85
    }
86
87
    /**
88
     * @Route("/create", bind="user_create")
89
     * @param  Request                   $request
90
     * @return Response|RedirectResponse
91
     */
92
    public function createAction(Request $request)
93
    {
94
        return self::crudCreateObject($request);
95
    }
96
97
    /**
98
     * @Route("/edit/{id}", bind="user_edit", asserts={"id"="[0-9a-f]{1,24}"})
99
     * @param  Request                   $request
100
     * @param $id
101
     * @return Response|RedirectResponse
102
     */
103
    public function editAction(Request $request, $id)
104
    {
105
        return self::crudEditObject($request, $id);
106
    }
107
108
    /**
109
     * @Route("/view/{id}", bind="user_view", asserts={"id"="[0-9a-f]{1,24}"})
110
     * @param  Request                   $request
111
     * @param $id
112
     * @return Response|RedirectResponse
113
     */
114
    public function viewAction(Request $request, $id)
115
    {
116
        return self::crudViewObject($request, $id);
117
    }
118
119
    /**
120
     * @Route("/delete/{id}", bind="user_delete", asserts={"id"="[0-9a-f]{1,24}"}, method="GET")
121
     * @param  Request          $request
122
     * @param $id
123
     * @return RedirectResponse
124
     */
125
    public function deleteAction(Request $request, $id)
126
    {
127
        return parent::crudDeleteObject($request, $id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudDeleteObject() instead of deleteAction()). Are you sure this is correct? If so, you might want to change this to $this->crudDeleteObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    protected function crudListRole()
134
    {
135
        return strtoupper('role_admin');
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    protected function crudCreateRole()
142
    {
143
        return strtoupper('role_admin');
144
    }
145
146
    /**
147
     * @param Request $request
148
     * @param object $object
149
     *
150
     * @return FormTypeInterface
151
     */
152
    protected function crudCreateFormType(Request $request, $object)
153
    {
154
        return new UserType($this->crudObjectClass());
155
    }
156
157
    /**
158
     * @param  User          $object
159
     * @param  FormInterface $form
160
     * @param  Request       $request
161
     * @return void
162
     */
163
    protected function crudCreatePrePersist($object, FormInterface $form, Request $request)
164
    {
165
        $this->userManager->update($object);
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    protected function crudEditRole()
172
    {
173
        return strtoupper('role_admin');
174
    }
175
176
    /**
177
     * @param Request $request
178
     * @param object $object
179
     *
180
     * @return FormTypeInterface
181
     */
182
    protected function crudEditFormType(Request $request, $object)
183
    {
184
        return new UserType($this->crudObjectClass());
185
    }
186
187
    /**
188
     * @param  User          $object
189
     * @param  FormInterface $form
190
     * @param  Request       $request
191
     * @return void
192
     */
193
    protected function crudEditPrePersist($object, FormInterface $form, Request $request)
194
    {
195
        $this->userManager->update($object);
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    protected function crudViewRole()
202
    {
203
        return strtoupper('role_admin');
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    protected function crudDeleteRole()
210
    {
211
        return strtoupper('role_admin');
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    protected function crudName()
218
    {
219
        return 'user';
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    protected function crudObjectClass()
226
    {
227
        return User::class;
228
    }
229
}
230