Passed
Push — master ( bf0ee3...0945b6 )
by FX
02:34
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\UtilService;
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\Bundle\FrameworkBundle\Controller\Controller;
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()
57
     */
58
    public function getProjectsAction(): array
59
    {
60
        $projects = $this->getDoctrine()
61
            ->getRepository(Project::class)
62
            ->findAll();
63
64
        return $projects;
65
    }
66
67
    /**
68
     * Create a project.
69
     *
70
     * @param Project                 $project    Project to create
71
     * @param ConstraintViolationList $violations List of violations
72
     *
73
     * @return Project|View
74
     *
75
     * @Rest\Post("/projects")
76
     * @ParamConverter("project", converter="fos_rest.request_body")
77
     * @Rest\View(statusCode=Response::HTTP_CREATED)
78
     */
79
    public function postProjectsAction(Project $project, ConstraintViolationList $violations)
80
    {
81
        if (count($violations)) {
82
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
83
        }
84
85
        $em = $this->getDoctrine()->getManager();
86
        $repository = $this->getDoctrine()->getRepository(Project::class);
87
88
        $utilService = new UtilService();
89
        $slug = $utilService->toAscii($project->getName());
90
91
        $root = $slug;
92
        $separator = '-';
93
        $ext = 0;
94
95
        while ($repository->refIdExists($slug)) {
96
            ++$ext;
97
            $slug = $root.$separator.$ext;
98
        }
99
        $project->setRefId($slug);
100
101
        if (null === $project->getWarningLimit()) {
102
            $project->setWarningLimit(Project::DEFAULT_WARNING_LIMIT);
103
        }
104
        if (null === $project->getSuccessLimit()) {
105
            $project->setSuccessLimit(Project::DEFAULT_SUCCESS_LIMIT);
106
        }
107
108
        $em->persist($project);
109
        $em->flush();
110
111
        return $project;
112
    }
113
}
114