Passed
Push — feature/#96-api-remove-doc ( 1e8b5a )
by FX
04:00
created

CampaignApiController::postCampaignAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 3
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace AppBundle\Controller;
24
25
use AppBundle\DTO\CampaignDTO;
26
use AppBundle\Entity\Campaign;
27
use AppBundle\Entity\Project;
28
use FOS\RestBundle\Controller\Annotations as Rest;
29
use FOS\RestBundle\View\View;
30
use Nelmio\ApiDocBundle\Annotation as Doc;
31
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpFoundation\Response;
34
35
/**
36
 * Campaign API controller class.
37
 *
38
 * @category  ci-report app
39
 *
40
 * @author    Francois-Xavier Soubirou <[email protected]>
41
 * @copyright 2017 Francois-Xavier Soubirou
42
 * @license   http://www.gnu.org/licenses/   GPLv3
43
 *
44
 * @see      https://www.ci-report.io
45
 *
46
 * @Rest\Route("/api")
47
 */
48
class CampaignApiController extends AbstractApiController
49
{
50
    /**
51
     * Get list of campaigns for a project. Example: </br>
52
     * <pre style="background:black; color:white; font-size:10px;"><code style="background:black;">curl https://www.ci-report.io/api/projects/project-one/campaigns -X GET
53
     * </code></pre>.
54
     *
55
     * @param Project $project Project
56
     *
57
     * @return array
58
     *
59
     * @Rest\Get("/projects/{prefid}/campaigns")
60
     * @Rest\View(serializerGroups={"public"})
61
     *
62
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
63
     *
64
     * @Doc\ApiDoc(
65
     *     section="Campaigns",
66
     *     description="Get the list of all campaigns for a project.",
67
     *     requirements={
68
     *         {
69
     *             "name"="prefid",
70
     *             "dataType"="string",
71
     *             "requirement"="string",
72
     *             "description"="Unique short name of project defined on project creation."
73
     *         }
74
     *     },
75
     *     output={
76
     *         "class"=Campaign::class,
77
     *         "groups"={"public"},
78
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
79
     *     },
80
     *     statusCodes={
81
     *         200="Returned when successful array of public data of campaigns",
82
     *         404="Returned when project not found"
83
     *     }
84
     * )
85
     */
86
    public function getCampaignsAction(Project $project): array
87
    {
88
        $campaigns = $this->getDoctrine()
89
            ->getRepository(Campaign::class)
90
            ->findCampaignsByProject($project);
91
92
        return $campaigns;
93
    }
94
95
    /**
96
     * Get campaign data. Example: </br>
97
     * <pre style="background:black; color:white; font-size:10px;"><code style="background:black;">curl https://www.ci-report.io/api/projects/project-one/campaigns/1 -X GET
98
     * </code></pre>.
99
     *
100
     * @param Campaign $campaign Campaign
101
     *
102
     * @return Campaign
103
     *
104
     * @Rest\Get(
105
     *    "/projects/{prefid}/campaigns/{crefid}",
106
     *    requirements={"crefid" = "\d+"}
107
     * )
108
     * @Rest\View(serializerGroups={"public"})
109
     *
110
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
111
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
112
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
113
     *    "map_method_signature" = true
114
     * })
115
     *
116
     * @Doc\ApiDoc(
117
     *     section="Campaigns",
118
     *     description="Get campaign data.",
119
     *     requirements={
120
     *         {
121
     *             "name"="prefid",
122
     *             "dataType"="string",
123
     *             "requirement"="string",
124
     *             "description"="Unique short name of project defined on project creation."
125
     *         },
126
     *         {
127
     *             "name"="crefid",
128
     *             "dataType"="int",
129
     *             "requirement"="int",
130
     *             "description"="Reference id of the campaign."
131
     *         }
132
     *     },
133
     *     output= {
134
     *         "class"=Campaign::class,
135
     *         "groups"={"public"},
136
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
137
     *     },
138
     *     statusCodes={
139
     *         200="Returned when successful",
140
     *         404="Returned when project or campaign not found"
141
     *     }
142
     * )
143
     */
144
    public function getCampaignAction(Campaign $campaign): Campaign
145
    {
146
        return $campaign;
147
    }
148
149
    /**
150
     * Get last added campaign data. Example: </br>
151
     * <pre style="background:black; color:white; font-size:10px;"><code style="background:black;">curl https://www.ci-report.io/api/projects/project-one/campaigns/last -X GET
152
     * </code></pre>.
153
     *
154
     * @param Campaign $campaign Campaign
155
     *
156
     * @return Campaign
157
     *
158
     * @Rest\Get("/projects/{prefid}/campaigns/last")
159
     * @Rest\View(serializerGroups={"public"})
160
     *
161
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
162
     *    "repository_method" = "findLastCampaignByProjectRefid",
163
     *    "mapping": {"prefid": "prefid"},
164
     *    "map_method_signature" = true
165
     * })
166
     *
167
     * @Doc\ApiDoc(
168
     *     section="Campaigns",
169
     *     description="Get last added campaign data.",
170
     *     requirements={
171
     *         {
172
     *             "name"="prefid",
173
     *             "dataType"="string",
174
     *             "requirement"="string",
175
     *             "description"="Unique short name of project defined on project creation."
176
     *         }
177
     *     },
178
     *     output= {
179
     *         "class"=Campaign::class,
180
     *         "groups"={"public"},
181
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
182
     *     },
183
     *     statusCodes={
184
     *         200="Returned when successful",
185
     *         404="Returned when no campaign exists for project"
186
     *     }
187
     * )
188
     */
189
    public function getLastCampaignAction(Campaign $campaign): Campaign
190
    {
191
        return $campaign;
192
    }
193
194
    /**
195
     * Create a campaign. Example: </br>
196
     * <pre style="background:black; color:white; font-size:10px;"><code style="background:black;">curl https://www.ci-report.io/api/projects/project-one/campaigns -H "Content-Type: application/json" -H "X-CIR-TKN: 1f4ffb19e4b9-02278af07b7d-4e370a76f001" -X POST --data '{"warning":80, "success":95, "start":"2017-07-01 12:30:01", "end":"2017-07-03 12:30:01"}'
197
     * </code></pre>.
198
     *
199
     * @param Project     $project     Project
200
     * @param CampaignDTO $campaignDTO Campaign to create
201
     * @param Request     $request     The request
202
     *
203
     * @return Campaign|View
204
     *
205
     * @Rest\Post("/projects/{prefid}/campaigns")
206
     * @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"public"})
207
     *
208
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
209
     * @ParamConverter("campaignDTO", converter="fos_rest.request_body")
210
     *
211
     * @Doc\ApiDoc(
212
     *     section="Campaigns",
213
     *     description="Create a campaign.",
214
     *     headers={
215
     *         {
216
     *             "name"="Content-Type",
217
     *             "required"=true,
218
     *             "description"="Type of content: application/json"
219
     *         },
220
     *         {
221
     *             "name"="X-CIR-TKN",
222
     *             "required"=true,
223
     *             "description"="Private token"
224
     *         }
225
     *     },
226
     *     requirements={
227
     *         {
228
     *             "name"="prefid",
229
     *             "dataType"="string",
230
     *             "requirement"="string",
231
     *             "description"="Unique short name of project defined on project creation."
232
     *         }
233
     *     },
234
     *     input= { "class"=CampaignDTO::class },
235
     *     output= {
236
     *         "class"=Campaign::class,
237
     *         "groups"={"public"},
238
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
239
     *     },
240
     *     statusCodes={
241
     *         201="Returned when created",
242
     *         400="Returned when a violation is raised by validation",
243
     *         401="Returned when X-CIR-TKN private token value is invalid",
244
     *         404="Returned when project not found"
245
     *     },
246
     *     tags={
247
     *         "token" = "#2c3e50"
248
     *     }
249
     * )
250
     */
251
    public function postCampaignAction(Project $project, CampaignDTO $campaignDTO, Request $request)
252
    {
253
        if ($this->isInvalidToken($request, $project->getToken())) {
254
            return $this->getInvalidTokenView();
255
        }
256
257
        $validator = $this->get('validator');
258
        $violationsDTO = $validator->validate($campaignDTO);
259
260
        if (count($violationsDTO) > 0) {
261
            return $this->view($violationsDTO, Response::HTTP_BAD_REQUEST);
262
        }
263
        $campaign = new Campaign($project);
264
        $campaign->setFromDTO($campaignDTO);
265
266
        $violations = $validator->validate($campaign);
267
        if (count($violations) > 0) {
268
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
269
        }
270
        $em = $this->getDoctrine()->getManager();
271
        $em->persist($campaign);
272
        $em->flush();
273
274
        return $campaign;
275
    }
276
277
    /**
278
     * Update a campaign. Example: </br>
279
     * <pre style="background:black; color:white; font-size:10px;"><code style="background:black;">curl https://www.ci-report.io/api/projects/project-one/campaigns/1 -H "Content-Type: application/json" -H "X-CIR-TKN: 1f4ffb19e4b9-02278af07b7d-4e370a76f001" -X PUT --data '{"start":"2017-07-01 12:30:01", "end":"2017-07-03 12:30:01"}'
280
     * </code></pre>.
281
     *
282
     * @param Campaign    $campaignDB  Campaign to update
283
     * @param CampaignDTO $campaignDTO Object containing input data
284
     * @param Request     $request     The request
285
     *
286
     * @return Campaign|View
287
     *
288
     * @Rest\Put(
289
     *    "/projects/{prefid}/campaigns/{crefid}",
290
     *    requirements={"crefid" = "\d+"}
291
     * )
292
     * @Rest\View(serializerGroups={"public"})
293
     *
294
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
295
     * @ParamConverter("campaignDB", class="AppBundle:Campaign", options={
296
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
297
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
298
     *    "map_method_signature" = true
299
     * })
300
     * @ParamConverter("campaignDTO", converter="fos_rest.request_body")
301
     *
302
     * @Doc\ApiDoc(
303
     *     section="Campaigns",
304
     *     description="Update a campaign.",
305
     *     headers={
306
     *         {
307
     *             "name"="Content-Type",
308
     *             "required"=true,
309
     *             "description"="Type of content: application/json"
310
     *         },
311
     *         {
312
     *             "name"="X-CIR-TKN",
313
     *             "required"=true,
314
     *             "description"="Private token"
315
     *         }
316
     *     },
317
     *     requirements={
318
     *         {
319
     *             "name"="prefid",
320
     *             "dataType"="string",
321
     *             "requirement"="string",
322
     *             "description"="Unique short name of project defined on project creation."
323
     *         },
324
     *         {
325
     *             "name"="crefid",
326
     *             "dataType"="int",
327
     *             "requirement"="int",
328
     *             "description"="Reference id of the campaign."
329
     *         }
330
     *     },
331
     *     input= { "class"=CampaignDTO::class },
332
     *     output= {
333
     *         "class"=Campaign::class,
334
     *         "groups"={"public"},
335
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
336
     *     },
337
     *     statusCodes={
338
     *         200="Returned when successful",
339
     *         400="Returned when a violation is raised by validation",
340
     *         401="Returned when X-CIR-TKN private token value is invalid",
341
     *         404="Returned when campaign not found",
342
     *         405="Returned when campaign refid is not set in URL"
343
     *     },
344
     *     tags={
345
     *         "token" = "#2c3e50"
346
     *     }
347
     * )
348
     */
349
    public function putCampaignAction(Project $project, Campaign $campaignDB, CampaignDTO $campaignDTO, Request $request)
350
    {
351
        if ($this->isInvalidToken($request, $project->getToken())) {
352
            return $this->getInvalidTokenView();
353
        }
354
355
        $validator = $this->get('validator');
356
        $violationsDTO = $validator->validate($campaignDTO);
357
358
        if (count($violationsDTO) > 0) {
359
            return $this->view($violationsDTO, Response::HTTP_BAD_REQUEST);
360
        }
361
        $campaignDB->setFromDTO($campaignDTO);
362
363
        $violations = $validator->validate($campaignDB);
364
        if (count($violations) > 0) {
365
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
366
        }
367
        $this->getDoctrine()->getManager()->flush();
368
369
        return $campaignDB;
370
    }
371
372
    /**
373
     * Delete a campaign. Example: </br>
374
     * <pre style="background:black; color:white; font-size:10px;"><code style="background:black;">curl https://www.ci-report.io/api/projects/project-one/campaigns/1 -H "X-CIR-TKN: 1f4ffb19e4b9-02278af07b7d-4e370a76f001" -X DELETE
375
     * </code></pre>.
376
     *
377
     * @param Campaign $campaign Campaign to delete
378
     * @param Request  $request  The request
379
     *
380
     * @return void|View
381
     *
382
     * @Rest\Delete(
383
     *    "/projects/{prefid}/campaigns/{crefid}",
384
     *    requirements={"crefid" = "\d+"}
385
     * )
386
     * @Rest\View(statusCode=Response::HTTP_NO_CONTENT)
387
     *
388
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
389
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
390
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
391
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
392
     *    "map_method_signature" = true
393
     * })
394
     *
395
     * @Doc\ApiDoc(
396
     *     section="Campaigns",
397
     *     description="Delete a campaign.",
398
     *     headers={
399
     *         {
400
     *             "name"="X-CIR-TKN",
401
     *             "required"=true,
402
     *             "description"="Private token"
403
     *         }
404
     *     },
405
     *     requirements={
406
     *         {
407
     *             "name"="prefid",
408
     *             "dataType"="string",
409
     *             "requirement"="string",
410
     *             "description"="Unique short name of project defined on project creation."
411
     *         },
412
     *         {
413
     *             "name"="crefid",
414
     *             "dataType"="int",
415
     *             "requirement"="int",
416
     *             "description"="Reference id of the campaign."
417
     *         }
418
     *     },
419
     *     statusCodes={
420
     *         204="Returned when successful",
421
     *         401="Returned when X-CIR-TKN private token value is invalid",
422
     *         404="Returned when campaign not found",
423
     *         405="Returned when campaign refid is not set in URL"
424
     *     },
425
     *     tags={
426
     *         "token" = "#2c3e50"
427
     *     }
428
     * )
429
     */
430
    public function deleteCampaignAction(Project $project, Campaign $campaign, Request $request)
431
    {
432
        if ($this->isInvalidToken($request, $project->getToken())) {
433
            return $this->getInvalidTokenView();
434
        }
435
        $em = $this->getDoctrine()->getManager();
436
        $em->remove($campaign);
437
        $em->flush();
438
    }
439
}
440