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