Passed
Push — master ( 31593e...785a7b )
by FX
03:35
created

SuiteApiController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 210
Duplicated Lines 11.43 %

Importance

Changes 0
Metric Value
dl 24
loc 210
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
B postSuiteAction() 24 24 4
A getSuiteAction() 0 3 1
A getSuitesAction() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\SuiteDTO;
26
use AppBundle\Entity\Campaign;
27
use AppBundle\Entity\Project;
28
use AppBundle\Entity\Suite;
29
use FOS\RestBundle\Controller\Annotations as Rest;
30
use FOS\RestBundle\View\View;
31
use Nelmio\ApiDocBundle\Annotation as Doc;
32
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpFoundation\Response;
35
36
/**
37
 * Suite API controller class.
38
 *
39
 * @category  ci-report app
40
 *
41
 * @author    Francois-Xavier Soubirou <[email protected]>
42
 * @copyright 2017 Francois-Xavier Soubirou
43
 * @license   http://www.gnu.org/licenses/   GPLv3
44
 *
45
 * @see      https://www.ci-report.io
46
 *
47
 * @Rest\Route("/api")
48
 */
49
class SuiteApiController extends AbstractApiController
50
{
51
    /**
52
     * Get list of suites for a project and a campaign. Example: </br>
53
     * <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/suites -X GET
54
     * </code></pre>.
55
     *
56
     * @param Campaign $campaign Campaign
57
     *
58
     * @return array
59
     *
60
     * @Rest\Get("/projects/{prefid}/campaigns/{crefid}/suites")
61
     * @Rest\View(serializerGroups={"public"})
62
     *
63
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
64
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
65
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
66
     *    "map_method_signature" = true
67
     * })
68
     *
69
     * @Doc\ApiDoc(
70
     *     section="Suites",
71
     *     description="Get the list of all suites for a campaign.",
72
     *     requirements={
73
     *         {
74
     *             "name"="prefid",
75
     *             "dataType"="string",
76
     *             "requirement"="string",
77
     *             "description"="Unique short name of project defined on project creation."
78
     *         },
79
     *         {
80
     *             "name"="crefid",
81
     *             "dataType"="int",
82
     *             "requirement"="int",
83
     *             "description"="Reference id of the campaign."
84
     *         }
85
     *     },
86
     *     output={
87
     *         "class"=Suite::class,
88
     *         "groups"={"public"},
89
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
90
     *     },
91
     *     statusCodes={
92
     *         200="Returned when successful array of public data of campaigns",
93
     *         404="Returned when project not found"
94
     *     }
95
     * )
96
     */
97
    public function getSuitesAction(Campaign $campaign): array
98
    {
99
        $suites = $this->getDoctrine()
100
            ->getRepository(Suite::class)
101
            ->findSuitesByCampaign($campaign);
102
103
        return $suites;
104
    }
105
106
    /**
107
     * Get suite data. Example: </br>
108
     * <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/suites/1 -X GET
109
     * </code></pre>.
110
     *
111
     * @param Suite $suite Suite
112
     *
113
     * @return Suite
114
     *
115
     * @Rest\Get(
116
     *    "/projects/{prefid}/campaigns/{crefid}/suites/{srefid}",
117
     *    requirements={"crefid" = "\d+", "srefid" = "\d+"}
118
     * )
119
     * @Rest\View(serializerGroups={"public"})
120
     *
121
     * @ParamConverter("suite", class="AppBundle:Suite", options={
122
     *    "repository_method" = "findSuiteByProjectRefidCampaignRefidAndRefid",
123
     *    "mapping": {"prefid": "prefid", "crefid": "crefid", "srefid": "srefid"},
124
     *    "map_method_signature" = true
125
     * })
126
     *
127
     * @Doc\ApiDoc(
128
     *     section="Suites",
129
     *     description="Get suite data.",
130
     *     requirements={
131
     *         {
132
     *             "name"="prefid",
133
     *             "dataType"="string",
134
     *             "requirement"="string",
135
     *             "description"="Unique short name of project defined on project creation."
136
     *         },
137
     *         {
138
     *             "name"="crefid",
139
     *             "dataType"="int",
140
     *             "requirement"="int",
141
     *             "description"="Reference id of the campaign."
142
     *         },
143
     *         {
144
     *             "name"="srefid",
145
     *             "dataType"="int",
146
     *             "requirement"="int",
147
     *             "description"="Reference id of the suite."
148
     *         }
149
     *     },
150
     *     output= {
151
     *         "class"=Suite::class,
152
     *         "groups"={"public"},
153
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
154
     *     },
155
     *     statusCodes={
156
     *         200="Returned when successful",
157
     *         404="Returned when project or campaign not found"
158
     *     }
159
     * )
160
     */
161
    public function getSuiteAction(Suite $suite): Suite
162
    {
163
        return $suite;
164
    }
165
166
    /**
167
     * Create a suite. Example: </br>
168
     * <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"}'
169
     * </code></pre>.
170
     *
171
     * @param Project  $project  Project
172
     * @param Campaign $campaign Campaign
173
     * @param SuiteDTO $suiteDTO Suite to create
174
     * @param Request  $request  The request
175
     *
176
     * @return Suite|View
177
     *
178
     * @Rest\Post("/projects/{prefid}/campaigns/{crefid}/suites")
179
     * @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"public"})
180
     *
181
     * @ParamConverter("project", options={"mapping": {"prefid": "refid"}})
182
     * @ParamConverter("campaign", class="AppBundle:Campaign", options={
183
     *    "repository_method" = "findCampaignByProjectRefidAndRefid",
184
     *    "mapping": {"prefid": "prefid", "crefid": "crefid"},
185
     *    "map_method_signature" = true
186
     * })
187
     * @ParamConverter("suiteDTO", converter="fos_rest.request_body")
188
     *
189
     * @Doc\ApiDoc(
190
     *     section="Suites",
191
     *     description="Create a suite.",
192
     *     headers={
193
     *         {
194
     *             "name"="Content-Type",
195
     *             "required"=true,
196
     *             "description"="Type of content: application/json"
197
     *         },
198
     *         {
199
     *             "name"="X-CIR-TKN",
200
     *             "required"=true,
201
     *             "description"="Private token"
202
     *         }
203
     *     },
204
     *     requirements={
205
     *         {
206
     *             "name"="prefid",
207
     *             "dataType"="string",
208
     *             "requirement"="string",
209
     *             "description"="Unique short name of project defined on project creation."
210
     *         },
211
     *         {
212
     *             "name"="crefid",
213
     *             "dataType"="int",
214
     *             "requirement"="int",
215
     *             "description"="Reference id of the campaign."
216
     *         }
217
     *     },
218
     *     input= { "class"=SuiteDTO::class },
219
     *     output= {
220
     *         "class"=Suite::class,
221
     *         "groups"={"public"},
222
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
223
     *     },
224
     *     statusCodes={
225
     *         201="Returned when created",
226
     *         400="Returned when a violation is raised by validation",
227
     *         401="Returned when X-CIR-TKN private token value is invalid",
228
     *         404="Returned when project not found"
229
     *     },
230
     *     tags={
231
     *         "token" = "#2c3e50"
232
     *     }
233
     * )
234
     */
235 View Code Duplication
    public function postSuiteAction(Project $project, Campaign $campaign, SuiteDTO $suiteDTO, Request $request)
236
    {
237
        if ($this->isInvalidToken($request, $project->getToken())) {
238
            return $this->getInvalidTokenView();
239
        }
240
241
        $validator = $this->get('validator');
242
        $violationsDTO = $validator->validate($suiteDTO);
243
244
        if (count($violationsDTO) > 0) {
245
            return $this->view($violationsDTO, Response::HTTP_BAD_REQUEST);
246
        }
247
        $suite = new Suite($campaign);
248
        $suite->setFromDTO($suiteDTO);
0 ignored issues
show
Bug introduced by
The method setFromDTO() does not exist on AppBundle\Entity\Suite. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
        $suite->/** @scrutinizer ignore-call */ 
249
                setFromDTO($suiteDTO);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
249
250
        $violations = $validator->validate($suite);
251
        if (count($violations) > 0) {
252
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
253
        }
254
        $em = $this->getDoctrine()->getManager();
255
        $em->persist($suite);
256
        $em->flush();
257
258
        return $suite;
259
    }
260
}
261