BadgeController::deleteAction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Controller;
4
5
use Badger\Bundle\GameBundle\Form\BadgeType;
6
use Badger\Component\Game\Model\BadgeInterface;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\Form\Extension\Core\Type\FileType;
9
use Symfony\Component\Form\Form;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
/**
15
 * Badge controller for admin CRUD.
16
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
17
 */
18
class BadgeController extends Controller
19
{
20
    /**
21
     * Lists all Badge entities.
22
     *
23
     * @return Response
24
     */
25
    public function indexAction()
26
    {
27
        $badges = $this->get('badger.game.repository.badge')
28
            ->findAll();
29
30
        return $this->render('@Game/badges/index.html.twig', [
31
            'badges' => $badges,
32
        ]);
33
    }
34
35
    /**
36
     * Creates a new Badge entity.
37
     *
38
     * @param Request $request
39
     *
40
     * @return RedirectResponse|Response
41
     */
42
    public function newAction(Request $request)
43
    {
44
        $badgeFactory = $this->get('badger.game.badge.factory');
45
        $badge = $badgeFactory->create();
46
47
        $form = $this->createForm(BadgeType::class, $badge);
48
        $form->add('file', FileType::class, ['label' => 'Badge image']);
49
        $form->remove('imagePath');
50
        $form->handleRequest($request);
51
52
        if ($form->isSubmitted() && $form->isValid()) {
53
            $badge->upload();
54
55
            $badgeSaver = $this->get('badger.game.saver.badge');
56
            $badgeSaver->save($badge);
57
58
            return $this->redirectToRoute('admin_badge_show', ['id' => $badge->getId()]);
59
        }
60
61
        return $this->render('@Game/badges/new.html.twig', [
62
            'badge' => $badge,
63
            'form' => $form->createView()
64
        ]);
65
    }
66
67
    /**
68
     * Finds and displays a Badge entity.
69
     *
70
     * @param string $id
71
     *
72
     * @return Response
73
     */
74 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...
75
    {
76
        $badge = $this->get('badger.game.repository.badge')->find($id);
77
        $deleteForm = $this->createDeleteForm($badge);
78
79
        return $this->render('@Game/badges/show.html.twig', [
80
            'badge' => $badge,
81
            'delete_form' => $deleteForm->createView(),
82
        ]);
83
    }
84
85
    /**
86
     * Displays a form to edit an existing Badge entity.
87
     *
88
     * @param Request $request
89
     * @param string  $id
90
     *
91
     * @return RedirectResponse|Response
92
     */
93
    public function editAction(Request $request, $id)
94
    {
95
        $badge = $this->get('badger.game.repository.badge')->find($id);
96
        $editForm = $this->createForm(BadgeType::class, $badge);
97
        $editForm->add('file');
98
        $editForm->remove('imagePath');
99
        $editForm->handleRequest($request);
100
101
        if ($editForm->isSubmitted() && $editForm->isValid()) {
102
            $badge->upload();
103
104
            $badgeSaver = $this->get('badger.game.saver.badge');
105
            $badgeSaver->save($badge);
106
107
            return $this->redirectToRoute('admin_badge_edit', ['id' => $badge->getId()]);
108
        }
109
110
        return $this->render('@Game/badges/edit.html.twig', [
111
            'badge' => $badge,
112
            'edit_form' => $editForm->createView(),
113
        ]);
114
    }
115
116
    /**
117
     * Deletes a Badge entity.
118
     *
119
     * @param Request $request
120
     * @param string  $id
121
     *
122
     * @return RedirectResponse
123
     */
124 View Code Duplication
    public function deleteAction(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...
125
    {
126
        $badge = $this->get('badger.game.repository.badge')->find($id);
127
        $form = $this->createDeleteForm($badge);
128
        $form->handleRequest($request);
129
130
        if ($form->isSubmitted() && $form->isValid()) {
131
            $badgeRemover = $this->get('badger.game.remover.badge');
132
            $badgeRemover->remove($badge);
133
        }
134
135
        return $this->redirectToRoute('admin_badge_index');
136
    }
137
138
    /**
139
     * Creates a form to delete a Badge entity.
140
     *
141
     * @param BadgeInterface $badge The Badge entity
142
     *
143
     * @return Form The form
144
     */
145 View Code Duplication
    private function createDeleteForm(BadgeInterface $badge)
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...
146
    {
147
        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...
148
            ->setAction($this->generateUrl('admin_badge_delete', ['id' => $badge->getId()]))
149
            ->setMethod('DELETE')
150
            ->getForm()
151
        ;
152
    }
153
}
154