|
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 App\Controller; |
|
24
|
|
|
|
|
25
|
|
|
use App\DTO\CampaignDTO; |
|
26
|
|
|
use App\Entity\Campaign; |
|
27
|
|
|
use App\Entity\Project; |
|
28
|
|
|
use FOS\RestBundle\Controller\Annotations as Rest; |
|
29
|
|
|
use FOS\RestBundle\View\View; |
|
30
|
|
|
use Nelmio\ApiDocBundle\Annotation\Operation; |
|
31
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
|
32
|
|
|
use Swagger\Annotations as SWG; |
|
33
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity; |
|
34
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
35
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
36
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Campaign API controller class. |
|
40
|
|
|
* |
|
41
|
|
|
* @category ci-report app |
|
42
|
|
|
* |
|
43
|
|
|
* @author Francois-Xavier Soubirou <[email protected]> |
|
44
|
|
|
* @copyright 2017 Francois-Xavier Soubirou |
|
45
|
|
|
* @license http://www.gnu.org/licenses/ GPLv3 |
|
46
|
|
|
* |
|
47
|
|
|
* @see https://www.ci-report.io |
|
48
|
|
|
* |
|
49
|
|
|
* @Rest\Route("/api") |
|
50
|
|
|
*/ |
|
51
|
|
|
class CampaignApiController extends AbstractApiController |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Get list of campaigns for a project. |
|
55
|
|
|
* |
|
56
|
|
|
* @param Project $project Project |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
* |
|
60
|
|
|
* @Rest\Get("/projects/{prefid}/campaigns") |
|
61
|
|
|
* @Rest\View(serializerGroups={"public"}) |
|
62
|
|
|
* |
|
63
|
|
|
* @ParamConverter("project", options={"mapping": {"prefid": "refid"}}) |
|
64
|
|
|
* |
|
65
|
|
|
* @Operation( |
|
66
|
|
|
* tags={"Campaigns"}, |
|
67
|
|
|
* summary="Get the list of all campaigns for a project.", |
|
68
|
|
|
* description="Example: </br><pre><code>curl https://www.ci-report.io/api/projects/project-one/campaigns -X GET</code></pre>", |
|
69
|
|
|
* @SWG\Parameter( |
|
70
|
|
|
* name="prefid", |
|
71
|
|
|
* in="path", |
|
72
|
|
|
* description="Unique short name of project defined on project creation.", |
|
73
|
|
|
* type="string" |
|
74
|
|
|
* ), |
|
75
|
|
|
* @SWG\Response( |
|
76
|
|
|
* response="200", |
|
77
|
|
|
* description="Returned when successful array of public data of campaigns", |
|
78
|
|
|
* @SWG\Schema( |
|
79
|
|
|
* type="array", |
|
80
|
|
|
* @Model(type=Campaign::class, groups={"public"}) |
|
81
|
|
|
* ) |
|
82
|
|
|
* ), |
|
83
|
|
|
* @SWG\Response( |
|
84
|
|
|
* response="404", |
|
85
|
|
|
* description="Returned when project not found", |
|
86
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
87
|
|
|
* ) |
|
88
|
|
|
* ) |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getCampaignsAction(Project $project): array |
|
91
|
|
|
{ |
|
92
|
|
|
$campaigns = $this->getDoctrine() |
|
93
|
|
|
->getRepository(Campaign::class) |
|
94
|
|
|
->findCampaignsByProject($project); |
|
95
|
|
|
|
|
96
|
|
|
return $campaigns; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get campaign data. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Campaign $campaign Campaign |
|
103
|
|
|
* |
|
104
|
|
|
* @return Campaign |
|
105
|
|
|
* |
|
106
|
|
|
* @Rest\Get( |
|
107
|
|
|
* "/projects/{prefid}/campaigns/{crefid}", |
|
108
|
|
|
* requirements={"crefid" = "\d+"} |
|
109
|
|
|
* ) |
|
110
|
|
|
* @Rest\View(serializerGroups={"public"}) |
|
111
|
|
|
* |
|
112
|
|
|
* @Entity("campaign", expr="repository.findCampaignByProjectRefidAndRefid(prefid, crefid)") |
|
113
|
|
|
* |
|
114
|
|
|
* @Operation( |
|
115
|
|
|
* tags={"Campaigns"}, |
|
116
|
|
|
* summary="Get campaign data.", |
|
117
|
|
|
* description="Example: </br><pre><code>curl https://www.ci-report.io/api/projects/project-one/campaigns/1 -X GET</code></pre>", |
|
118
|
|
|
* @SWG\Parameter( |
|
119
|
|
|
* name="prefid", |
|
120
|
|
|
* in="path", |
|
121
|
|
|
* description="Unique short name of project defined on project creation.", |
|
122
|
|
|
* type="string" |
|
123
|
|
|
* ), |
|
124
|
|
|
* @SWG\Parameter( |
|
125
|
|
|
* name="crefid", |
|
126
|
|
|
* in="path", |
|
127
|
|
|
* description="Reference id of the campaign.", |
|
128
|
|
|
* type="integer" |
|
129
|
|
|
* ), |
|
130
|
|
|
* @SWG\Response( |
|
131
|
|
|
* response="200", |
|
132
|
|
|
* description="Returned when successful", |
|
133
|
|
|
* @Model(type=Campaign::class, groups={"public"}) |
|
134
|
|
|
* ), |
|
135
|
|
|
* @SWG\Response( |
|
136
|
|
|
* response="404", |
|
137
|
|
|
* description="Returned when project or campaign not found", |
|
138
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
139
|
|
|
* ) |
|
140
|
|
|
* ) |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getCampaignAction(Campaign $campaign): Campaign |
|
143
|
|
|
{ |
|
144
|
|
|
return $campaign; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get last added campaign data. |
|
149
|
|
|
* |
|
150
|
|
|
* @param Campaign $campaign Campaign |
|
151
|
|
|
* |
|
152
|
|
|
* @return Campaign |
|
153
|
|
|
* |
|
154
|
|
|
* @Rest\Get("/projects/{prefid}/campaigns/last") |
|
155
|
|
|
* @Rest\View(serializerGroups={"public"}) |
|
156
|
|
|
* |
|
157
|
|
|
* @Entity("campaign", expr="repository.findLastCampaignByProjectRefid(prefid)") |
|
158
|
|
|
* |
|
159
|
|
|
* @Operation( |
|
160
|
|
|
* tags={"Campaigns"}, |
|
161
|
|
|
* summary="Get last added campaign data.", |
|
162
|
|
|
* description="Example: </br><pre><code>curl https://www.ci-report.io/api/projects/project-one/campaigns/last -X GET</code></pre>", |
|
163
|
|
|
* @SWG\Parameter( |
|
164
|
|
|
* name="prefid", |
|
165
|
|
|
* in="path", |
|
166
|
|
|
* description="Unique short name of project defined on project creation.", |
|
167
|
|
|
* type="string" |
|
168
|
|
|
* ), |
|
169
|
|
|
* @SWG\Response( |
|
170
|
|
|
* response="200", |
|
171
|
|
|
* description="Returned when successful", |
|
172
|
|
|
* @Model(type=Campaign::class, groups={"public"}) |
|
173
|
|
|
* ), |
|
174
|
|
|
* @SWG\Response( |
|
175
|
|
|
* response="404", |
|
176
|
|
|
* description="Returned when no campaign exists for project", |
|
177
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
178
|
|
|
* ) |
|
179
|
|
|
* ) |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getLastCampaignAction(Campaign $campaign): Campaign |
|
182
|
|
|
{ |
|
183
|
|
|
return $campaign; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Create a campaign. |
|
188
|
|
|
* |
|
189
|
|
|
* @param Project $project Project |
|
190
|
|
|
* @param CampaignDTO $campaignDTO Campaign to create |
|
191
|
|
|
* @param Request $request The request |
|
192
|
|
|
* |
|
193
|
|
|
* @return Campaign|View |
|
194
|
|
|
* |
|
195
|
|
|
* @Rest\Post("/projects/{prefid}/campaigns") |
|
196
|
|
|
* @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"public"}) |
|
197
|
|
|
* |
|
198
|
|
|
* @ParamConverter("project", options={"mapping": {"prefid": "refid"}}) |
|
199
|
|
|
* @ParamConverter("campaignDTO", converter="fos_rest.request_body") |
|
200
|
|
|
* |
|
201
|
|
|
* @Operation( |
|
202
|
|
|
* tags={"Campaigns"}, |
|
203
|
|
|
* summary="Create a campaign.", |
|
204
|
|
|
* description="Example: </br><pre><code>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 '{"start":"2017-07-01 12:30:01", "end":"2017-07-03 12:30:01"}'</code></pre>", |
|
205
|
|
|
* @SWG\Parameter( |
|
206
|
|
|
* name="prefid", |
|
207
|
|
|
* in="path", |
|
208
|
|
|
* description="Unique short name of project defined on project creation.", |
|
209
|
|
|
* type="string" |
|
210
|
|
|
* ), |
|
211
|
|
|
* @SWG\Parameter( |
|
212
|
|
|
* name="data", |
|
213
|
|
|
* in="body", |
|
214
|
|
|
* required=false, |
|
215
|
|
|
* @SWG\Schema(ref="#/definitions/CampaignDataModel") |
|
216
|
|
|
* ), |
|
217
|
|
|
* @SWG\Response( |
|
218
|
|
|
* response="201", |
|
219
|
|
|
* description="Returned when created", |
|
220
|
|
|
* @Model(type=Campaign::class, groups={"public"}) |
|
221
|
|
|
* ), |
|
222
|
|
|
* @SWG\Response( |
|
223
|
|
|
* response="400", |
|
224
|
|
|
* description="Returned when a violation is raised by validation", |
|
225
|
|
|
* @SWG\Schema( |
|
226
|
|
|
* type="array", |
|
227
|
|
|
* @SWG\Items(ref="#/definitions/ErrorModel") |
|
228
|
|
|
* ) |
|
229
|
|
|
* ), |
|
230
|
|
|
* @SWG\Response( |
|
231
|
|
|
* response="401", |
|
232
|
|
|
* description="Returned when X-CIR-TKN private token value is invalid", |
|
233
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
234
|
|
|
* ), |
|
235
|
|
|
* @SWG\Response( |
|
236
|
|
|
* response="404", |
|
237
|
|
|
* description="Returned when project not found", |
|
238
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
239
|
|
|
* ) |
|
240
|
|
|
* ) |
|
241
|
|
|
*/ |
|
242
|
|
|
public function postCampaignAction(Project $project, CampaignDTO $campaignDTO, Request $request) |
|
243
|
|
|
{ |
|
244
|
|
|
if ($this->isInvalidToken($request, $project->getToken())) { |
|
245
|
|
|
return $this->getInvalidTokenView(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
$validator = $this->get('validator'); |
|
249
|
|
|
$violationsDTO = $validator->validate($campaignDTO); |
|
250
|
|
|
|
|
251
|
|
|
if (count($violationsDTO) > 0) { |
|
252
|
|
|
return $this->view($violationsDTO, Response::HTTP_BAD_REQUEST); |
|
253
|
|
|
} |
|
254
|
|
|
$campaign = new Campaign($project); |
|
255
|
|
|
$campaign->setFromDTO($campaignDTO); |
|
256
|
|
|
|
|
257
|
|
|
$violations = $validator->validate($campaign); |
|
258
|
|
|
if (count($violations) > 0) { |
|
259
|
|
|
return $this->view($violations, Response::HTTP_BAD_REQUEST); |
|
260
|
|
|
} |
|
261
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
262
|
|
|
$em->persist($campaign); |
|
263
|
|
|
$em->flush(); |
|
264
|
|
|
|
|
265
|
|
|
return $campaign; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Update a campaign. |
|
270
|
|
|
* |
|
271
|
|
|
* @param Project $project Project |
|
272
|
|
|
* @param Campaign $campaignDB Campaign to update |
|
273
|
|
|
* @param CampaignDTO $campaignDTO Object containing input data |
|
274
|
|
|
* @param Request $request The request |
|
275
|
|
|
* |
|
276
|
|
|
* @return Campaign|View |
|
277
|
|
|
* |
|
278
|
|
|
* @Rest\Put( |
|
279
|
|
|
* "/projects/{prefid}/campaigns/{crefid}", |
|
280
|
|
|
* requirements={"crefid" = "\d+"} |
|
281
|
|
|
* ) |
|
282
|
|
|
* @Rest\View(serializerGroups={"public"}) |
|
283
|
|
|
* |
|
284
|
|
|
* @ParamConverter("project", options={"mapping": {"prefid": "refid"}}) |
|
285
|
|
|
* @Entity("campaignDB", expr="repository.findCampaignByProjectRefidAndRefid(prefid, crefid)") |
|
286
|
|
|
* @ParamConverter("campaignDTO", converter="fos_rest.request_body") |
|
287
|
|
|
* |
|
288
|
|
|
* @Operation( |
|
289
|
|
|
* tags={"Campaigns"}, |
|
290
|
|
|
* summary="Update a campaign.", |
|
291
|
|
|
* description="Example: </br><pre><code>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"}'</code></pre>", |
|
292
|
|
|
* @SWG\Parameter( |
|
293
|
|
|
* name="prefid", |
|
294
|
|
|
* in="path", |
|
295
|
|
|
* description="Unique short name of project defined on project creation.", |
|
296
|
|
|
* type="string" |
|
297
|
|
|
* ), |
|
298
|
|
|
* @SWG\Parameter( |
|
299
|
|
|
* name="crefid", |
|
300
|
|
|
* in="path", |
|
301
|
|
|
* description="Reference id of the campaign.", |
|
302
|
|
|
* type="integer" |
|
303
|
|
|
* ), |
|
304
|
|
|
* @SWG\Parameter( |
|
305
|
|
|
* name="data", |
|
306
|
|
|
* in="body", |
|
307
|
|
|
* required=true, |
|
308
|
|
|
* @SWG\Schema(ref="#/definitions/CampaignDataModel") |
|
309
|
|
|
* ), |
|
310
|
|
|
* @SWG\Response( |
|
311
|
|
|
* response="200", |
|
312
|
|
|
* description="Returned when successful", |
|
313
|
|
|
* @Model(type=Campaign::class, groups={"public"}) |
|
314
|
|
|
* ), |
|
315
|
|
|
* @SWG\Response( |
|
316
|
|
|
* response="400", |
|
317
|
|
|
* description="Returned when a violation is raised by validation", |
|
318
|
|
|
* @SWG\Schema( |
|
319
|
|
|
* type="array", |
|
320
|
|
|
* @SWG\Items(ref="#/definitions/ErrorModel") |
|
321
|
|
|
* ) |
|
322
|
|
|
* ), |
|
323
|
|
|
* @SWG\Response( |
|
324
|
|
|
* response="401", |
|
325
|
|
|
* description="Returned when X-CIR-TKN private token value is invalid", |
|
326
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
327
|
|
|
* ), |
|
328
|
|
|
* @SWG\Response( |
|
329
|
|
|
* response="404", |
|
330
|
|
|
* description="Returned when campaign not found", |
|
331
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
332
|
|
|
* ) |
|
333
|
|
|
* ) |
|
334
|
|
|
*/ |
|
335
|
|
|
public function putCampaignAction(Project $project, Campaign $campaignDB, CampaignDTO $campaignDTO, Request $request) |
|
336
|
|
|
{ |
|
337
|
|
|
if ($this->isInvalidToken($request, $project->getToken())) { |
|
338
|
|
|
return $this->getInvalidTokenView(); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
$validator = $this->get('validator'); |
|
342
|
|
|
$violationsDTO = $validator->validate($campaignDTO); |
|
343
|
|
|
|
|
344
|
|
|
if (count($violationsDTO) > 0) { |
|
345
|
|
|
return $this->view($violationsDTO, Response::HTTP_BAD_REQUEST); |
|
346
|
|
|
} |
|
347
|
|
|
$campaignDB->setFromDTO($campaignDTO); |
|
348
|
|
|
|
|
349
|
|
|
$violations = $validator->validate($campaignDB); |
|
350
|
|
|
if (count($violations) > 0) { |
|
351
|
|
|
return $this->view($violations, Response::HTTP_BAD_REQUEST); |
|
352
|
|
|
} |
|
353
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
354
|
|
|
|
|
355
|
|
|
return $campaignDB; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Delete a campaign. |
|
360
|
|
|
* |
|
361
|
|
|
* @param Project $project Project |
|
362
|
|
|
* @param Campaign $campaign Campaign to delete |
|
363
|
|
|
* @param Request $request The request |
|
364
|
|
|
* |
|
365
|
|
|
* @return void|View |
|
366
|
|
|
* |
|
367
|
|
|
* @Rest\Delete( |
|
368
|
|
|
* "/projects/{prefid}/campaigns/{crefid}", |
|
369
|
|
|
* requirements={"crefid" = "\d+"} |
|
370
|
|
|
* ) |
|
371
|
|
|
* @Rest\View(statusCode=Response::HTTP_NO_CONTENT) |
|
372
|
|
|
* |
|
373
|
|
|
* @ParamConverter("project", options={"mapping": {"prefid": "refid"}}) |
|
374
|
|
|
* @Entity("campaign", expr="repository.findCampaignByProjectRefidAndRefid(prefid, crefid)") |
|
375
|
|
|
* |
|
376
|
|
|
* @Operation( |
|
377
|
|
|
* tags={"Campaigns"}, |
|
378
|
|
|
* summary="Delete a campaign.", |
|
379
|
|
|
* description="Example: </br><pre><code>curl https://www.ci-report.io/api/projects/project-one/campaigns/1 -H "X-CIR-TKN: 1f4ffb19e4b9-02278af07b7d-4e370a76f001" -X DELETE</code></pre>", |
|
380
|
|
|
* @SWG\Parameter( |
|
381
|
|
|
* name="prefid", |
|
382
|
|
|
* in="path", |
|
383
|
|
|
* description="Unique short name of project defined on project creation.", |
|
384
|
|
|
* type="string" |
|
385
|
|
|
* ), |
|
386
|
|
|
* @SWG\Parameter( |
|
387
|
|
|
* name="crefid", |
|
388
|
|
|
* in="path", |
|
389
|
|
|
* description="Reference id of the campaign.", |
|
390
|
|
|
* type="integer" |
|
391
|
|
|
* ), |
|
392
|
|
|
* @SWG\Response( |
|
393
|
|
|
* response="204", |
|
394
|
|
|
* description="Returned when successful" |
|
395
|
|
|
* ), |
|
396
|
|
|
* @SWG\Response( |
|
397
|
|
|
* response="401", |
|
398
|
|
|
* description="Returned when X-CIR-TKN private token value is invalid", |
|
399
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
400
|
|
|
* ), |
|
401
|
|
|
* @SWG\Response( |
|
402
|
|
|
* response="404", |
|
403
|
|
|
* description="Returned when campaign not found", |
|
404
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
405
|
|
|
* ), |
|
406
|
|
|
* @SWG\Response( |
|
407
|
|
|
* response="405", |
|
408
|
|
|
* description="Returned when campaign refid is not set in URL", |
|
409
|
|
|
* @SWG\Schema(ref="#/definitions/ErrorModel") |
|
410
|
|
|
* ) |
|
411
|
|
|
* ) |
|
412
|
|
|
*/ |
|
413
|
|
|
public function deleteCampaignAction(Project $project, Campaign $campaign, Request $request) |
|
414
|
|
|
{ |
|
415
|
|
|
if ($this->isInvalidToken($request, $project->getToken())) { |
|
416
|
|
|
return $this->getInvalidTokenView(); |
|
417
|
|
|
} |
|
418
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
419
|
|
|
$em->remove($campaign); |
|
420
|
|
|
$em->flush(); |
|
421
|
|
|
} |
|
422
|
|
|
} |
|
423
|
|
|
|