QuestController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 37.8 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 48
loc 127
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 9 1
A newAction() 0 20 3
A showAction() 10 10 1
A editAction() 18 18 3
A deleteAction() 12 12 3
A createDeleteForm() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Badger\Bundle\GameBundle\Controller;
4
5
use Badger\Bundle\GameBundle\Form\QuestType;
6
use Badger\Component\Game\Model\QuestInterface;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\Form\Form;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * Quest controller for admin CRUD.
15
 *
16
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
17
 */
18
class QuestController extends Controller
19
{
20
    /**
21
     * Lists all Quest entities.
22
     *
23
     * @return Response
24
     */
25
    public function indexAction()
26
    {
27
        $quests = $this->get('badger.game.repository.quest')
28
            ->getQuestsOrdered('endDate');
29
30
        return $this->render('@Game/quests/index.html.twig', [
31
            'quests' => $quests,
32
        ]);
33
    }
34
35
    /**
36
     * Creates a new Quest entity.
37
     *
38
     * @param Request $request
39
     *
40
     * @return RedirectResponse|Response
41
     */
42
    public function newAction(Request $request)
43
    {
44
        $questFactory = $this->get('badger.game.quest.factory');
45
        $quest = $questFactory->create();
46
47
        $form = $this->createForm(QuestType::class, $quest);
48
        $form->handleRequest($request);
49
50
        if ($form->isSubmitted() && $form->isValid()) {
51
            $questSaver = $this->get('badger.game.saver.quest');
52
            $questSaver->save($quest);
53
54
            return $this->redirectToRoute('admin_quest_show', ['id' => $quest->getId()]);
55
        }
56
57
        return $this->render('@Game/quests/new.html.twig', [
58
            'quest' => $quest,
59
            'form'  => $form->createView()
60
        ]);
61
    }
62
63
    /**
64
     * Finds and displays a Quest entity.
65
     *
66
     * @param string $id
67
     *
68
     * @return Response
69
     */
70 View Code Duplication
    public function showAction($id)
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...
71
    {
72
        $quest = $this->get('badger.game.repository.quest')->find($id);
73
        $deleteForm = $this->createDeleteForm($quest);
74
75
        return $this->render('@Game/quests/show.html.twig', [
76
            'quest'       => $quest,
77
            'delete_form' => $deleteForm->createView(),
78
        ]);
79
    }
80
81
    /**
82
     * Displays a form to edit an existing Quest entity.
83
     *
84
     * @param Request $request
85
     * @param int     $id
86
     *
87
     * @return RedirectResponse|Response
88
     */
89 View Code Duplication
    public function editAction(Request $request, $id)
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...
90
    {
91
        $quest = $this->get('badger.game.repository.quest')->find($id);
92
        $editForm = $this->createForm(QuestType::class, $quest);
93
        $editForm->handleRequest($request);
94
95
        if ($editForm->isSubmitted() && $editForm->isValid()) {
96
            $questSaver = $this->get('badger.game.saver.quest');
97
            $questSaver->save($quest);
98
99
            return $this->redirectToRoute('admin_quest_edit', ['id' => $quest->getId()]);
100
        }
101
102
        return $this->render('@Game/quests/edit.html.twig', [
103
            'quest'       => $quest,
104
            'edit_form'   => $editForm->createView(),
105
        ]);
106
    }
107
108
    /**
109
     * Deletes a Quest entity.
110
     *
111
     * @param Request        $request
112
     * @param QuestInterface $quest
113
     *
114
     * @return RedirectResponse
115
     */
116 View Code Duplication
    public function deleteAction(Request $request, QuestInterface $quest)
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...
117
    {
118
        $form = $this->createDeleteForm($quest);
119
        $form->handleRequest($request);
120
121
        if ($form->isSubmitted() && $form->isValid()) {
122
            $questRemover = $this->get('badger.game.remover.quest');
123
            $questRemover->remove($quest);
124
        }
125
126
        return $this->redirectToRoute('admin_quest_index');
127
    }
128
129
    /**
130
     * Creates a form to delete a Quest entity.
131
     *
132
     * @param QuestInterface $quest The Quest entity
133
     *
134
     * @return Form The form
135
     */
136 View Code Duplication
    private function createDeleteForm(QuestInterface $quest)
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...
137
    {
138
        return $this->createFormBuilder()
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Symfony\Component\Form\FormConfigBuilder. Did you maybe mean getFormConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
139
            ->setAction($this->generateUrl('admin_quest_delete', ['id' => $quest->getId()]))
140
            ->setMethod('DELETE')
141
            ->getForm()
142
        ;
143
    }
144
}
145