| Total Complexity | 83 |
| Total Lines | 613 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseHomeController 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 CourseHomeController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class CourseHomeController extends ToolBaseController |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @Route("/{cid}/home", name="chamilo_core_course_home") |
||
| 32 | * |
||
| 33 | * @Entity("course", expr="repository.find(cid)") |
||
| 34 | */ |
||
| 35 | public function indexAction(Request $request, CToolRepository $toolRepository, ToolChain $toolChain) |
||
| 200 | ] |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @Route("/{cid}/tool/{toolId}", name="chamilo_core_course_redirect_tool") |
||
| 206 | * |
||
| 207 | * @Entity("course", expr="repository.find(cid)") |
||
| 208 | */ |
||
| 209 | public function redirectTool(Request $request, $toolId, ToolChain $toolChain) |
||
| 210 | { |
||
| 211 | $criteria = ['id' => $toolId]; |
||
| 212 | /** @var CTool $tool */ |
||
| 213 | $tool = $this->getDoctrine()->getRepository('Chamilo\CourseBundle\Entity\CTool')->findOneBy($criteria); |
||
| 214 | $tool = $toolChain->getToolFromName($tool->getTool()->getName()); |
||
| 215 | $url = $tool->getLink().'?'.$this->getCourseUrlQuery(); |
||
| 216 | |||
| 217 | return $this->redirect($url); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @Route("/show/{iconId}", methods={"GET"}) |
||
| 222 | * |
||
| 223 | * @param $iconId |
||
| 224 | * |
||
| 225 | * @return string|null |
||
| 226 | */ |
||
| 227 | public function showIconAction($iconId) |
||
| 228 | { |
||
| 229 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 230 | $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId]; |
||
| 231 | $tool = $this->getRepository('Chamilo\CourseBundle\Entity\CTool')->findOneBy($criteria); |
||
| 232 | if ($tool) { |
||
| 233 | $tool->setVisibility(1); |
||
| 234 | } |
||
| 235 | $entityManager->persist($tool); |
||
| 236 | //$entityManager->flush(); |
||
| 237 | return Display::return_message(get_lang('Visible'), 'confirmation'); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @Route("/hide/{iconId}", methods={"GET"}) |
||
| 242 | * |
||
| 243 | * @param $iconId |
||
| 244 | * |
||
| 245 | * @return string|null |
||
| 246 | */ |
||
| 247 | public function hideIconAction($iconId) |
||
| 248 | { |
||
| 249 | if (!$this->isCourseTeacher()) { |
||
| 250 | return $this->abort(404); |
||
| 251 | } |
||
| 252 | |||
| 253 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 254 | $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId]; |
||
| 255 | $tool = $this->getRepository( |
||
| 256 | 'Chamilo\CourseBundle\Entity\CTool' |
||
| 257 | )->findOneBy($criteria); |
||
| 258 | if ($tool) { |
||
| 259 | $tool->setVisibility(0); |
||
| 260 | } |
||
| 261 | $entityManager->persist($tool); |
||
| 262 | //$entityManager->flush(); |
||
| 263 | return Display::return_message(get_lang('The tool is now invisible.'), 'confirmation'); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @Route("/delete/{iconId}", methods={"GET"}) |
||
| 268 | * |
||
| 269 | * @param $iconId |
||
| 270 | * |
||
| 271 | * @return string|null |
||
| 272 | */ |
||
| 273 | public function deleteIcon($iconId) |
||
| 274 | { |
||
| 275 | if (!$this->isCourseTeacher()) { |
||
| 276 | return $this->abort(404); |
||
| 277 | } |
||
| 278 | |||
| 279 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 280 | $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId, 'added_tool' => 1]; |
||
| 281 | $tool = $this->getRepository( |
||
| 282 | 'Chamilo\CourseBundle\Entity\CTool' |
||
| 283 | )->findOneBy($criteria); |
||
| 284 | $entityManager->remove($tool); |
||
| 285 | //$entityManager->flush(); |
||
| 286 | return Display::return_message(get_lang('Deleted'), 'confirmation'); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @Route("/icon_list", methods={"GET"}) |
||
| 291 | */ |
||
| 292 | public function iconListAction(Request $request) |
||
| 293 | { |
||
| 294 | $em = $this->getDoctrine()->getManager(); |
||
| 295 | $repo = $this->getDoctrine()->getRepository('ChamiloCourseBundle:CTool'); |
||
| 296 | |||
| 297 | $sessionId = (int) $request->get('id_session'); |
||
| 298 | $itemsFromSession = []; |
||
| 299 | if (!empty($sessionId)) { |
||
| 300 | $query = $repo->createQueryBuilder('a'); |
||
| 301 | $query->select('s'); |
||
| 302 | $query->from('Chamilo\CourseBundle\Entity\CTool', 's'); |
||
| 303 | $query->where('s.cId = :courseId AND s.sessionId = :sessionId') |
||
| 304 | ->setParameters( |
||
| 305 | [ |
||
| 306 | 'course' => $this->getCourse()->getId(), |
||
| 307 | 'sessionId' => $sessionId, |
||
| 308 | ] |
||
| 309 | ); |
||
| 310 | $itemsFromSession = $query->getQuery()->getResult(); |
||
| 311 | |||
| 312 | $itemNameList = []; |
||
| 313 | foreach ($itemsFromSession as $item) { |
||
| 314 | $itemNameList[] = $item->getName(); |
||
| 315 | } |
||
| 316 | |||
| 317 | //$itemsFromSession = $this->getRepository()->findBy($criteria); |
||
| 318 | $query = $repo->createQueryBuilder('a'); |
||
| 319 | $query->select('s'); |
||
| 320 | $query->from('Chamilo\CourseBundle\Entity\CTool', 's'); |
||
| 321 | $query->where('s.cId = :courseId AND s.sessionId = 0') |
||
| 322 | ->setParameters( |
||
| 323 | [ |
||
| 324 | 'courseId' => $this->getCourse()->getId(), |
||
| 325 | ] |
||
| 326 | ); |
||
| 327 | if (!empty($itemNameList)) { |
||
| 328 | $query->andWhere($query->expr()->notIn('s.name', $itemNameList)); |
||
| 329 | } |
||
| 330 | $itemsFromCourse = $query->getQuery()->getResult(); |
||
| 331 | } else { |
||
| 332 | $criteria = ['cId' => $this->getCourse()->getId(), 'sessionId' => 0]; |
||
| 333 | $itemsFromCourse = $repo->findBy($criteria); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $this->render( |
||
| 337 | '@ChamiloCourse/Home/list.html.twig', |
||
| 338 | [ |
||
| 339 | 'items_from_course' => $itemsFromCourse, |
||
| 340 | 'items_from_session' => $itemsFromSession, |
||
| 341 | 'links' => '', |
||
| 342 | ] |
||
| 343 | ); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @Route("/{itemName}/add", methods={"GET", "POST"}) |
||
| 348 | * |
||
| 349 | * @param $itemName |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | public function addIconAction($itemName) |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @Route("/{itemId}/edit", methods={"GET"}) |
||
| 411 | */ |
||
| 412 | public function editIconAction($itemId) |
||
| 413 | { |
||
| 414 | if (!$this->isCourseTeacher()) { |
||
| 415 | return $this->abort(404); |
||
| 416 | } |
||
| 417 | |||
| 418 | $sessionId = intval($this->getRequest()->get('id_session')); |
||
| 419 | |||
| 420 | $criteria = ['cId' => $this->getCourse()->getId(), 'id' => $itemId]; |
||
| 421 | /** @var CTool $item */ |
||
| 422 | $item = $this->getRepository()->findOneBy($criteria); |
||
| 423 | |||
| 424 | $form = $this->createForm($this->getFormType(), $item); |
||
| 425 | $form->handleRequest($this->getRequest()); |
||
| 426 | |||
| 427 | if ($form->isValid()) { |
||
| 428 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 429 | $entityManager->persist($item); |
||
| 430 | $entityManager->flush(); |
||
| 431 | |||
| 432 | $customIcon = $item->getCustomIcon(); |
||
| 433 | if (!empty($customIcon)) { |
||
| 434 | $item->createGrayIcon($this->get('imagine')); |
||
| 435 | } |
||
| 436 | |||
| 437 | $this->get('session')->getFlashBag()->add('success', "Updated"); |
||
| 438 | $url = $this->generateUrl('course_home.controller:iconListAction', ['id_session' => $sessionId]); |
||
| 439 | |||
| 440 | return $this->redirect($url); |
||
| 441 | } |
||
| 442 | |||
| 443 | $this->getTemplate()->assign('item', $item); |
||
| 444 | $this->getTemplate()->assign('form', $form->createView()); |
||
| 445 | $this->getTemplate()->assign('links', $this->generateLinks()); |
||
| 446 | |||
| 447 | return $this->render('@ChamiloCourse/Home/edit.html.twig'); |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @Route("/{itemId}/delete", methods={"GET"}) |
||
| 452 | */ |
||
| 453 | public function deleteIconAction($itemId) |
||
| 454 | { |
||
| 455 | if (!$this->isCourseTeacher()) { |
||
| 456 | return $this->abort(404); |
||
| 457 | } |
||
| 458 | |||
| 459 | $criteria = ['cId' => $this->getCourse()->getId(), 'id' => $itemId]; |
||
| 460 | |||
| 461 | /** @var CTool $item */ |
||
| 462 | $item = $this->getRepository()->findOneBy($criteria); |
||
| 463 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 464 | $sessionId = $item->getSessionId(); |
||
| 465 | if (!empty($sessionId)) { |
||
| 466 | $entityManager->remove($item); |
||
| 467 | } else { |
||
| 468 | $item->setCustomIcon(null); |
||
| 469 | $entityManager->persist($item); |
||
| 470 | } |
||
| 471 | $entityManager->flush(); |
||
| 472 | $this->get('session')->getFlashBag()->add('success', "Deleted"); |
||
| 473 | |||
| 474 | $this->getTemplate()->assign('links', $this->generateLinks()); |
||
| 475 | $url = $this->generateUrl('course_home.controller:iconListAction'); |
||
| 476 | |||
| 477 | return $this->redirect($url); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | private function autoLaunch() |
||
| 641 | )); |
||
| 642 | } |
||
| 643 | } |
||
| 644 | } |
||
| 645 |