Completed
Push — master ( fc1588...7621d0 )
by Julito
16:55
created

ResourceUploadController::upload()   B

Complexity

Conditions 7
Paths 228

Size

Total Lines 93
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 47
nc 228
nop 0
dl 0
loc 93
rs 7.2763
c 2
b 0
f 0

How to fix   Long Method   

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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
7
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
8
use Chamilo\CoreBundle\Repository\ResourceRepository;
9
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
10
use Oneup\UploaderBundle\Controller\BlueimpController;
11
use Oneup\UploaderBundle\Uploader\File\FileInterface;
12
use Oneup\UploaderBundle\Uploader\File\FilesystemFile;
13
use Oneup\UploaderBundle\Uploader\Response\EmptyResponse;
14
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
15
use Symfony\Component\HttpFoundation\File\UploadedFile;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
18
19
/**
20
 * Class ResourceUploaderController.
21
 */
22
class ResourceUploadController extends BlueimpController
23
{
24
    /**
25
     * This will upload an image to the selected node id.
26
     * This action is listend by the ResourceUploadListener
27
     *
28
     * @return JsonResponse
29
     */
30
    public function upload()
31
    {
32
        error_log('upload');
33
        $container = $this->container;
34
        $doctrine = $container->get('doctrine');
35
        $em = $doctrine->getManager();
36
        $request = $this->getRequest();
37
38
        $type = $request->get('type');
39
        $tool = $request->get('tool');
40
        $id = $request->get('id');
41
        $courseId = $request->get('cid');
42
        $sessionId = $request->get('sid');
43
44
        $course = null;
45
        if (!empty($courseId)) {
46
            $course = $doctrine->getRepository('ChamiloCoreBundle:Course')->find($courseId);
47
        }
48
49
        $session = null;
50
        if (!empty($sessionId)) {
51
            $session = $doctrine->getRepository('ChamiloCoreBundle:Session')->find($sessionId);
52
        }
53
54
        $token = $container->get('security.token_storage')->getToken();
55
        $user = $token->getUser();
56
57
        // Create repository from tool and type.
58
        $factory = $container->get('Chamilo\CoreBundle\Repository\ResourceFactory');
59
        /** @var ResourceRepository $repo */
60
        $repo = $factory->createRepository($tool, $type);
61
62
        /** @var ResourceNode $parent */
63
        $parent = $repo->getResourceNodeRepository()->find($id);
64
65
        /*$checker = $container->get('security.authorization_checker');
66
        if (!$checker->isGranted($parent, ResourceNodeVoter::CREATE)) {
67
            return new AccessDeniedException('No permissions');
68
        }*/
69
70
        //$chunked = null !== $request->headers->get('content-range');
71
        $response = new EmptyResponse();
72
        $files = $this->getFiles($request->files);
73
        try {
74
            /** @var UploadedFile $file */
75
            foreach ($files as $file) {
76
                try {
77
                    $title = $file->getClientOriginalName();
78
79
                    if (!($file instanceof FileInterface)) {
80
                        $file = new FilesystemFile($file);
81
                    }
82
83
                    $this->validate($file, $request, $response);
84
85
                    $this->dispatchPreUploadEvent($file, $response, $request);
86
87
                    $resource = $repo->saveUpload($file);
88
89
                    // Uploading file.
90
                    /*$document = new CDocument();
91
                    $document
92
                        ->setFiletype('file')
93
                        ->setTitle($title)
94
                        ->setSize($file->getSize())
95
                    ;*/
96
97
                    $em->persist($resource);
98
                    $resourceNode = $repo->createNodeForResource($resource, $user, $parent, $file);
99
100
                    $repo->addResourceNodeToCourse(
101
                        $resourceNode,
102
                        ResourceLink::VISIBILITY_PUBLISHED,
103
                        $course,
104
                        $session,
105
                        null
106
                    );
107
                    $em->flush();
108
                    // Finish uploading.
109
110
                    $this->dispatchPostEvents($resource, $response, $request);
111
                    /*$chunked ?
112
                        $this->handleChunkedUpload($file, $response, $request) :
113
                        $this->handleUpload($file, $response, $request);*/
114
                } catch (UploadException $e) {
115
                    $this->errorHandler->addException($response, $e);
116
                }
117
            }
118
        } catch (UploadException $e) {
119
            return new JsonResponse([]);
120
        }
121
122
        return $this->createSupportedJsonResponse($response->assemble());
123
    }
124
}
125