TagController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 116
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A showAction() 0 7 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Tag;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Request;
8
9
/**
10
 * Tag controller.
11
 */
12
class TagController extends Controller
13
{
14
    use ControllerUtilsTrait;
15
16
    /**
17
     * Lists all tag entities.
18
     */
19
    public function indexAction()
20
    {
21
        $em = $this->getDoctrine()->getManager();
22
23
        $tags = $em->getRepository('AppBundle:Tag')->findAll();
24
25
        return $this->render('tag/index.html.twig', array(
26
            'tags' => $tags,
27
        ));
28
    }
29
30
    /**
31
     * Creates a new tag entity.
32
     */
33
    public function newAction(Request $request)
34
    {
35
        $tag = new Tag();
36
        $form = $this->createForm('AppBundle\Form\TagType', $tag);
37
        $form->handleRequest($request);
38
39
        if ($form->isSubmitted() && $form->isValid()) {
40
            $em = $this->getDoctrine()->getManager();
41
            $em->persist($tag);
42
            $em->flush();
43
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
44
45
            return $this->redirectToRoute('tag_show', array('id' => $tag->getId()));
46
        }
47
48
        return $this->render('tag/edit.html.twig', array(
49
            'title' => $this->get('translator')->trans('form.create', [], 'tag'),
50
            'tag' => $tag,
51
            'form' => $form->createView(),
52
        ));
53
    }
54
55
    /**
56
     * Finds and displays a tag entity.
57
     */
58
    public function showAction(Tag $tag)
59
    {
60
        $deleteForm = $this->createDeleteForm($tag);
61
62
        return $this->render('tag/show.html.twig', array(
63
            'tag' => $tag,
64
            'delete_form' => $deleteForm->createView(),
65
        ));
66
    }
67
68
    /**
69
     * Displays a form to edit an existing tag entity.
70
     */
71
    public function editAction(Request $request, Tag $tag)
72
    {
73
        $deleteForm = $this->createDeleteForm($tag);
74
        $editForm = $this->createForm('AppBundle\Form\TagType', $tag);
75
        $editForm->handleRequest($request);
76
77
        if ($editForm->isSubmitted() && $editForm->isValid()) {
78
            $this->getDoctrine()->getManager()->flush();
79
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
80
81
            return $this->redirectToRoute('tag_edit', array('id' => $tag->getId()));
82
        }
83
84
        return $this->render('tag/edit.html.twig', array(
85
            'title' => $this->get('translator')->trans('form.edit', [], 'tag'),
86
            'tag' => $tag,
87
            'form' => $editForm->createView(),
88
            'delete_form' => $deleteForm->createView(),
89
        ));
90
    }
91
92
    /**
93
     * Deletes a tag entity.
94
     */
95
    public function deleteAction(Request $request, Tag $tag)
96
    {
97
        $form = $this->createDeleteForm($tag);
98
        $form->handleRequest($request);
99
100
        if ($form->isSubmitted() && $form->isValid()) {
101
            $em = $this->getDoctrine()->getManager();
102
            $em->remove($tag);
103
            $em->flush();
104
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
105
106
            return $this->redirectToRoute('tag_index');
107
        }
108
109
        return $this->render('tag/delete.twig', array(
110
            'tag' => $tag,
111
            'form' => $form->createView(),
112
        ));
113
    }
114
115
    /**
116
     * Creates a form to delete a tag entity.
117
     *
118
     * @param Tag $tag The tag entity
119
     *
120
     * @return \Symfony\Component\Form\Form The form
121
     */
122
    private function createDeleteForm(Tag $tag)
123
    {
124
        return $this->createFormBuilder()
125
            ->setAction($this->generateUrl('tag_delete', array('id' => $tag->getId())))
126
            ->setMethod('DELETE')
127
            ->getForm();
128
    }
129
}
130