| Total Complexity | 62 |
| Total Lines | 591 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ResourceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResourceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class ResourceController extends BaseController implements CourseControllerInterface |
||
| 44 | { |
||
| 45 | use CourseControllerTrait; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param Request $request |
||
| 49 | * |
||
| 50 | * @return Response |
||
| 51 | */ |
||
| 52 | public function indexAction(Request $request): Response |
||
| 53 | { |
||
| 54 | return []; |
||
|
|
|||
| 55 | |||
| 56 | $source = new Entity('ChamiloCourseBundle:CDocument'); |
||
| 57 | |||
| 58 | /* @var Grid $grid */ |
||
| 59 | $grid = $this->get('grid'); |
||
| 60 | |||
| 61 | /*$tableAlias = $source->getTableAlias(); |
||
| 62 | $source->manipulateQuery(function (QueryBuilder $query) use ($tableAlias, $course) { |
||
| 63 | $query->andWhere($tableAlias . '.cId = '.$course->getId()); |
||
| 64 | //$query->resetDQLPart('orderBy'); |
||
| 65 | } |
||
| 66 | );*/ |
||
| 67 | |||
| 68 | $repository = $this->get('Chamilo\CourseBundle\Repository\CDocumentRepository'); |
||
| 69 | |||
| 70 | $course = $this->getCourse(); |
||
| 71 | $tool = $repository->getTool('document'); |
||
| 72 | |||
| 73 | $parentId = $request->get('parent'); |
||
| 74 | $parent = null; |
||
| 75 | if (!empty($parentId)) { |
||
| 76 | $parent = $repository->find($parentId); |
||
| 77 | } |
||
| 78 | $resources = $repository->getResourcesByCourse($course, $tool, $parent); |
||
| 79 | |||
| 80 | $source->setData($resources); |
||
| 81 | $grid->setSource($source); |
||
| 82 | |||
| 83 | //$grid->hideFilters(); |
||
| 84 | $grid->setLimits(20); |
||
| 85 | //$grid->isReadyForRedirect(); |
||
| 86 | //$grid->setMaxResults(1); |
||
| 87 | //$grid->setLimits(2); |
||
| 88 | /*$grid->getColumn('id')->manipulateRenderCell( |
||
| 89 | function ($value, $row, $router) use ($course) { |
||
| 90 | //$router = $this->get('router'); |
||
| 91 | return $router->generate( |
||
| 92 | 'chamilo_notebook_show', |
||
| 93 | array('id' => $row->getField('id'), 'course' => $course) |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | );*/ |
||
| 97 | |||
| 98 | $courseIdentifier = $course->getCode(); |
||
| 99 | |||
| 100 | if ($this->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { |
||
| 101 | $deleteMassAction = new MassAction( |
||
| 102 | 'Delete', |
||
| 103 | 'chamilo.controller.notebook:deleteMassAction', |
||
| 104 | true, |
||
| 105 | ['course' => $courseIdentifier] |
||
| 106 | ); |
||
| 107 | $grid->addMassAction($deleteMassAction); |
||
| 108 | } |
||
| 109 | |||
| 110 | $translation = $this->container->get('translator'); |
||
| 111 | |||
| 112 | $myRowAction = new RowAction( |
||
| 113 | $translation->trans('View'), |
||
| 114 | 'app_document_show', |
||
| 115 | false, |
||
| 116 | '_self', |
||
| 117 | ['class' => 'btn btn-secondary'] |
||
| 118 | ); |
||
| 119 | $myRowAction->setRouteParameters(['course' => $courseIdentifier, 'id']); |
||
| 120 | $grid->addRowAction($myRowAction); |
||
| 121 | |||
| 122 | if ($this->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { |
||
| 123 | $myRowAction = new RowAction( |
||
| 124 | $translation->trans('Edit'), |
||
| 125 | 'app_document_update', |
||
| 126 | false, |
||
| 127 | '_self', |
||
| 128 | ['class' => 'btn btn-secondary'] |
||
| 129 | ); |
||
| 130 | $myRowAction->setRouteParameters(['course' => $courseIdentifier, 'id']); |
||
| 131 | $grid->addRowAction($myRowAction); |
||
| 132 | |||
| 133 | $myRowAction = new RowAction( |
||
| 134 | $translation->trans('Delete'), |
||
| 135 | 'app_document_delete', |
||
| 136 | false, |
||
| 137 | '_self', |
||
| 138 | ['class' => 'btn btn-danger', 'form_delete' => true] |
||
| 139 | ); |
||
| 140 | $myRowAction->setRouteParameters(['course' => $courseIdentifier, 'id']); |
||
| 141 | $grid->addRowAction($myRowAction); |
||
| 142 | } |
||
| 143 | |||
| 144 | $grid->addExport(new CSVExport($translation->trans('CSV Export'), 'export', ['course' => $courseIdentifier])); |
||
| 145 | |||
| 146 | $grid->addExport( |
||
| 147 | new ExcelExport( |
||
| 148 | $translation->trans('Excel Export'), |
||
| 149 | 'export', |
||
| 150 | ['course' => $courseIdentifier] |
||
| 151 | ) |
||
| 152 | ); |
||
| 153 | |||
| 154 | return $grid->getGridResponse('ChamiloCoreBundle:Document:index.html.twig', ['parent_id' => $parentId]); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param Request $request |
||
| 159 | * @param string $fileType |
||
| 160 | * |
||
| 161 | * @return RedirectResponse|Response|null |
||
| 162 | */ |
||
| 163 | public function createResource(Request $request, $fileType = 'file') |
||
| 164 | { |
||
| 165 | $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
||
| 166 | |||
| 167 | $this->isGrantedOr403($configuration, ResourceActions::CREATE); |
||
| 168 | /** @var CDocument $newResource */ |
||
| 169 | $newResource = $this->newResourceFactory->create($configuration, $this->factory); |
||
| 170 | $form = $this->resourceFormFactory->create($configuration, $newResource); |
||
| 171 | |||
| 172 | $course = $this->getCourse(); |
||
| 173 | $session = $this->getSession(); |
||
| 174 | $newResource->setCourse($course); |
||
| 175 | $newResource->c_id = $course->getId(); |
||
| 176 | $newResource->setFiletype($fileType); |
||
| 177 | $form->setData($newResource); |
||
| 178 | |||
| 179 | $parentId = $request->get('parent'); |
||
| 180 | $parent = null; |
||
| 181 | if (!empty($parentId)) { |
||
| 182 | /** @var CDocument $parent */ |
||
| 183 | $parent = $this->repository->find($parentId); |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) { |
||
| 187 | /** @var CDocument $newResource */ |
||
| 188 | $newResource = $form->getData(); |
||
| 189 | $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 190 | |||
| 191 | if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
||
| 192 | throw new HttpException($event->getErrorCode(), $event->getMessage()); |
||
| 193 | } |
||
| 194 | if ($event->isStopped()) { |
||
| 195 | $this->flashHelper->addFlashFromEvent($configuration, $event); |
||
| 196 | |||
| 197 | if ($event->hasResponse()) { |
||
| 198 | return $event->getResponse(); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $this->redirectHandler->redirectToIndex($configuration, $newResource); |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($configuration->hasStateMachine()) { |
||
| 205 | $this->stateMachine->apply($configuration, $newResource); |
||
| 206 | } |
||
| 207 | |||
| 208 | //$sharedType = $form->get('shared')->getData(); |
||
| 209 | $shareList = []; |
||
| 210 | $sharedType = 'this_course'; |
||
| 211 | |||
| 212 | switch ($sharedType) { |
||
| 213 | case 'this_course': |
||
| 214 | if (empty($course)) { |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | // Default Chamilo behaviour: |
||
| 218 | // Teachers can edit and students can see |
||
| 219 | $shareList = [ |
||
| 220 | [ |
||
| 221 | 'sharing' => 'course', |
||
| 222 | 'mask' => ResourceNodeVoter::getReaderMask(), |
||
| 223 | 'role' => ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT, |
||
| 224 | 'search' => $course->getId(), |
||
| 225 | ], |
||
| 226 | [ |
||
| 227 | 'sharing' => 'course', |
||
| 228 | 'mask' => ResourceNodeVoter::getEditorMask(), |
||
| 229 | 'role' => ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER, |
||
| 230 | 'search' => $course->getId(), |
||
| 231 | ], |
||
| 232 | ]; |
||
| 233 | break; |
||
| 234 | case 'shared': |
||
| 235 | $shareList = $form->get('rights')->getData(); |
||
| 236 | break; |
||
| 237 | case 'only_me': |
||
| 238 | $shareList = [ |
||
| 239 | [ |
||
| 240 | 'sharing' => 'user', |
||
| 241 | 'only_me' => true, |
||
| 242 | ], |
||
| 243 | ]; |
||
| 244 | break; |
||
| 245 | } |
||
| 246 | |||
| 247 | $resourceNode = $repository->addResourceNode($newResource, $this->getUser(), $parent); |
||
| 248 | |||
| 249 | // Loops all sharing options |
||
| 250 | foreach ($shareList as $share) { |
||
| 251 | $idList = []; |
||
| 252 | if (isset($share['search'])) { |
||
| 253 | $idList = explode(',', $share['search']); |
||
| 254 | } |
||
| 255 | |||
| 256 | $resourceRight = null; |
||
| 257 | if (isset($share['mask'])) { |
||
| 258 | $resourceRight = new ResourceRight(); |
||
| 259 | $resourceRight |
||
| 260 | ->setMask($share['mask']) |
||
| 261 | ->setRole($share['role']) |
||
| 262 | ; |
||
| 263 | } |
||
| 264 | |||
| 265 | // Build links |
||
| 266 | switch ($share['sharing']) { |
||
| 267 | case 'everyone': |
||
| 268 | $repository->addResourceToEveryone( |
||
| 269 | $resourceNode, |
||
| 270 | $resourceRight |
||
| 271 | ); |
||
| 272 | break; |
||
| 273 | case 'course': |
||
| 274 | $repository->addResourceToCourse( |
||
| 275 | $resourceNode, |
||
| 276 | $course, |
||
| 277 | $resourceRight |
||
| 278 | ); |
||
| 279 | break; |
||
| 280 | case 'session': |
||
| 281 | $repository->addResourceToSession( |
||
| 282 | $resourceNode, |
||
| 283 | $course, |
||
| 284 | $session, |
||
| 285 | $resourceRight |
||
| 286 | ); |
||
| 287 | break; |
||
| 288 | case 'user': |
||
| 289 | // Only for me |
||
| 290 | if (isset($share['only_me'])) { |
||
| 291 | $repository->addResourceOnlyToMe($resourceNode); |
||
| 292 | } else { |
||
| 293 | // To other users |
||
| 294 | $repository->addResourceToUserList($resourceNode, $idList); |
||
| 295 | } |
||
| 296 | break; |
||
| 297 | case 'group': |
||
| 298 | // @todo |
||
| 299 | break; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | $newResource |
||
| 304 | ->setCourse($course) |
||
| 305 | ->setFiletype($fileType) |
||
| 306 | ->setSession($session) |
||
| 307 | //->setTitle($title) |
||
| 308 | //->setComment($comment) |
||
| 309 | ->setReadonly(false) |
||
| 310 | ->setResourceNode($resourceNode) |
||
| 311 | ; |
||
| 312 | |||
| 313 | $path = \URLify::filter($newResource->getTitle()); |
||
| 314 | |||
| 315 | switch ($fileType) { |
||
| 316 | case 'folder': |
||
| 317 | $newResource |
||
| 318 | ->setPath($path) |
||
| 319 | ->setSize(0) |
||
| 320 | ; |
||
| 321 | break; |
||
| 322 | case 'file': |
||
| 323 | $newResource |
||
| 324 | ->setPath($path) |
||
| 325 | ->setSize(0) |
||
| 326 | ; |
||
| 327 | break; |
||
| 328 | } |
||
| 329 | |||
| 330 | $this->repository->add($newResource); |
||
| 331 | $postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 332 | |||
| 333 | $newResource->setId($newResource->getIid()); |
||
| 334 | $this->getDoctrine()->getManager()->persist($newResource); |
||
| 335 | $this->getDoctrine()->getManager()->flush(); |
||
| 336 | |||
| 337 | if (!$configuration->isHtmlRequest()) { |
||
| 338 | return $this->viewHandler->handle($configuration, View::create($newResource, Response::HTTP_CREATED)); |
||
| 339 | } |
||
| 340 | |||
| 341 | $this->addFlash('success', 'saved'); |
||
| 342 | |||
| 343 | //$this->flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource); |
||
| 344 | if ($postEvent->hasResponse()) { |
||
| 345 | return $postEvent->getResponse(); |
||
| 346 | } |
||
| 347 | |||
| 348 | return $this->redirectToRoute( |
||
| 349 | 'app_document_show', |
||
| 350 | [ |
||
| 351 | 'id' => $newResource->getIid(), |
||
| 352 | 'course' => $course->getCode(), |
||
| 353 | 'parent_id' => $parentId, |
||
| 354 | ] |
||
| 355 | ); |
||
| 356 | //return $this->redirectHandler->redirectToResource($configuration, $newResource); |
||
| 357 | } |
||
| 358 | |||
| 359 | if (!$configuration->isHtmlRequest()) { |
||
| 360 | return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
||
| 361 | } |
||
| 362 | |||
| 363 | $initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource); |
||
| 364 | if ($initializeEvent->hasResponse()) { |
||
| 365 | return $initializeEvent->getResponse(); |
||
| 366 | } |
||
| 367 | |||
| 368 | $view = View::create() |
||
| 369 | ->setData([ |
||
| 370 | 'configuration' => $configuration, |
||
| 371 | 'metadata' => $this->metadata, |
||
| 372 | 'resource' => $newResource, |
||
| 373 | $this->metadata->getName() => $newResource, |
||
| 374 | 'form' => $form->createView(), |
||
| 375 | 'parent_id' => $parentId, |
||
| 376 | 'file_type' => $fileType, |
||
| 377 | ]) |
||
| 378 | ->setTemplate($configuration->getTemplate(ResourceActions::CREATE.'.html')) |
||
| 379 | ; |
||
| 380 | |||
| 381 | return $this->viewHandler->handle($configuration, $view); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param Request $request |
||
| 386 | * |
||
| 387 | * @return Response |
||
| 388 | */ |
||
| 389 | public function createAction(Request $request): Response |
||
| 390 | { |
||
| 391 | return $this->createResource($request, 'folder'); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param Request $request |
||
| 396 | * |
||
| 397 | * @return Response |
||
| 398 | */ |
||
| 399 | public function createDocumentAction(Request $request): Response |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Shows a resource. |
||
| 406 | * |
||
| 407 | * @param Request $request |
||
| 408 | * @param Glide $glide |
||
| 409 | * |
||
| 410 | * @return Response |
||
| 411 | */ |
||
| 412 | public function getResourceFileAction(Request $request, Glide $glide): Response |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Shows a resource. |
||
| 428 | * |
||
| 429 | * @param Request $request |
||
| 430 | * @param CDocumentRepository $documentRepo |
||
| 431 | * @param Glide $glide |
||
| 432 | * |
||
| 433 | * @return Response |
||
| 434 | */ |
||
| 435 | public function showAction(Request $request): Response |
||
| 436 | { |
||
| 437 | $em = $this->getDoctrine(); |
||
| 438 | |||
| 439 | $id = $request->get('id'); |
||
| 440 | $resourceNode = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($id); |
||
| 441 | |||
| 442 | if (null === $resourceNode) { |
||
| 443 | throw new NotFoundHttpException(); |
||
| 444 | } |
||
| 445 | |||
| 446 | $params = [ |
||
| 447 | 'resource_node' => $resourceNode, |
||
| 448 | ]; |
||
| 449 | return $this->render('@ChamiloCore/Resource/info.html.twig', $params); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param Request $request |
||
| 454 | * |
||
| 455 | * @return Response |
||
| 456 | */ |
||
| 457 | public function updateAction(Request $request): Response |
||
| 546 | } |
||
| 547 | /** |
||
| 548 | * @param Request $request |
||
| 549 | * @param ResourceNode $resourceNode |
||
| 550 | * @param Glide $glide |
||
| 551 | * @param $type |
||
| 552 | * @param string $filter |
||
| 553 | * |
||
| 554 | * @return mixed|StreamedResponse |
||
| 555 | * @throws \League\Flysystem\FileNotFoundException |
||
| 556 | */ |
||
| 557 | private function showFile(Request $request, ResourceNode $resourceNode, Glide $glide, $type, $filter = '') |
||
| 558 | { |
||
| 559 | $fs = $this->container->get('oneup_flysystem.resources_filesystem'); |
||
| 560 | |||
| 561 | $this->denyAccessUnlessGranted( |
||
| 562 | ResourceNodeVoter::VIEW, |
||
| 563 | $resourceNode, |
||
| 564 | 'Unauthorised access to resource' |
||
| 565 | ); |
||
| 566 | $resourceFile = $resourceNode->getResourceFile(); |
||
| 567 | |||
| 568 | if (!$resourceFile) { |
||
| 569 | throw new NotFoundHttpException(); |
||
| 570 | } |
||
| 571 | |||
| 572 | $fileName = $resourceNode->getName(); |
||
| 573 | $filePath = $resourceFile->getFile()->getPathname(); |
||
| 574 | $mimeType = $resourceFile->getMimeType(); |
||
| 575 | |||
| 576 | switch ($type) { |
||
| 577 | case 'download': |
||
| 578 | $forceDownload = true; |
||
| 579 | break; |
||
| 580 | case 'show': |
||
| 581 | default: |
||
| 582 | $forceDownload = false; |
||
| 583 | if (strpos($mimeType, 'image') !== false) { |
||
| 584 | $server = $glide->getServer(); |
||
| 585 | $params = $request->query->all(); |
||
| 586 | |||
| 587 | // The filter overwrites the params from get |
||
| 588 | if (!empty($filter)) { |
||
| 589 | $params = $glide->getFilters()[$filter] ?? []; |
||
| 590 | } |
||
| 591 | |||
| 592 | // The image was cropped manually by the user, so we force to render this version, |
||
| 593 | // no matter other crop parameters. |
||
| 594 | $crop = $resourceFile->getCrop(); |
||
| 595 | if (!empty($crop)) { |
||
| 596 | $params['crop'] = $crop; |
||
| 597 | } |
||
| 598 | |||
| 599 | return $server->getImageResponse($filePath, $params); |
||
| 600 | } |
||
| 601 | break; |
||
| 602 | } |
||
| 603 | |||
| 604 | $stream = $fs->readStream($filePath); |
||
| 605 | $response = new StreamedResponse(function () use ($stream): void { |
||
| 606 | stream_copy_to_stream($stream, fopen('php://output', 'wb')); |
||
| 607 | }); |
||
| 608 | $disposition = $response->headers->makeDisposition( |
||
| 609 | $forceDownload ? ResponseHeaderBag::DISPOSITION_ATTACHMENT : ResponseHeaderBag::DISPOSITION_INLINE, |
||
| 610 | Transliterator::transliterate($fileName) |
||
| 611 | ); |
||
| 612 | $response->headers->set('Content-Disposition', $disposition); |
||
| 613 | $response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream'); |
||
| 614 | |||
| 615 | return $response; |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Upload form. |
||
| 620 | * |
||
| 621 | * @Route("/upload/{type}/{id}", name="resource_upload", methods={"GET", "POST"}, options={"expose"=true}) |
||
| 622 | * |
||
| 623 | * @return Response |
||
| 624 | */ |
||
| 625 | public function showUploadFormAction($type, $id): Response |
||
| 634 | ] |
||
| 635 | ); |
||
| 636 | } |
||
| 637 | } |
||
| 638 |