Passed
Push — master ( 041328...8c7a21 )
by IT
03:09
created

VideoController::uploadVideoAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 2
nop 2
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Video;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Request;
8
9
/**
10
 * Video controller.
11
 */
12
class VideoController extends Controller
13
{
14
    use ControllerUtilsTrait;
15
16
    /**
17
     * Lists all video entities.
18
     */
19
    public function indexAction()
20
    {
21
        $em = $this->getDoctrine()->getManager();
22
23
        $videos = $em->getRepository('AppBundle:Video')->findAll();
24
25
        return $this->render('video/index.html.twig', array(
26
            'videos' => $videos,
27
        ));
28
    }
29
30
    /**
31
     * Creates a new video entity.
32
     */
33
    public function newAction(Request $request)
34
    {
35
        $video = new Video();
36
        $video->setCreator($this->getUser());
37
        $form = $this->createForm('AppBundle\Form\VideoType', $video, ['action_type' => 'create']);
38
        $form->handleRequest($request);
39
40
        if ($form->isSubmitted() && $form->isValid()) {
41
            $em = $this->getDoctrine()->getManager();
42
            $em->persist($video);
43
            $em->flush();
44
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
45
46
            return $this->redirectToRoute('video_upload', array('id' => $video->getId()));
47
        }
48
49
        return $this->render('video/edit.html.twig', array(
50
            'title' => $this->get('translator')->trans('form_create.create', [], 'video'),
51
            'video' => $video,
52
            'form' => $form->createView(),
53
        ));
54
    }
55
56
    public function uploadVideoAction(Request $request, Video $video)
57
    {
58
        $form = $this->createForm('AppBundle\Form\VideoFileType', $video);
59
        $form->handleRequest($request);
60
61
        if ($form->isSubmitted() && $form->isValid()) {
62
            $em = $this->getDoctrine()->getManager();
63
            $video->setUpdatedAt(new \DateTime());
64
            $em->persist($video);
65
            $em->flush();
66
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
67
68
            return $this->redirectToRoute('video_show', array('id' => $video->getId()));
69
        }
70
        return $this->render('video/upload.html.twig', array(
71
            'title' => $this->get('translator')->trans('form_create.create', [], 'video'),
72
            'video' => $video,
73
            'form' => $form->createView(),
74
        ));
75
    }
76
77
    /**
78
     * Finds and displays a video entity.
79
     */
80
    public function showAction(Video $video)
81
    {
82
        $deleteForm = $this->createDeleteForm($video);
83
84
        return $this->render('video/show.html.twig', array(
85
            'video' => $video,
86
            'delete_form' => $deleteForm->createView(),
87
        ));
88
    }
89
90
    /**
91
     * Displays a form to edit an existing video entity.
92
     */
93
    public function editAction(Request $request, Video $video)
94
    {
95
        $editForm = $this->createForm('AppBundle\Form\VideoType', $video, ['action_type' => 'edit']);
96
        $editForm->handleRequest($request);
97
98
        if ($editForm->isSubmitted() && $editForm->isValid()) {
99
            $video->setUpdatedAt(new \DateTime());
100
            $em = $this->getDoctrine()->getManager();
101
            $em->persist($video);
102
            $em->flush();
103
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
104
            return $this->redirectToRoute('video_show', ['id' => $video->getId()]);
105
        }
106
107
        return $this->render('video/edit.html.twig', array(
108
            'title' => $this->get('translator')->trans('form_create.edit', [], 'video'),
109
            'video' => $video,
110
            'form' => $editForm->createView(),
111
        ));
112
    }
113
114
    public function searchAction(Request $request)
115
    {
116
        $form = $this->createForm('AppBundle\Form\VideoSearchType');
117
        $form->handleRequest($request);
118
        $videos = [];
119
        if ($form->isSubmitted() && $form->isValid()) {
120
            $criteria = $form->getData();
121
            $videos = $this->getDoctrine()->getRepository(Video::class)->findVideo($criteria);
122
        }
123
124
        return $this->render('video/search.twig', array(
125
            'form' => $form->createView(),
126
            'submitted' => $form->isSubmitted(),
127
            'videos' => $videos,
128
        ));
129
    }
130
131
    /**
132
     * Deletes a video entity.
133
     */
134
    public function deleteAction(Request $request, Video $video)
135
    {
136
        $form = $this->createDeleteForm($video);
137
        $form->handleRequest($request);
138
139
        if ($form->isSubmitted() && $form->isValid()) {
140
            $em = $this->getDoctrine()->getManager();
141
            $em->remove($video);
142
            $em->flush();
143
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
144
145
            return $this->redirectToRoute('video_index');
146
        }
147
148
        return $this->render('video/delete.twig', array(
149
            'video' => $video,
150
            'form' => $form->createView(),
151
        ));
152
    }
153
154
    /**
155
     * Creates a form to delete a video entity.
156
     *
157
     * @param Video $video The video entity
158
     *
159
     * @return \Symfony\Component\Form\Form The form
160
     */
161
    private function createDeleteForm(Video $video)
162
    {
163
        return $this->createFormBuilder()
164
            ->setAction($this->generateUrl('video_delete', array('id' => $video->getId())))
165
//            ->setMethod('DELETE')
166
            ->getForm();
167
    }
168
}
169