Completed
Pull Request — master (#1672)
by Derek Stephen
15:58
created

ChooserController::uploadFileAction()   D

Complexity

Conditions 12
Paths 288

Size

Total Lines 97

Duplication

Lines 23
Ratio 23.71 %

Importance

Changes 0
Metric Value
dl 23
loc 97
rs 4.0921
c 0
b 0
f 0
cc 12
nc 288
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Controller;
4
5
use Doctrine\ORM\EntityNotFoundException;
6
use Kunstmaan\MediaBundle\AdminList\MediaAdminListConfigurator;
7
use Kunstmaan\MediaBundle\Entity\Folder;
8
use Kunstmaan\MediaBundle\Entity\Media;
9
use Kunstmaan\MediaBundle\Form\FolderType;
10
use Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler;
11
use Kunstmaan\MediaBundle\Helper\MediaManager;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * ChooserController.
21
 */
22
class ChooserController extends Controller
23
{
24
25
    /**
26
     * @Route("/chooser", name="KunstmaanMediaBundle_chooser")
27
     * @param Request $request
28
     * @throws EntityNotFoundException
29
     *
30
     * @return RedirectResponse
31
     */
32
    public function chooserIndexAction(Request $request)
33
    {
34
        $em       = $this->getDoctrine()->getManager();
35
        $session  = $request->getSession();
36
        $folderId = false;
37
38
        $type            = $request->get('type', 'all');
39
        $cKEditorFuncNum = $request->get('CKEditorFuncNum');
40
        $linkChooser     = $request->get('linkChooser');
41
42
        // Go to the last visited folder
43 View Code Duplication
        if ($session->get('last-media-folder')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
            try {
45
                $em->getRepository('KunstmaanMediaBundle:Folder')->getFolder($session->get('last-media-folder'));
46
                $folderId = $session->get('last-media-folder');
47
            } catch (EntityNotFoundException $e) {
48
                $folderId = false;
49
            }
50
        }
51
52
        if (!$folderId) {
53
            // Redirect to the first top folder
54
            /* @var Folder $firstFolder */
55
            $firstFolder = $em->getRepository('KunstmaanMediaBundle:Folder')->getFirstTopFolder();
56
            $folderId    = $firstFolder->getId();
57
        }
58
59
        $params = [
60
            'folderId'        => $folderId,
61
            'type'            => $type,
62
            'CKEditorFuncNum' => $cKEditorFuncNum,
63
            'linkChooser'     => $linkChooser
64
        ];
65
66
        return $this->redirect($this->generateUrl('KunstmaanMediaBundle_chooser_show_folder', $params));
67
    }
68
69
    /**
70
     * @Route("/chooser/{folderId}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_chooser_show_folder")
71
     * @Template()
72
     * @param Request $request
73
     * @param $folderId
74
     * @throws EntityNotFoundException
75
     *
76
     * @return array
77
     */
78
    public function chooserShowFolderAction(Request $request, $folderId)
79
    {
80
        $em      = $this->getDoctrine()->getManager();
81
        $session = $request->getSession();
82
83
        $type            = $request->get('type');
84
        $cKEditorFuncNum = $request->get('CKEditorFuncNum');
85
        $linkChooser     = $request->get('linkChooser');
86
87
        // Remember the last visited folder in the session
88
        $session->set('last-media-folder', $folderId);
89
90
        // Check when user switches between thumb -and list view
91
        $viewMode = $request->query->get('viewMode');
92 View Code Duplication
        if ($viewMode && $viewMode == 'list-view') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
            $session->set('media-list-view', true);
94
        } elseif ($viewMode && $viewMode == 'thumb-view') {
95
            $session->remove('media-list-view');
96
        }
97
98
        /* @var MediaManager $mediaHandler */
99
        $mediaHandler = $this->get('kunstmaan_media.media_manager');
100
101
        /* @var Folder $folder */
102
        $folder = $em->getRepository('KunstmaanMediaBundle:Folder')->getFolder($folderId);
103
104
        /** @var AbstractMediaHandler $handler */
105
        $handler = null;
106
        if ($type) {
107
            $handler = $mediaHandler->getHandlerForType($type);
108
        }
109
110
        /* @var MediaManager $mediaManager */
111
        $mediaManager = $this->get('kunstmaan_media.media_manager');
112
113
        $adminListConfigurator = new MediaAdminListConfigurator($em, $mediaManager, $folder, $request);
0 ignored issues
show
Compatibility introduced by
$em of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
114
        $adminList             = $this->get('kunstmaan_adminlist.factory')->createList($adminListConfigurator);
115
        $adminList->bindRequest($request);
116
117
        $sub = new Folder();
118
        $sub->setParent($folder);
119
        $subForm  = $this->createForm(FolderType::class, $sub, ['folder' => $sub]);
120
121
        $linkChooserLink = null;
122 View Code Duplication
        if (!empty($linkChooser)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
123
            $params = [];
124
            if (!empty($cKEditorFuncNum)) {
125
                $params['CKEditorFuncNum'] = $cKEditorFuncNum;
126
                $routeName                 = 'KunstmaanNodeBundle_ckselecturl';
127
            } else {
128
                $routeName = 'KunstmaanNodeBundle_selecturl';
129
            }
130
            $linkChooserLink = $this->generateUrl($routeName, $params);
131
        }
132
133
        $viewVariabels = [
134
            'cKEditorFuncNum' => $cKEditorFuncNum,
135
            'linkChooser'     => $linkChooser,
136
            'linkChooserLink' => $linkChooserLink,
137
            'mediamanager'    => $mediaManager,
138
            'foldermanager'   => $this->get('kunstmaan_media.folder_manager'),
139
            'handler'         => $handler,
140
            'type'            => $type,
141
            'folder'          => $folder,
142
            'adminlist'       => $adminList,
143
            'subform'         => $subForm->createView()
144
        ];
145
146
        /* generate all forms */
147
        $forms = [];
148
149
        foreach($mediaManager->getFolderAddActions()  as $addAction ) {
150
            $forms[$addAction['type']] = $this->createTypeFormView($mediaHandler,$addAction['type']);
151
        }
152
153
        $viewVariabels['forms'] = $forms;
154
155
        return $viewVariabels;
156
    }
157
158
    /**
159
     * @param MediaManager $mediaManager
160
     * @param String       $type
161
     *
162
     * @return \Symfony\Component\Form\FormView
163
     */
164
    private function createTypeFormView(MediaManager $mediaManager, $type)
165
    {
166
        $handler = $mediaManager->getHandlerForType($type);
167
        $media   = new Media();
168
        $helper  = $handler->getFormHelper($media);
169
170
        return $this->createForm($handler->getFormType(), $helper, $handler->getFormTypeOptions())->createView();
171
    }
172
173
174
    /**
175
     * @Route("/upload-file/{type}", requirements={"folderId" = "\d+"}, name="KunstmaanMediaBundle_upload_file")
176
     * @Template()
177
     * @param Request $request
178
     * @throws EntityNotFoundException
179
     *
180
     * @return array
181
     */
182
    public function uploadFileAction(Request $request)
183
    {
184
        $em      = $this->getDoctrine()->getManager();
185
        $session = $request->getSession();
186
187
        $type            = $request->get('type');
188
        $cKEditorFuncNum = $request->get('CKEditorFuncNum');
189
        $linkChooser     = $request->get('linkChooser');
190
191 View Code Duplication
        if ($session->get('last-media-folder')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
192
            try {
193
                $em->getRepository('KunstmaanMediaBundle:Folder')->getFolder($session->get('last-media-folder'));
194
                $folderId = $session->get('last-media-folder');
195
            } catch (EntityNotFoundException $e) {
196
                $folderId = false;
197
            }
198
        }
199
200
        if (!isset($folderId)) {
201
            // Redirect to the first top folder
202
            /* @var Folder $firstFolder */
203
            $firstFolder = $em->getRepository('KunstmaanMediaBundle:Folder')->getFirstTopFolder();
204
            $folderId    = $firstFolder->getId();
205
        }
206
207
        // Remember the last visited folder in the session
208
        $session->set('last-media-folder', $folderId);
209
210
        // Check when user switches between thumb -and list view
211
        $viewMode = $request->query->get('viewMode');
212 View Code Duplication
        if ($viewMode && $viewMode == 'list-view') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
213
            $session->set('media-list-view', true);
214
        } elseif ($viewMode && $viewMode == 'thumb-view') {
215
            $session->remove('media-list-view');
216
        }
217
218
        /* @var MediaManager $mediaHandler */
219
        $mediaHandler = $this->get('kunstmaan_media.media_manager');
220
221
        /* @var Folder $folder */
222
        $folder = $em->getRepository('KunstmaanMediaBundle:Folder')->getFolder($folderId);
223
224
        /** @var AbstractMediaHandler $handler */
225
        $handler = null;
226
        if ($type) {
227
            $handler = $mediaHandler->getHandlerForType($type);
228
        }
229
230
        /* @var MediaManager $mediaManager */
231
        $mediaManager = $this->get('kunstmaan_media.media_manager');
232
233
        $adminListConfigurator = new MediaAdminListConfigurator($em, $mediaManager, $folder, $request);
0 ignored issues
show
Compatibility introduced by
$em of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
234
        $adminList             = $this->get('kunstmaan_adminlist.factory')->createList($adminListConfigurator);
235
        $adminList->bindRequest($request);
236
237
        $sub = new Folder();
238
        $sub->setParent($folder);
239
        $subForm  = $this->createForm(FolderType::class, $sub, ['folder' => $sub]);
240
241
        $linkChooserLink = null;
242 View Code Duplication
        if (!empty($linkChooser)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
243
            $params = [];
244
            if (!empty($cKEditorFuncNum)) {
245
                $params['CKEditorFuncNum'] = $cKEditorFuncNum;
246
                $routeName                 = 'KunstmaanNodeBundle_ckselecturl';
247
            } else {
248
                $routeName = 'KunstmaanNodeBundle_selecturl';
249
            }
250
            $linkChooserLink = $this->generateUrl($routeName, $params);
251
        }
252
253
        $viewVariabels = [
254
            'cKEditorFuncNum' => $cKEditorFuncNum,
255
            'linkChooser'     => $linkChooser,
256
            'linkChooserLink' => $linkChooserLink,
257
            'mediamanager'    => $mediaManager,
258
            'foldermanager'   => $this->get('kunstmaan_media.folder_manager'),
259
            'handler'         => $handler,
260
            'type'            => $type,
261
            'folder'          => $folder,
262
            'adminlist'       => $adminList,
263
            'subform'         => $subForm->createView()
264
        ];
265
266
        /* generate all forms */
267
        $forms = [];
268
269
        $handler = $mediaManager->getHandlerForType($type);
270
        $actions = $handler->getAddFolderActions();
271
        foreach($actions  as $addAction ) {
272
            $forms[$addAction['type']] = $this->createTypeFormView($mediaHandler,$addAction['type']);
273
        }
274
275
        $viewVariabels['forms'] = $forms;
276
277
        return $viewVariabels;
278
    }
279
280
    /**
281
     * @Route("/media-uploaded", name="KunstmaanMediaBundle_media_uploaded")
282
     * @param Request $request
283
     *
284
     * @return JsonResponse
285
     */
286
    public function mediaUploadedAction(Request $request)
287
    {
288
        $id = $request->get('mediaId');
289
        $repository = $this->getDoctrine()->getRepository(Media::class);
290
        /** @var Media $media */
291
        $media = $repository->find($id);
292
        $data = [
293
            'id' => $media->getId(),
294
            'url' => $media->getUrl(),
295
            'title' => $media->getName(),
296
        ];
297
        return new JsonResponse($data);
298
    }
299
}
300