Completed
Push — master ( 546355...e7f15d )
by FX
06:52
created

CampaignApiController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 227
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCampaignsAction() 0 7 1
A getLastCampaignAction() 0 3 1
A getCampaignAction() 0 3 1
B postCampaignAction() 0 24 4
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 Nelmio\ApiDocBundle\Annotation as Doc;
30
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\Response;
33
34
/**
35
 * Campaign API controller class.
36
 *
37
 * @category  ci-report app
38
 *
39
 * @author    Francois-Xavier Soubirou <[email protected]>
40
 * @copyright 2017 Francois-Xavier Soubirou
41
 * @license   http://www.gnu.org/licenses/   GPLv3
42
 *
43
 * @see      https://www.ci-report.io
44
 *
45
 * @Rest\Route("/api")
46
 */
47
class CampaignApiController extends AbstractApiController
48
{
49
    /**
50
     * Get list of campaigns for a project. Example: </br>
51
     * <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
52
     * </code></pre>.
53
     *
54
     * @param Project $project Project
55
     *
56
     * @return array
57
     *
58
     * @Rest\Get("/projects/{prefid}/campaigns")
59
     * @Rest\View(serializerGroups={"public"})
60
     *
61
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
62
     *
63
     * @Doc\ApiDoc(
64
     *     section="Campaigns",
65
     *     description="Get the list of all campaigns for a project.",
66
     *     requirements={
67
     *         {
68
     *             "name"="prefid",
69
     *             "dataType"="string",
70
     *             "requirement"="string",
71
     *             "description"="Unique short name of project defined on project creation."
72
     *         }
73
     *     },
74
     *     output={
75
     *         "class"=Campaign::class,
76
     *         "groups"={"public"},
77
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
78
     *     },
79
     *     statusCodes={
80
     *         200="Returned when successful array of public data of campaigns",
81
     *         404="Returned when project not found"
82
     *     }
83
     * )
84
     */
85
    public function getCampaignsAction(Project $project): array
86
    {
87
        $campaigns = $this->getDoctrine()
88
            ->getRepository(Campaign::class)
89
            ->findCampaignsByProject($project);
90
91
        return $campaigns;
92
    }
93
94
    /**
95
     * Get campaign data. Example: </br>
96
     * <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
97
     * </code></pre>.
98
     *
99
     * @param Campaign $campaign Campaign
100
     *
101
     * @return Campaign
102
     *
103
     * @Rest\Get(
104
     *    "/projects/{prefid}/campaigns/{crefid}",
105
     *    requirements={"crefid" = "\d+"}
106
     * )
107
     * @Rest\View(serializerGroups={"public"})
108
     *
109
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
110
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
111
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
112
     *    "map_method_signature" = true
113
     * })
114
     *
115
     * @Doc\ApiDoc(
116
     *     section="Campaigns",
117
     *     description="Get campaign data.",
118
     *     requirements={
119
     *         {
120
     *             "name"="prefid",
121
     *             "dataType"="string",
122
     *             "requirement"="string",
123
     *             "description"="Unique short name of project defined on project creation."
124
     *         },
125
     *         {
126
     *             "name"="crefid",
127
     *             "dataType"="int",
128
     *             "requirement"="int",
129
     *             "description"="Reference id of the campaign."
130
     *         }
131
     *     },
132
     *     output= {
133
     *         "class"=Campaign::class,
134
     *         "groups"={"public"},
135
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
136
     *     },
137
     *     statusCodes={
138
     *         200="Returned when successful",
139
     *         404="Returned when project or campaign not found"
140
     *     }
141
     * )
142
     */
143
    public function getCampaignAction(Campaign $campaign): Campaign
144
    {
145
        return $campaign;
146
    }
147
148
    /**
149
     * Get last added campaign data. Example: </br>
150
     * <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
151
     * </code></pre>.
152
     *
153
     * @param Campaign $campaign Campaign
154
     *
155
     * @return Campaign
156
     *
157
     * @Rest\Get("/projects/{prefid}/campaigns/last")
158
     * @Rest\View(serializerGroups={"public"})
159
     *
160
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
161
     *    "repository_method" = "findLastCampaignByProjectRefid",
162
     *    "mapping": {"prefid": "prefid"},
163
     *    "map_method_signature" = true
164
     * })
165
     *
166
     * @Doc\ApiDoc(
167
     *     section="Campaigns",
168
     *     description="Get last added campaign data.",
169
     *     requirements={
170
     *         {
171
     *             "name"="prefid",
172
     *             "dataType"="string",
173
     *             "requirement"="string",
174
     *             "description"="Unique short name of project defined on project creation."
175
     *         }
176
     *     },
177
     *     output= {
178
     *         "class"=Campaign::class,
179
     *         "groups"={"public"},
180
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
181
     *     },
182
     *     statusCodes={
183
     *         200="Returned when successful",
184
     *         404="Returned when no campaign exists for project"
185
     *     }
186
     * )
187
     */
188
    public function getLastCampaignAction(Campaign $campaign): Campaign
189
    {
190
        return $campaign;
191
    }
192
193
    /**
194
     * Create a campaign. Example: </br>
195
     * <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"}'
196
     * </code></pre>.
197
     *
198
     * @param Project     $project     Project
199
     * @param CampaignDTO $campaignDTO Project to create
200
     * @param Request     $request     The request
201
     *
202
     * @return Campaign|View
0 ignored issues
show
Bug introduced by
The type AppBundle\Controller\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
203
     *
204
     * @Rest\Post("/projects/{prefid}/campaigns")
205
     * @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"public"})
206
     *
207
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
208
     * @ParamConverter("campaignDTO", converter="fos_rest.request_body")
209
     *
210
     * @Doc\ApiDoc(
211
     *     section="Campaigns",
212
     *     description="Create a campaign.",
213
     *     headers={
214
     *         {
215
     *             "name"="Content-Type",
216
     *             "required"=true,
217
     *             "description"="Type of content: application/json"
218
     *         },
219
     *         {
220
     *             "name"="X-CIR-TKN",
221
     *             "required"=true,
222
     *             "description"="Private token"
223
     *         }
224
     *     },
225
     *     requirements={
226
     *         {
227
     *             "name"="prefid",
228
     *             "dataType"="string",
229
     *             "requirement"="string",
230
     *             "description"="Unique short name of project defined on project creation."
231
     *         }
232
     *     },
233
     *     input= { "class"=CampaignDTO::class },
234
     *     output= {
235
     *         "class"=Campaign::class,
236
     *         "groups"={"public"},
237
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
238
     *     },
239
     *     statusCodes={
240
     *         201="Returned when created",
241
     *         400="Returned when a violation is raised by validation",
242
     *         401="Returned when X-CIR-TKN private token value is invalid",
243
     *         404="Returned when project not found"
244
     *     },
245
     *     tags={
246
     *         "token" = "#2c3e50"
247
     *     }
248
     * )
249
     */
250
    public function postCampaignAction(Project $project, CampaignDTO $campaignDTO, Request $request)
251
    {
252
        if ($this->isInvalidToken($request, $project->getToken())) {
253
            return $this->getInvalidTokenView();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getInvalidTokenView() returns the type FOS\RestBundle\View\View which is incompatible with the documented return type AppBundle\Controller\Vie...pBundle\Entity\Campaign.
Loading history...
254
        }
255
256
        $validator = $this->get('validator');
257
        $violationsDTO = $validator->validate($campaignDTO);
258
259
        if (count($violationsDTO) > 0) {
260
            return $this->view($violationsDTO, Response::HTTP_BAD_REQUEST);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->view($viol...onse::HTTP_BAD_REQUEST) returns the type FOS\RestBundle\View\View which is incompatible with the documented return type AppBundle\Controller\Vie...pBundle\Entity\Campaign.
Loading history...
261
        }
262
        $campaign = new Campaign($project);
263
        $campaign->setFromDTO($campaignDTO);
264
265
        $violations = $validator->validate($campaign);
266
        if (count($violations) > 0) {
267
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->view($viol...onse::HTTP_BAD_REQUEST) returns the type FOS\RestBundle\View\View which is incompatible with the documented return type AppBundle\Controller\Vie...pBundle\Entity\Campaign.
Loading history...
268
        }
269
        $em = $this->getDoctrine()->getManager();
270
        $em->persist($campaign);
271
        $em->flush();
272
273
        return $campaign;
274
    }
275
}
276