BandController::updateMemberAction()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0016

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 20
cts 21
cp 0.9524
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 4
nop 1
crap 4.0016
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Band;
6
use AppBundle\Entity\BandMember;
7
use AppBundle\Entity\Repository\BandRepository;
8
use AppBundle\Entity\User;
9
use AppBundle\Form\Ambassador\BandFormType;
10
use AppBundle\Form\Ambassador\BandMemberFormType;
11
use AppBundle\Response\ApiValidationError;
12
use AppBundle\Response\CreatedApiResponse;
13
use AppBundle\Response\EmptyApiResponse;
14
use AppBundle\Response\Infrastructure\AbstractApiResponse;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
16
use AppBundle\Controller\Infrastructure\RestController;
17
use AppBundle\Response\ApiError;
18
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
25
/**
26
 * @author Vehsamrak
27
 * @Route("band")
28
 */
29
class BandController extends RestController
30
{
31
32
    /**
33
     * List all registered bands
34
     * @Route("s/{limit}/{offset}", name="bands_list")
35
     * @Method("GET")
36
     * @ApiDoc(
37
     *     section="Band",
38
     *     statusCodes={
39
     *         200="OK",
40
     *     }
41
     * )
42
     * @param int $limit Limit results. Default is 50
43
     * @param int $offset Starting serial number of result collection. Default is 0
44
     */
45 2
    public function listAction($limit = null, $offset = null): Response
46
    {
47 2
        return $this->listEntities($this->get('rockparade.band_repository'), $limit, $offset);
48
    }
49
50
    /**
51
     * View band by id
52
     * @Route("/{id}", name="band_view")
53
     * @Method("GET")
54
     * @ApiDoc(
55
     *     section="Band",
56
     *     statusCodes={
57
     *         200="Band was found",
58
     *         404="Band with given id was not found",
59
     *     }
60
     * )
61
     * @param string $id band name
62
     */
63 6
    public function viewAction(string $id): Response
64
    {
65 6
        return $this->viewEntity($this->get('rockparade.band_repository'), $id);
66
    }
67
68
    /**
69
     * Create new band
70
     * @Route("", name="band_create")
71
     * @Method("POST")
72
     * @Security("has_role('ROLE_USER')")
73
     * @ApiDoc(
74
     *     section="Band",
75
     *     requirements={
76
     *         {
77
     *             "name"="name",
78
     *             "dataType"="string",
79
     *             "requirement"="true",
80
     *             "description"="band name"
81
     *         },
82
     *         {
83
     *             "name"="description",
84
     *             "dataType"="string",
85
     *             "requirement"="true",
86
     *             "description"="band description"
87
     *         },
88
     *         {
89
     *             "name"="members",
90
     *             "dataType"="array",
91
     *             "requirement"="false",
92
     *             "description"="logins and short descriptions of band musicians"
93
     *         },
94
     *     },
95
     *     statusCodes={
96
     *         201="New band was created. Link to new resource provided in header 'Location'",
97
     *         400="Validation error",
98
     *     }
99
     * )
100
     */
101 2 View Code Duplication
    public function createAction(Request $request): Response
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...
102
    {
103 2
        $form = $this->createAndProcessForm($request, BandFormType::class);
104
105 2
        $apiResponseFactory = $this->get('rockparade.api_response_factory');
106 2
        $response = $apiResponseFactory->createResponse(
107 2
            $this->createApiOperation($request),
108
            $form,
109 2
            $this->getUser()
110
        );
111
112 2
        return $this->respond($response);
113
    }
114
115
    /**
116
     * Edit band
117
     * @Route("/{id}", name="band_edit")
118
     * @Method("PUT")
119
     * @Security("has_role('ROLE_USER')")
120
     * @ApiDoc(
121
     *     section="Band",
122
     *     requirements={
123
     *         {
124
     *             "name"="name",
125
     *             "dataType"="string",
126
     *             "requirement"="true",
127
     *             "description"="band name"
128
     *         },
129
     *         {
130
     *             "name"="description",
131
     *             "dataType"="string",
132
     *             "requirement"="true",
133
     *             "description"="band description"
134
     *         },
135
     *         {
136
     *             "name"="users",
137
     *             "dataType"="array",
138
     *             "requirement"="true",
139
     *             "description"="logins of band musicians"
140
     *         },
141
     *     },
142
     *     statusCodes={
143
     *         204="Band was edited with new data",
144
     *         400="Validation error",
145
     *         404="Band with given id was not found",
146
     *     }
147
     * )
148
     * @param string $id band id
149
     */
150 2
    public function editAction(Request $request, string $id): Response
151
    {
152
        /** @var BandRepository $bandRepository */
153 2
        $bandRepository = $this->get('rockparade.band_repository');
154
        /** @var Band $band */
155 2
        $band = $bandRepository->findOneById($id);
156
157 2
        $form = $this->createForm(BandFormType::class);
158 2
        $this->processForm($request, $form);
159 2
        $form = $this->get('rockparade.band')->processFormAndUpdateBand($form, $band, $this->getUser());
160
161 2
        return $this->respond($this->createResponseFromUpdateForm($form));
162
    }
163
164
    /**
165
     * Add member to band
166
     * @Route("/members", name="band_member_create")
167
     * @Method("POST")
168
     * @Security("has_role('ROLE_USER')")
169
     * @ApiDoc(
170
     *     section="Band",
171
     *     requirements={
172
     *         {
173
     *             "name"="ambassador",
174
     *             "dataType"="string",
175
     *             "requirement"="true",
176
     *             "description"="band id"
177
     *         },
178
     *         {
179
     *             "name"="login",
180
     *             "dataType"="string",
181
     *             "requirement"="true",
182
     *             "description"="user login"
183
     *         },
184
     *         {
185
     *             "name"="short_description",
186
     *             "dataType"="string",
187
     *             "requirement"="true",
188
     *             "description"="short description of musicians role in band"
189
     *         },
190
     *         {
191
     *             "name"="description",
192
     *             "dataType"="string",
193
     *             "requirement"="false",
194
     *             "description"="long description of musician"
195
     *         },
196
     *     },
197
     *     statusCodes={
198
     *         200="Member was added to the band",
199
     *         400="Validation error",
200
     *         404="Band or User was not found",
201
     *     }
202
     * )
203
     */
204 1 View Code Duplication
    public function createMemberAction(Request $request): Response
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...
205
    {
206 1
        $form = $this->createAndProcessForm($request, BandMemberFormType::class);
207
208 1
        $apiResponseFactory = $this->get('rockparade.api_response_factory');
209 1
        $response = $apiResponseFactory->createResponse(
210 1
            $this->createApiOperation($request),
211
            $form,
212 1
            $this->getUser()
213
        );
214
215 1
        return $this->respond($response);
216
    }
217
218
    /**
219
     * Delete member from band
220
     * @Route("/{id}/member/{userLogin}", name="band_member_delete")
221
     * @Method("DELETE")
222
     * @Security("has_role('ROLE_USER')")
223
     * @ApiDoc(
224
     *     section="Band",
225
     *     statusCodes={
226
     *         204="Member was deleted from the band",
227
     *         404="Band or user was not found",
228
     *     }
229
     * )
230
     * @param string $id band id
231
     * @param string $userLogin member login
232
     */
233 1
    public function deleteMemberAction(string $id, string $userLogin)
234
    {
235 1
        $bandRepository = $this->get('rockparade.band_repository');
236 1
        $band = $bandRepository->findOneById($id);
237
238 1
        if ($band) {
239 1
            $userRepository = $this->get('rockparade.user_repository');
240 1
            $user = $userRepository->findOneByLogin($userLogin);
241
242 1
            if ($user) {
243 1
                $bandMemberRepository = $this->get('rockparade.band_member_repository');
244 1
                $bandMember = $bandMemberRepository->findByAmbassadorAndUser($band, $user);
245
246 1 View Code Duplication
                if ($bandMember) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
247 1
                    $band->removeMember($bandMember);
248 1
                    $bandRepository->flush();
249
250 1
                    $response = new EmptyApiResponse(Response::HTTP_NO_CONTENT);
251
                } else {
252 1
                    $response = $this->createEntityNotFoundResponse(BandMember::class, $userLogin);
253
                }
254
            } else {
255 1
                $response = $this->createEntityNotFoundResponse(User::class, $userLogin);
256
            }
257
        } else {
258
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
259
        }
260
261 1
        return $this->respond($response);
262
    }
