Passed
Push — master ( 334091...a751e7 )
by Petr
03:39
created

BandController::listMembersAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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