| Conditions | 7 |
| Paths | 252 |
| Total Lines | 93 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 | |||
| 36 | $container = $this->container; |
||
| 37 | $doctrine = $container->get('doctrine'); |
||
| 38 | $em = $doctrine->getManager(); |
||
| 39 | $request = $this->getRequest(); |
||
| 40 | |||
| 41 | $type = $request->get('type'); |
||
| 42 | $tool = $request->get('tool'); |
||
| 43 | $id = $request->get('id'); |
||
| 44 | $courseCode = $request->get('cidReq'); |
||
| 45 | $sessionId = $request->get('id_session'); |
||
| 46 | |||
| 47 | $controller = $container->get('Chamilo\CoreBundle\Controller\ResourceController'); |
||
| 48 | |||
| 49 | $course = null; |
||
| 50 | if (!empty($courseCode)) { |
||
| 51 | $course = $doctrine->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $courseCode]); |
||
| 52 | } |
||
| 53 | |||
| 54 | $session = null; |
||
| 55 | if (!empty($sessionId)) { |
||
| 56 | $session = $doctrine->getRepository('ChamiloCoreBundle:Session')->find($sessionId); |
||
| 57 | } |
||
| 58 | |||
| 59 | $token = $container->get('security.token_storage')->getToken(); |
||
| 60 | $user = $token->getUser(); |
||
| 61 | |||
| 62 | $repo = $controller->getRepository($tool, $type); |
||
| 63 | /** @var ResourceNode $parent */ |
||
| 64 | $parent = $repo->getResourceNodeRepository()->find($id); |
||
| 65 | |||
| 66 | /*$checker = $container->get('security.authorization_checker'); |
||
| 67 | if (!$checker->isGranted($parent, ResourceNodeVoter::CREATE)) { |
||
| 68 | return new AccessDeniedException('No permissions'); |
||
| 69 | }*/ |
||
| 70 | |||
| 71 | $chunked = null !== $request->headers->get('content-range'); |
||
| 72 | |||
| 73 | $response = new EmptyResponse(); |
||
| 74 | $files = $this->getFiles($request->files); |
||
| 75 | try { |
||
| 76 | /** @var UploadedFile $file */ |
||
| 77 | foreach ($files as $file) { |
||
| 78 | try { |
||
| 79 | $title = $file->getClientOriginalName(); |
||
| 80 | |||
| 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 | $document = new CDocument(); |
||
| 90 | $document |
||
| 91 | ->setFiletype('file') |
||
| 92 | ->setTitle($title) |
||
| 93 | ->setSize($file->getSize()) |
||
| 94 | ->setCourse($course) |
||
| 95 | ; |
||
| 96 | |||
| 97 | $em->persist($document); |
||
| 98 | $resourceNode = $repo->createNodeForResource($document, $user, $parent, $file); |
||
| 99 | |||
| 100 | $repo->addResourceNodeToCourse( |
||
| 101 | $resourceNode, |
||
| 102 | ResourceLink::VISIBILITY_PUBLISHED, |
||
| 103 | $course, |
||
| 104 | $session, |
||
| 105 | null |
||
| 106 | ); |
||
| 107 | |||
| 108 | $em->flush(); |
||
| 109 | |||
| 110 | $this->dispatchPostEvents($document, $response, $request); |
||
| 111 | |||
| 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 nothing |
||
| 121 | return new JsonResponse([]); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $this->createSupportedJsonResponse($response->assemble()); |
||
| 125 | } |
||
| 127 |