Completed
Push — master ( ae55ff...05955d )
by FX
02:19
created

ProjectApiController::getProjectsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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\Entity\Project;
26
use AppBundle\Service\ProjectService;
27
use FOS\RestBundle\Controller\Annotations as Rest;
28
use FOS\RestBundle\Controller\FOSRestController;
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\Response;
33
use Symfony\Component\Validator\ConstraintViolationList;
34
35
/**
36
 * Project 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://ci-report.io
45
 *
46
 * @Rest\Route("/api")
47
 */
48
class ProjectApiController extends FOSRestController
49
{
50
    /**
51
     * Get list of projects.
52
     *
53
     * @return array
54
     *
55
     * @Rest\Get("/projects")
56
     * @Rest\View(serializerGroups={"public"})
57
     *
58
     * @Doc\ApiDoc(
59
     *     section="Projects",
60
     *     description="Get the list of all projects.",
61
     *     output={
62
     *         "class"=Project::class,
63
     *         "groups"={"public"},
64
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
65
     *     },
66
     *     statusCodes={
67
     *         200="Returned when successful array of public data of projects"
68
     *     }
69
     * )
70
     */
71
    public function getProjectsAction(): array
72
    {
73
        $projects = $this->getDoctrine()
74
            ->getRepository(Project::class)
75
            ->findAll();
76
77
        return $projects;
78
    }
79
80
    /**
81
     * Get public project data.
82
     *
83
     * @param Project $project Project to create
84
     *
85
     * @return Project
86
     *
87
     * @Rest\Get("/projects/{ref_id}")
88
     * @Rest\View(serializerGroups={"public"})
89
     *
90
     * @ParamConverter("project", options={"mapping": {"ref_id": "refId"}})
91
     *
92
     * @Doc\ApiDoc(
93
     *     section="Projects",
94
     *     description="Get public project data.",
95
     *     requirements={
96
     *         {
97
     *             "name"="ref_id",
98
     *             "dataType"="string",
99
     *             "requirement"="string",
100
     *             "description"="Unique short name of project defined on project creation."
101
     *         }
102
     *     },
103
     *     output= {
104
     *         "class"=Project::class,
105
     *         "groups"={"public"},
106
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
107
     *     },
108
     *     statusCodes={
109
     *         200="Returned when successful",
110
     *         404="Returned when project not found"
111
     *     }
112
     * )
113
     */
114
    public function getProjectAction(Project $project): Project
115
    {
116
        return $project;
117
    }
118
119
    /**
120
     * Create a project. Private data are sent by email.
121
     *
122
     * @param Project                 $project    Project to create
123
     * @param ConstraintViolationList $violations List of violations
124
     *
125
     * @return Project|View
126
     *
127
     * @Rest\Post("/projects")
128
     * @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"public"})
129
     *
130
     * @ParamConverter("project", converter="fos_rest.request_body")
131
     *
132
     * @Doc\ApiDoc(
133
     *     section="Projects",
134
     *     description="Create a project. Private data are sent by mail.",
135
     *     input= { "class"=Project::class, "groups"={"create"} },
136
     *     output= {
137
     *         "class"=Project::class,
138
     *         "groups"={"public"},
139
     *         "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
140
     *     },
141
     *     statusCodes={
142
     *         201="Returned when created",
143
     *         400="Returned when a violation is raised by validation"
144
     *     }
145
     * )
146
     */
147
    public function postProjectsAction(Project $project, ConstraintViolationList $violations)
148
    {
149
        if (count($violations)) {
150
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
151
        }
152
        $projectService = $this->get(ProjectService::class);
153
        $projectService->setSlugAndToken($project);
154
155
        if (null === $project->getWarningLimit()) {
156
            $project->setWarningLimit(Project::DEFAULT_WARNING_LIMIT);
157
        }
158
        if (null === $project->getSuccessLimit()) {
159
            $project->setSuccessLimit(Project::DEFAULT_SUCCESS_LIMIT);
160
        }
161
        $em = $this->getDoctrine()->getManager();
162
        $em->persist($project);
163
        $em->flush();
164
165
        $projectService->sendRegistrationEmail($project);
166
167
        return $project;
168
    }
169
}
170