| Conditions | 7 |
| Paths | 228 |
| Total Lines | 93 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 125 |