Passed
Push — master ( 2e4256...21a45d )
by Petr
03:39
created

BandController::viewAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 AppBundle\Response\ApiResponse;
19
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Validator\Constraints as Assert;
26
27
/**
28
 * @author Vehsamrak
29
 * @Route("band")
30
 */
31
class BandController extends RestController
32
{
33
34
    /**
35
     * List all registered bands
36
     * @Route("s/{limit}/{offset}", name="bands_list")
37
     * @Method("GET")
38
     * @ApiDoc(
39
     *     section="Band",
40
     *     statusCodes={
41
     *         200="OK",
42
     *     }
43
     * )
44
     * @param int $limit Limit results. Default is 50
45
     * @param int $offset Starting serial number of result collection. Default is 0
46
     */
47 2
    public function listAction($limit = null, $offset = null): Response
48
    {
49 2
        return $this->listEntities($this->get('rockparade.band_repository'), $limit, $offset);
50
    }
51
52
    /**
53
     * View band by id
54
     * @Route("/{id}", name="band_view")
55
     * @Method("GET")
56
     * @ApiDoc(
57
     *     section="Band",
58
     *     statusCodes={
59
     *         200="Band was found",
60
     *         404="Band with given id was not found",
61
     *     }
62
     * )
63
     * @param string $id band name
64
     */
65 3
    public function viewAction(string $id): Response
66
    {
67 3
        return $this->viewEntity($this->get('rockparade.band_repository'), $id);
68
    }
69
70
    /**
71
     * Create new band
72
     * @Route("", name="band_create")
73
     * @Method("POST")
74
     * @Security("has_role('ROLE_USER')")
75
     * @ApiDoc(
76
     *     section="Band",
77
     *     requirements={
78
     *         {
79
     *             "name"="name",
80
     *             "dataType"="string",
81
     *             "requirement"="true",
82
     *             "description"="band name"
83
     *         },
84
     *         {
85
     *             "name"="description",
86
     *             "dataType"="string",
87
     *             "requirement"="true",
88
     *             "description"="band description"
89
     *         },
90
     *         {
91
     *             "name"="members",
92
     *             "dataType"="array",
93
     *             "requirement"="false",
94
     *             "description"="logins and short descriptions of band musicians"
95
     *         },
96
     *     },
97
     *     statusCodes={
98
     *         201="New band was created. Link to new resource provided in header 'Location'",
99
     *         400="Validation error",
100
     *     }
101
     * )
102
     */
103 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...
104
    {
105 2
        $form = $this->createAndProcessForm($request, BandFormType::class);
106
107 2
        $apiResponseFactory = $this->get('rockparade.api_response_factory');
108 2
        $response = $apiResponseFactory->createResponse(
109 2
            $this->createApiOperation($request),
110
            $form,
111 2
            $this->getUser()
112
        );
113
114 2
        return $this->respond($response);
115
    }
116
117
    /**
118
     * Edit band
119
     * @Route("/{id}", name="band_edit")
120
     * @Method("PUT")
121
     * @Security("has_role('ROLE_USER')")
122
     * @ApiDoc(
123
     *     section="Band",
124
     *     requirements={
125
     *         {
126
     *             "name"="name",
127
     *             "dataType"="string",
128
     *             "requirement"="true",
129
     *             "description"="band name"
130
     *         },
131
     *         {
132
     *             "name"="description",
133
     *             "dataType"="string",
134
     *             "requirement"="true",
135
     *             "description"="band description"
136
     *         },
137
     *         {
138
     *             "name"="users",
139
     *             "dataType"="array",
140
     *             "requirement"="true",
141
     *             "description"="logins of band musicians"
142
     *         },
143
     *     },
144
     *     statusCodes={
145
     *         204="Band was edited with new data",
146
     *         400="Validation error",
147
     *         404="Band with given id was not found",
148
     *     }
149
     * )
150
     * @param string $id band id
151
     */
152 2
    public function editAction(Request $request, string $id): Response
153
    {
154
        /** @var BandRepository $bandRepository */
155 2
        $bandRepository = $this->get('rockparade.band_repository');
156
        /** @var Band $band */
157 2
        $band = $bandRepository->findOneById($id);
158
159 2
        $form = $this->createForm(BandFormType::class);
160 2
        $this->processForm($request, $form);
161 2
        $form = $this->get('rockparade.band')->processFormAndUpdateBand($form, $band, $this->getUser());
162
163 2
        return $this->respond($this->createResponseFromUpdateForm($form));
164
    }
165
166
    /**
167
     * List all band members
168
     * @Route("/{id}/members", name="band_members")
169
     * @Method("GET")
170
     * @ApiDoc(
171
     *     section="Band",
172
     *     statusCodes={
173
     *         200="OK",
174
     *         404="Band with given id was not found",
175
     *     }
176
     * )
177
     * @param string $id band id
178
     */
179 4
    public function listMembersAction(string $id): Response
180
    {
181 4
        $bandRepository = $this->get('rockparade.band_repository');
182 4
        $band = $bandRepository->findOneById($id);
183
184 4
        if ($band) {
185 4
            $response = new ApiResponse($band->getMembers(), Response::HTTP_OK);
186
        } else {
187
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
188
        }
189
190 4
        return $this->respond($response);
191
    }
192
193
    /**
194
     * Add member to band
195
     * @Route("/{id}/members", name="band_member_create")
196
     * @Method("POST")
197
     * @Security("has_role('ROLE_USER')")
198
     * @ApiDoc(
199
     *     section="Band",
200
     *     requirements={
201
     *         {
202
     *             "name"="login",
203
     *             "dataType"="string",
204
     *             "requirement"="true",
205
     *             "description"="user login"
206
     *         },
207
     *         {
208
     *             "name"="short_description",
209
     *             "dataType"="string",
210
     *             "requirement"="true",
211
     *             "description"="short description of musicians role in band"
212
     *         },
213
     *         {
214
     *             "name"="description",
215
     *             "dataType"="string",
216
     *             "requirement"="false",
217
     *             "description"="long description of musician"
218
     *         },
219
     *     },
220
     *     statusCodes={
221
     *         200="Member was added to the band",
222
     *         400="Validation error",
223
     *         404="Band with given id was not found",
224
     *     }
225
     * )
226
     * @param string $id band id
227
     */
228 1
    public function createMemberAction(Request $request, string $id): Response
229
    {
230 1
        $form = $this->createForm(BandMemberFormType::class);
231 1
        $this->processForm($request, $form);
232
233 1
        if ($form->isValid()) {
234 1
            $bandRepository = $this->get('rockparade.band_repository');
235 1
            $band = $bandRepository->findOneById($id);
236
237 1
            if (!$band) {
238
                $response = $this->createEntityNotFoundResponse(Band::class, $id);
239
            } else {
240 1
                $newUserLogin = $form->get('login')->getData();
241 1
                $newUser = $this->get('rockparade.user_repository')->findOneByLogin($newUserLogin);
242
243 1
                if (!$newUser) {
244
                    $response = $this->createEntityNotFoundResponse(User::class, $newUserLogin);
245
                } else {
246 1
                    $bandMemberRepository = $this->get('rockparade.band_member_repository');
247 1
                    $shortDescription = (string) $form->get('short_description')->getData();
248 1
                    $description = (string) $form->get('description')->getData();
249 1
                    $bandMember = $bandMemberRepository->getOrCreateByBandAndUser(
250
                        $band,
251
                        $newUser,
252
                        $shortDescription,
253
                        $description
254
                    );
255
256 1
                    $band->addMember($bandMember);
257 1
                    $bandRepository->flush();
258
259 1
                    $response = new EmptyApiResponse(Response::HTTP_OK);
260
                }
261
            }
262
        } else {
263
            $response = new ApiValidationError($form);
264
        }
265
266 1
        return $this->respond($response);
267
    }
268
269
    /**
270
     * Delete member from band
271
     * @Route("/{id}/member/{userLogin}", name="band_member_delete")
272
     * @Method("DELETE")
273
     * @Security("has_role('ROLE_USER')")
274
     * @ApiDoc(
275
     *     section="Band",
276
     *     statusCodes={
277
     *         204="Member was deleted from the band",
278
     *         404="Band or user was not found",
279
     *     }
280
     * )
281
     * @param string $id band id
282
     * @param string $userLogin member login
283
     */
284 1
    public function deleteMemberAction(string $id, string $userLogin)
285
    {
286 1
        $bandRepository = $this->get('rockparade.band_repository');
287 1
        $band = $bandRepository->findOneById($id);
288
289 1
        if ($band) {
290 1
            $userRepository = $this->get('rockparade.user_repository');
291 1
            $user = $userRepository->findOneByLogin($userLogin);
292
293 1
            if ($user) {
294 1
                $bandMemberRepository = $this->get('rockparade.band_member_repository');
295 1
                $bandMember = $bandMemberRepository->findByBandAndUser($band, $user);
296
297 1
                if ($bandMember) {
298 1
                    $band->removeMember($bandMember);
299 1
                    $bandRepository->flush();
300
301 1
                    $response = new EmptyApiResponse(Response::HTTP_NO_CONTENT);
302
                } else {
303 1
                    $response = $this->createEntityNotFoundResponse(BandMember::class, $userLogin);
304
                }
305
            } else {
306 1
                $response = $this->createEntityNotFoundResponse(User::class, $userLogin);
307
            }
308
        } else {
309
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
310
        }
311
312 1
        return $this->respond($response);
313
    }
314
    
315
    /**
316
     * Update band member
317
     * @Route("/{id}/member", name="band_member_update")
318
     * @Method("PUT")
319
     * @Security("has_role('ROLE_USER')")
320
     * @ApiDoc(
321
     *     section="Band",
322
     *     requirements={
323
     *         {
324
     *             "name"="login",
325
     *             "dataType"="string",
326
     *             "requirement"="true",
327
     *             "description"="login of musician"
328
     *         },
329
     *         {
330
     *             "name"="short_description",
331
     *             "dataType"="string",
332
     *             "requirement"="true",
333
     *             "description"="short description of role in band"
334
     *         },
335
     *         {
336
     *             "name"="description",
337
     *             "dataType"="string",
338
     *             "requirement"="false",
339
     *             "description"="long description of musician"
340
     *         },
341
     *     },
342
     *     statusCodes={
343
     *         204="Band member was successfully updated",
344
     *         404="Band or user was not found",
345
     *     }
346
     * )
347
     * @param string $id band id
348
     * @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...
349
     */
350 1
    public function updateMemberAction(Request $request, string $id)
351
    {
352 1
        $bandRepository = $this->get('rockparade.band_repository');
353 1
        $band = $bandRepository->findOneById($id);
354
355 1
        if ($band) {
356 1
            $userLogin = $request->get('login');
357 1
            $userRepository = $this->get('rockparade.user_repository');
358 1
            $user = $userRepository->findOneByLogin($userLogin);
359
360 1
            if ($user) {
361 1
                $bandMemberRepository = $this->get('rockparade.band_member_repository');
362 1
                $bandMember = $bandMemberRepository->findByBandAndUser($band, $user);
363
                
364 1
                if ($bandMember) {
365 1
                    $form = $this->createForm(BandMemberFormType::class);
366 1
                    $this->processForm($request, $form);
367 1
                    $form = $this->get('rockparade.band')->processFormAndUpdateBandMember($form, $bandMember);
368
                    
369 1
                    $bandRepository->flush();
370
371 1
                    $response = $this->createResponseFromUpdateForm($form);
372
                } else {
373 1
                    $response = $this->createEntityNotFoundResponse(BandMember::class, $userLogin);
374
                }
375
            } else {
376 1
                $response = $this->createEntityNotFoundResponse(User::class, $userLogin);
377
            }
378
        } else {
379
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
380
        }
381
382 1
        return $this->respond($response);
383
    }
384
385
    /**
386
     * @return ApiError|CreatedApiResponse|EmptyApiResponse
387
     */
388 3
    private function createResponseFromUpdateForm(FormInterface $form): AbstractApiResponse
389
    {
390 3
        if ($form->isValid()) {
391 2
            return new EmptyApiResponse(Response::HTTP_NO_CONTENT);
392
        } else {
393 1
            return new ApiValidationError($form);
394
        }
395
    }
396
}
397