Passed
Push — master ( 4c897b...5f1a0a )
by FX
02:15
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 FOS\RestBundle\Controller\Annotations as Rest;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\HttpFoundation\Response;
30
31
/**
32
 * Project controller class.
33
 *
34
 * @category  ci-report app
35
 *
36
 * @author    Francois-Xavier Soubirou <[email protected]>
37
 * @copyright 2017 Francois-Xavier Soubirou
38
 * @license   http://www.gnu.org/licenses/   GPLv3
39
 *
40
 * @see      https://ci-report.io
41
 *
42
 * @Rest\Route("/api")
43
 */
44
class ProjectApiController extends Controller
45
{
46
    /**
47
     * Get list of projects.
48
     *
49
     * @return array
50
     *
51
     * @Rest\Get("/projects")
52
     * @Rest\View()
53
     */
54
    public function getProjectsAction(): array
55
    {
56
        $projects = $this->getDoctrine()
57
            ->getRepository(Project::class)
58
            ->findAll();
59
60
        return $projects;
61
    }
62
63
    /**
64
     * Create a project.
65
     *
66
     * @param Project $project Project to create
67
     *
68
     * @return Project
69
     *
70
     * @Rest\Post("/projects")
71
     * @ParamConverter("project", converter="fos_rest.request_body")
72
     * @Rest\View(statusCode=Response::HTTP_CREATED)
73
     */
74
    public function postProjectsAction(Project $project): Project
75
    {
76
        $em = $this->getDoctrine()->getManager();
77
78
        $em->persist($project);
79
        $em->flush();
80
81
        return $project;
82
    }
83
}
84