ComestibleController::crudListFormDataEnrich()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Dominikzogg\EnergyCalculator\Controller;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Dominikzogg\EnergyCalculator\Entity\Comestible;
7
use Dominikzogg\EnergyCalculator\Form\ComestibleListType;
8
use Dominikzogg\EnergyCalculator\Form\ComestibleType;
9
use Dominikzogg\EnergyCalculator\Repository\ComestibleRepository;
10
use Knp\Component\Pager\Paginator;
11
use Saxulum\Crud\Listing\ListingFactory;
12
use Saxulum\RouteController\Annotation\DI;
13
use Saxulum\RouteController\Annotation\Route;
14
use Symfony\Component\Form\FormFactory;
15
use Symfony\Component\Form\FormTypeInterface;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\PropertyAccess\PropertyAccessor;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
24
use Symfony\Component\Security\Core\SecurityContextInterface;
25
26
/**
27
 * @Route("/{_locale}/comestible", asserts={"_locale"="([a-z]{2}|[a-z]{2}_[A-Z]{2})"})
28
 * @DI(serviceIds={
29
 *      "security.authorization_checker",
30
 *      "security.token_storage",
31
 *      "saxulum.crud.listing.factory",
32
 *      "doctrine",
33
 *      "form.factory",
34
 *      "knp_paginator",
35
 *      "url_generator",
36
 *      "twig"
37
 * })
38
 */
39
class ComestibleController extends AbstractCRUDController
40
{
41
    /**
42
     * @param AuthorizationCheckerInterface $authorizationChecker
43
     * @param TokenStorageInterface $tokenStorage
44
     * @param ListingFactory $listingFactory
45
     * @param ManagerRegistry          $doctrine
46
     * @param FormFactory              $formFactory
47
     * @param Paginator                $paginator
48
     * @param UrlGeneratorInterface    $urlGenerator
49
     * @param \Twig_Environment        $twig
50
     */
51 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...
52
        AuthorizationCheckerInterface $authorizationChecker,
53
        TokenStorageInterface $tokenStorage,
54
        ListingFactory $listingFactory,
55
        ManagerRegistry $doctrine,
56
        FormFactory $formFactory,
57
        Paginator $paginator,
58
        UrlGeneratorInterface $urlGenerator,
59
        \Twig_Environment $twig
60
    ) {
61
        $this->authorizationChecker = $authorizationChecker;
62
        $this->tokenStorage = $tokenStorage;
63
        $this->listingFactory = $listingFactory;
64
        $this->doctrine = $doctrine;
65
        $this->formFactory = $formFactory;
66
        $this->paginator = $paginator;
67
        $this->urlGenerator = $urlGenerator;
68
        $this->twig = $twig;
69
    }
70
71
    /**
72
     * @Route("/", bind="comestible_list", method="GET")
73
     * @param  Request  $request
74
     * @return Response
75
     */
76
    public function listAction(Request $request)
77
    {
78
        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...
79
    }
80
81
    /**
82
     * @Route("/create", bind="comestible_create")
83
     * @param  Request                   $request
84
     * @return Response|RedirectResponse
85
     */
86
    public function createAction(Request $request)
87
    {
88
        return parent::crudCreateObject($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudCreateObject() instead of createAction()). Are you sure this is correct? If so, you might want to change this to $this->crudCreateObject().

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...
89
    }
90
91
    /**
92
     * @Route("/edit/{id}", bind="comestible_edit", asserts={"id"="[0-9a-f]{1,24}"})
93
     * @param  Request                   $request
94
     * @param $id
95
     * @return Response|RedirectResponse
96
     */
97
    public function editAction(Request $request, $id)
98
    {
99
        return parent::crudEditObject($request, $id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudEditObject() instead of editAction()). Are you sure this is correct? If so, you might want to change this to $this->crudEditObject().

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...
100
    }
101
102
    /**
103
     * @Route("/delete/{id}", bind="comestible_delete", asserts={"id"="[0-9a-f]{1,24}"}, method="GET")
104
     * @param  Request          $request
105
     * @param $id
106
     * @return RedirectResponse
107
     */
108
    public function deleteAction(Request $request, $id)
109
    {
110
        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...
111
    }
112
113
    /**
114
     * @Route("/choice", bind="comestible_choice", method="GET")
115
     * @param  Request          $request
116
     * @return RedirectResponse
117
     */
118
    public function choiceAction(Request $request)
119
    {
120
        $choiceLabel = $request->query->get('choice_label', 'name');
121
        $search = urldecode($request->query->get('q', ''));
122
123
        /** @var ComestibleRepository $repo */
124
        $repo = $this->crudRepositoryForClass($this->crudObjectClass());
125
        $propertyAccessor = new PropertyAccessor();
126
        $data = array();
127
        foreach ($repo->searchComestibleOfUser($this->getUser(), $search) as $comestible) {
128
            $data[] = array(
129
                'id' => $comestible->getId(),
130
                'text' => $propertyAccessor->getValue($comestible, $choiceLabel),
131
                'default' => $comestible->getDefaultValue(),
132
            );
133
        }
134
135
        return new JsonResponse($data);
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    protected function crudListPerPage()
142
    {
143
        return 20;
144
    }
145
146
    /**
147
     * @param Request $request
148
     *
149
     * @return FormTypeInterface
150
     */
151
    protected function crudListFormType(Request $request)
152
    {
153
        return new ComestibleListType();
154
    }
155
156
    /**
157
     * @param  Request $request
158
     * @param  array   $formData
159
     * @return array
160
     */
161
    protected function crudListFormDataEnrich(Request $request, array $formData)
162
    {
163
        return array_replace_recursive($formData, array(
164
            'user' => $this->getUser()->getId(),
165
        ));
166
    }
167
168
    /**
169
     * @param  Request    $request
170
     * @return Comestible
171
     */
172
    protected function crudCreateFactory(Request $request)
173
    {
174
        $objectClass = $this->crudObjectClass();
175
176
        /** @var Comestible $object */
177
        $object = new $objectClass();
178
        $object->setUser($this->getUser());
179
180
        return $object;
181
    }
182
183
    /**
184
     * @param Request $request
185
     * @param object $object
186
     *
187
     * @return FormTypeInterface
188
     */
189
    protected function crudCreateFormType(Request $request, $object)
190
    {
191
        return new ComestibleType();
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    protected function crudEditRole()
198
    {
199
        return strtoupper('edit');
200
    }
201
202
    /**
203
     * @param Request $request
204
     * @param object $object
205
     *
206
     * @return FormTypeInterface
207
     */
208
    protected function crudEditFormType(Request $request, $object)
209
    {
210
        return new ComestibleType();
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    protected function crudViewRoute()
217
    {
218
        return '';
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    protected function crudDeleteRole()
225
    {
226
        return strtoupper('delete');
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    protected function crudName()
233
    {
234
        return 'comestible';
235
    }
236
237
    /**
238
     * @return string
239
     */
240
    protected function crudObjectClass()
241
    {
242
        return Comestible::class;
243
    }
244
}
245