263
    
264
    /**
265
     * Update band member
266
     * @Route("/{id}/member", name="band_member_update")
267
     * @Method("PUT")
268
     * @Security("has_role('ROLE_USER')")
269
     * @ApiDoc(
270
     *     section="Band",
271
     *     requirements={
272
     *         {
273
     *             "name"="ambassador",
274
     *             "dataType"="string",
275
     *             "requirement"="true",
276
     *             "description"="band id"
277
     *         },
278
     *         {
279
     *             "name"="login",
280
     *             "dataType"="string",
281
     *             "requirement"="true",
282
     *             "description"="login of musician"
283
     *         },
284
     *         {
285
     *             "name"="short_description",
286
     *             "dataType"="string",
287
     *             "requirement"="true",
288
     *             "description"="short description of role in band"
289
     *         },
290
     *         {
291
     *             "name"="description",
292
     *             "dataType"="string",
293
     *             "requirement"="false",
294
     *             "description"="long description of musician"
295
     *         },
296
     *     },
297
     *     statusCodes={
298
     *         204="Band member was successfully updated",
299
     *         404="Band or user was not found",
300
     *     }
301
     * )
302
     * @param string $userLogin member login
0 ignored issues
show
Bug introduced by
There is no parameter named $userLogin. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
303
     */
