| Conditions | 8 |
| Paths | 272 |
| Total Lines | 92 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 32 | public function upload() |
||
| 33 | { |
||
| 34 | error_log('upload'); |
||
| 35 | $container = $this->container; |
||
| 36 | $doctrine = $container->get('doctrine'); |
||
| 37 | $em = $doctrine->getManager(); |
||
| 38 | $request = $this->getRequest(); |
||
| 39 | |||
| 40 | $type = $request->get('type'); |
||
| 41 | $tool = $request->get('tool'); |
||
| 42 | $id = $request->get('id'); |
||
| 43 | $courseId = $request->get('cid'); |
||
| 44 | $sessionId = $request->get('sid'); |
||
| 45 | |||
| 46 | $course = null; |
||
| 47 | if (!empty($courseId)) { |
||
| 48 | $course = $doctrine->getRepository('ChamiloCoreBundle:Course')->find($courseId); |
||
| 49 | } |
||
| 50 | |||
| 51 | $session = null; |
||
| 52 | if (!empty($sessionId)) { |
||
| 53 | $session = $doctrine->getRepository('ChamiloCoreBundle:Session')->find($sessionId); |
||
| 54 | } |
||
| 55 | |||
| 56 | $token = $container->get('security.token_storage')->getToken(); |
||
| 57 | $user = $token->getUser(); |
||
| 58 | |||
| 59 | // Create repository from tool and type. |
||
| 60 | $factory = $container->get('Chamilo\CoreBundle\Repository\ResourceFactory'); |
||
| 61 | /** @var ResourceRepository $repo */ |
||
| 62 | $repo = $factory->createRepository($tool, $type); |
||
| 63 | |||
| 64 | /** @var ResourceNode $parent */ |
||
| 65 | $parent = $repo->getResourceNodeRepository()->find($id); |
||
| 66 | |||
| 67 | /*$checker = $container->get('security.authorization_checker'); |
||
| 68 | if (!$checker->isGranted($parent, ResourceNodeVoter::CREATE)) { |
||
| 69 | return new AccessDeniedException('No permissions'); |
||
| 70 | }*/ |
||
| 71 | |||
| 72 | //$chunked = null !== $request->headers->get('content-range'); |
||
| 73 | $response = new EmptyResponse(); |
||
| 74 | $files = $this->getFiles($request->files); |
||
| 75 | |||
| 76 | try { |
||
| 77 | /** @var UploadedFile $file */ |
||
| 78 | foreach ($files as $file) { |
||
| 79 | try { |
||
| 80 | //$title = $file->getClientOriginalName(); |
||
| 81 | if (!($file instanceof FileInterface)) { |
||
| 82 | $file = new FilesystemFile($file); |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->validate($file, $request, $response); |
||
| 86 | |||
| 87 | $this->dispatchPreUploadEvent($file, $response, $request); |
||
| 88 | |||
| 89 | $resource = $repo->saveUpload($file, $course, $session); |
||
| 90 | |||
| 91 | if ($resource instanceof CDocument) { |
||
| 92 | $resource |
||
| 93 | ->setCourse($course) |
||
| 94 | ->setSession($session) |
||
| 95 | ; |
||
| 96 | } |
||
| 97 | |||
| 98 | $repo->addResourceToCourseWithParent( |
||
| 99 | $resource, |
||
| 100 | $parent, |
||
| 101 | ResourceLink::VISIBILITY_PUBLISHED, |
||
| 102 | $user, |
||
| 103 | $course, |
||
| 104 | $session, |
||
| 105 | null, |
||
| 106 | $file |
||
| 107 | ); |
||
| 108 | $em->flush(); |
||
| 109 | // Finish uploading. |
||
| 110 | |||
| 111 | $this->dispatchPostEvents($resource, $response, $request); |
||
| 112 | /*$chunked ? |
||
| 113 | $this->handleChunkedUpload($file, $response, $request) : |
||
| 114 | $this->handleUpload($file, $response, $request);*/ |
||
| 115 | } catch (UploadException $e) { |
||
| 116 | $this->errorHandler->addException($response, $e); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } catch (UploadException $e) { |
||
| 120 | return new JsonResponse([]); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this->createSupportedJsonResponse($response->assemble()); |
||
| 124 | } |
||
| 126 |