Passed
Push — master ( 21a45d...334091 )
by Petr
03:48
created

BandController::createMemberAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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("/members", name="band_member_create")
196
     * @Method("POST")
197
     * @Security("has_role('ROLE_USER')")
198
     * @ApiDoc(
199
     *     section="Band",
200
     *     requirements={
201
     *         {
202
     *             "name"="ambassador",
203
     *             "dataType"="string",
204
     *             "requirement"="true",
205
     *             "description"="band id"
206
     *         },
207
     *         {
208
     *             "name"="login",
209
     *             "dataType"="string",
210
     *             "requirement"="true",
211
     *             "description"="user login"
212
     *         },
213
     *         {
214
     *             "name"="short_description",
215
     *             "dataType"="string",
216
     *             "requirement"="true",
217
     *             "description"="short description of musicians role in band"
218
     *         },
219
     *         {
220
     *             "name"="description",
221
     *             "dataType"="string",
222
     *             "requirement"="false",
223
     *             "description"="long description of musician"
224
     *         },
225
     *     },
226
     *     statusCodes={
227
     *         200="Member was added to the band",
228
     *         400="Validation error",
229
     *         404="Band or User was not found",
230
     *     }
231
     * )
232
     */
233 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...
234
    {
235 1
        $form = $this->createAndProcessForm($request, BandMemberFormType::class);
236
237 1
        $apiResponseFactory = $this->get('rockparade.api_response_factory');
238 1
        $response = $apiResponseFactory->createResponse(
239 1
            $this->createApiOperation($request),
240
            $form,
241 1
            $this->getUser()
242
        );
243
244 1
        return $this->respond($response);
245
    }
246
247
    /**
248
     * Delete member from band
249
     * @Route("/{id}/member/{userLogin}", name="band_member_delete")
250
     * @Method("DELETE")
251
     * @Security("has_role('ROLE_USER')")
252
     * @ApiDoc(
253
     *     section="Band",
254
     *     statusCodes={
255
     *         204="Member was deleted from the band",
256
     *         404="Band or user was not found",
257
     *     }
258
     * )
259
     * @param string $id band id
260
     * @param string $userLogin member login
261
     */
262 1
    public function deleteMemberAction(string $id, string $userLogin)
263
    {
264 1
        $bandRepository = $this->get('rockparade.band_repository');
265 1
        $band = $bandRepository->findOneById($id);
266
267 1
        if ($band) {
268 1
            $userRepository = $this->get('rockparade.user_repository');
269 1
            $user = $userRepository->findOneByLogin($userLogin);
270
271 1
            if ($user) {
272 1
                $bandMemberRepository = $this->get('rockparade.band_member_repository');
273 1
                $bandMember = $bandMemberRepository->findByAmbassadorAndUser($band, $user);
274
275 1
                if ($bandMember) {
276 1
                    $band->removeMember($bandMember);
277 1
                    $bandRepository->flush();
278
279 1
                    $response = new EmptyApiResponse(Response::HTTP_NO_CONTENT);
280
                } else {
281 1
                    $response = $this->createEntityNotFoundResponse(BandMember::class, $userLogin);
282
                }
283
            } else {
284 1
                $response = $this->createEntityNotFoundResponse(User::class, $userLogin);
285
            }
286
        } else {
287
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
288
        }
289
290 1
        return $this->respond($response);
291
    }
292
    
293
    /**
294
     * Update band member
295
     * @Route("/{id}/member", name="band_member_update")
296
     * @Method("PUT")
297
     * @Security("has_role('ROLE_USER')")
298
     * @ApiDoc(
299
     *     section="Band",
300
     *     requirements={
301
     *         {
302
     *             "name"="ambassador",
303
     *             "dataType"="string",
304
     *             "requirement"="true",
305
     *             "description"="band id"
306
     *         },
307
     *         {
308
     *             "name"="login",
309
     *             "dataType"="string",
310
     *             "requirement"="true",
311
     *             "description"="login of musician"
312
     *         },
313
     *         {
314
     *             "name"="short_description",
315
     *             "dataType"="string",
316
     *             "requirement"="true",
317
     *             "description"="short description of role in band"
318
     *         },
319
     *         {
320
     *             "name"="description",
321
     *             "dataType"="string",
322
     *             "requirement"="false",
323
     *             "description"="long description of musician"
324
     *         },
325
     *     },
326
     *     statusCodes={
327
     *         204="Band member was successfully updated",
328
     *         404="Band or user was not found",
329
     *     }
330
     * )
331
     * @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...
332
     */
333 1
    public function updateMemberAction(Request $request)
334
    {
335 1
        $id = $request->get('ambassador');
336
337 1
        $bandRepository = $this->get('rockparade.band_repository');
338 1
        $band = $bandRepository->findOneById($id);
339
340 1
        if ($band) {
341 1
            $userLogin = $request->get('login');
342 1
            $userRepository = $this->get('rockparade.user_repository');
343 1
            $user = $userRepository->findOneByLogin($userLogin);
344
345 1
            if ($user) {
346 1
                $bandMemberRepository = $this->get('rockparade.band_member_repository');
347 1
                $bandMember = $bandMemberRepository->findByAmbassadorAndUser($band, $user);
348
                
349 1
                if ($bandMember) {
350 1
                    $form = $this->createForm(BandMemberFormType::class);
351 1
                    $this->processForm($request, $form);
352 1
                    $form = $this->get('rockparade.band')->processFormAndUpdateBandMember($form, $bandMember);
353
                    
354 1
                    $bandRepository->flush();
355
356 1
                    $response = $this->createResponseFromUpdateForm($form);
357
                } else {
358 1
                    $response = $this->createEntityNotFoundResponse(BandMember::class, $userLogin);
359
                }
360
            } else {
361 1
                $response = $this->createEntityNotFoundResponse(User::class, $userLogin);
362
            }
363
        } else {
364
            $response = $this->createEntityNotFoundResponse(Band::class, $id);
365
        }
366
367 1
        return $this->respond($response);
368
    }
369
370
    /**
371
     * @return ApiError|CreatedApiResponse|EmptyApiResponse
372
     */
373 3
    private function createResponseFromUpdateForm(FormInterface $form): AbstractApiResponse
374
    {
375 3
        if ($form->isValid()) {
376 2
            return new EmptyApiResponse(Response::HTTP_NO_CONTENT);
377
        } else {
378 1
            return new ApiValidationError($form);
379
        }
380
    }
381
}
382