304 1
    public function updateMemberAction(Request $request)
305
    {
306 1
        $id = $request->get('ambassador');
307
308 1
        $bandRepository = $this->get('rockparade.band_repository');
309 1
        $band = $bandRepository->findOneById($id);
310
311 1
        if ($band) {
312 1
            $userLogin = $request->get('login');
313 1
            $userRepository = $this->get('rockparade.user_repository');
314 1
            $user = $userRepository->findOneByLogin($userLogin);
315
316 1
            if ($user) {
317 1
                $bandMemberRepository = $this->get('rockparade.band_member_repository');
318 1
                $bandMember = $bandMemberRepository->findByAmbassadorAndUser($band, $user);
319
                
320 1
                if ($bandMember) {
321 1
                    $form = $this->createForm(BandMemberFormType::class);
322 1
                    $this->processForm($request, $form);
323 1
                    $form = $this->get('rockparade.band')->processFormAndUpdateBandMember($form, $bandMember);
324
                    
325 1
                    $bandRepository->flush();
326
327 1
                    $response = $this->createResponseFromUpdateForm($form);
328
                } else {
329 1
                    $response = $this->createEntityNotFoundResponse(BandMember::class, $userLogin);
330
                }
331
            } else {
332 1
                $response = $this->createEntityNotFoundResponse(User::class, $userLogin);
333
            }
334
        } else {
335
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
336
        }
337
338 1
        return $this->respond($response);
339
    }
340
341
    /**
342
     * @return ApiError|CreatedApiResponse|EmptyApiResponse
343
     */
344 3
    private function createResponseFromUpdateForm(FormInterface $form): AbstractApiResponse
345
    {
346 3
        if ($form->isValid()) {
347 2
            return new EmptyApiResponse(Response::HTTP_NO_CONTENT);
348
        } else {
349 1
            return new ApiValidationError($form);
350
        }
351
    }
352
}
353