ChoiceController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright 2014-2018 Arnaud Bienvenu
4
 *
5
 * This file is part of Kyela.
6
7
 * Kyela is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
12
 * Kyela is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with Kyela.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Abienvenu\KyelaBundle\Controller;
23
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\JsonResponse;
26
use Symfony\Component\Routing\Annotation\Route;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
28
use Abienvenu\KyelaBundle\Entity\Choice;
29
use Abienvenu\KyelaBundle\Form\Type\ChoiceType;
30
31
/**
32
 * Choice controller.
33
 *
34
 * @Route("/{pollUrl}/choice")
35
 */
36
class ChoiceController extends CRUDController
37
{
38
    protected $entityName = 'KyelaBundle:Choice';
39
    protected $cancelRoute = 'choice';
40
    protected $successRoute = 'choice';
41
    protected $deleteRoute = 'choice_delete';
42
    protected $deleteSuccessRoute = 'choice';
43
44
    /**
45
     * Lists all Choice entities.
46
     *
47
     * @Route("/", name="choice", methods="GET")
48
     * @Template()
49
     */
50
    public function indexAction()
51
    {
52
        $em = $this->getDoctrine()->getManager();
53
        $choices = $em->getRepository("KyelaBundle:Choice")->getOrderedChoices($this->poll);
54
        return ['poll' => $this->poll, 'choices' => $choices];
55
    }
56
57
    /**
58
     * Displays a form to create a new Choice entity.
59
     *
60
     * @Route("/new", name="choice_new", methods={"GET", "POST"})
61
     * @Template()
62
     */
63
    public function newAction(Request $request)
64
    {
65
        $choice = new Choice();
66
        // By default, this new choice will be added at the end
67
        $choice->setPriority(count($this->poll->getChoices()));
68
        return $this->doNewAction(ChoiceType::class, $choice, $request);
69
    }
70
71
    /**
72
     * Displays a form to edit an existing Choice entity.
73
     *
74
     * @Route("/{id}/edit", name="choice_edit", methods={"GET", "PUT"})
75
     * @Template()
76
     */
77
    public function editAction(Request $request, $id)
78
    {
79
        return $this->doEditAction(ChoiceType::class, $id, $request);
80
    }
81
82
    /**
83
     * Deletes a Choice entity.
84
     *
85
     * @Route("/{id}", name="choice_delete", methods="DELETE")
86
     */
87
    public function deleteAction(Request $request, $id)
88
    {
89
        return $this->doDeleteAction($request, $id);
90
    }
91
92
    /**
93
     * Reorder choices
94
     *
95
     * @Route("/order", name="choice_order", methods="POST")
96
     */
97 View Code Duplication
    public function orderAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $em = $this->getDoctrine()->getManager();
100
        $repository = $em->getRepository("KyelaBundle:Choice");
101
        $order = $request->request->get('choice');
102
        foreach ($order as $priority => $choiceId)
103
        {
104
            /** @var Choice $choice */
105
            $choice = $repository->find($choiceId);
106
            $choice->setPriority($priority);
107
        }
108
        $em->flush();
109
110
        $response = new JsonResponse();
111
        $response->setData(["code" => 100, "success" => true]);
112
        return $response;
113
    }
114
}
115