AdventureController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 19.19 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 19
loc 99
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 9 1
A formAction() 0 20 4
A showAction() 0 9 1
A deleteAction() 12 12 3
A createDeleteForm() 7 7 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\Entity\Adventure;
6
use Badger\Bundle\GameBundle\Form\AdventureType;
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
 * Adventure controller for admin CRUD.
15
 */
16
class AdventureController extends Controller
17
{
18
    /**
19
     * Lists all Adventure entities.
20
     *
21
     * @return Response
22
     */
23
    public function indexAction()
24
    {
25
        $adventures = $this->get('badger.game.repository.adventure')
26
            ->findAll();
27
28
        return $this->render('@Game/adventures/index.html.twig', [
29
            'adventures' => $adventures,
30
        ]);
31
    }
32
33
    /**
34
     * Creates or edits an Adventure entity.
35
     *
36
     * @param Request   $request
37
     * @param Adventure $adventure
38
     *
39
     * @return RedirectResponse|Response
40
     */
41
    public function formAction(Request $request, Adventure $adventure = null)
42
    {
43
        if (null === $adventure) {
44
            $adventure = $this->get('badger.game.adventure.factory')->create();
45
        }
46
47
        $form = $this->createForm(AdventureType::class, $adventure);
48
        $form->handleRequest($request);
49
50
        if ($form->isSubmitted() && $form->isValid()) {
51
            $this->get('badger.game.saver.adventure')->save($adventure);
52
53
            return $this->redirectToRoute('admin_adventure_show', ['id' => $adventure->getId()]);
54
        }
55
56
        return $this->render('@Game/adventures/form.html.twig', [
57
            'adventure' => $adventure,
58
            'form' => $form->createView()
59
        ]);
60
    }
61
62
    /**
63
     * Finds and displays a Adventure entity.
64
     *
65
     * @param Adventure $adventure
66
     *
67
     * @return Response
68
     */
69
    public function showAction(Adventure $adventure)
70
    {
71
        $deleteForm = $this->createDeleteForm($adventure);
72
73
        return $this->render('@Game/adventures/show.html.twig', [
74
            'adventure'   => $adventure,
75
            'delete_form' => $deleteForm->createView(),
76
        ]);
77
    }
78
79
    /**
80
     * Deletes a Adventure entity.
81
     *
82
     * @param Request   $request
83
     * @param Adventure $adventure
84
     *
85
     * @return RedirectResponse
86
     */
87 View Code Duplication
    public function deleteAction(Request $request, Adventure $adventure)
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...
88
    {
89
        $form = $this->createDeleteForm($adventure);
90
        $form->handleRequest($request);
91
92
        if ($form->isSubmitted() && $form->isValid()) {
93
            $badgeRemover = $this->get('badger.game.remover.adventure');
94
            $badgeRemover->remove($adventure);
95
        }
96
97
        return $this->redirectToRoute('admin_adventure_index');
98
    }
99
100
    /**
101
     * Creates a form to delete a Badge entity.
102
     *
103
     * @param Adventure $adventure The Badge entity
104
     *
105
     * @return Form The form
106
     */
107 View Code Duplication
    private function createDeleteForm(Adventure $adventure)
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...
108
    {
109
        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...
110
            ->setAction($this->generateUrl('admin_adventure_delete', ['id' => $adventure->getId()]))
111
            ->setMethod('DELETE')
112
            ->getForm();
113
    }
114
}
115