Passed
Push — master ( a600df...e0fe4b )
by FX
02:35
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\Validator\ConstraintViolationList;
33
34
/**
35
 * Project 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://ci-report.io
44
 *
45
 * @Rest\Route("/api")
46
 */
47
class ProjectApiController extends FOSRestController
48
{
49
    /**
50
     * Get list of projects.
51
     *
52
     * @return array
53
     *
54
     * @Rest\Get("/projects")
55
     * @Rest\View()
56
     */
57
    public function getProjectsAction(): array
58
    {
59
        $projects = $this->getDoctrine()
60
            ->getRepository(Project::class)
61
            ->findAll();
62
63
        return $projects;
64
    }
65
66
    /**
67
     * Create a project.
68
     *
69
     * @param Project                 $project    Project to create
70
     * @param ConstraintViolationList $violations List of violations
71
     *
72
     * @return Project|View
73
     *
74
     * @Rest\Post("/projects")
75
     * @ParamConverter("project", converter="fos_rest.request_body")
76
     * @Rest\View(statusCode=Response::HTTP_CREATED)
77
     */
78
    public function postProjectsAction(Project $project, ConstraintViolationList $violations)
79
    {
80
        if (count($violations)) {
81
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
82
        }
83
        $this->get(ProjectService::class)->setSlugAndToken($project);
84
85
        if (null === $project->getWarningLimit()) {
86
            $project->setWarningLimit(Project::DEFAULT_WARNING_LIMIT);
87
        }
88
        if (null === $project->getSuccessLimit()) {
89
            $project->setSuccessLimit(Project::DEFAULT_SUCCESS_LIMIT);
90
        }
91
        $em = $this->getDoctrine()->getManager();
92
        $em->persist($project);
93
        $em->flush();
94
95
        return $project;
96
    }
97
}
98