| Total Complexity | 77 |
| Total Lines | 625 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class Container |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * @var ContainerInterface |
||
| 59 | */ |
||
| 60 | public static $container; |
||
| 61 | public static $session; |
||
| 62 | public static $request; |
||
| 63 | public static $configuration; |
||
| 64 | public static $environment; |
||
| 65 | public static $urlGenerator; |
||
| 66 | public static $checker; |
||
| 67 | /** @var TranslatorInterface */ |
||
| 68 | public static $translator; |
||
| 69 | public static $mailer; |
||
| 70 | public static $template; |
||
| 71 | |||
| 72 | public static $rootDir; |
||
| 73 | public static $logDir; |
||
| 74 | public static $tempDir; |
||
| 75 | public static $dataDir; |
||
| 76 | public static $courseDir; |
||
| 77 | public static $assets; |
||
| 78 | public static $htmlEditor; |
||
| 79 | public static $twig; |
||
| 80 | public static $roles; |
||
| 81 | /** @var string */ |
||
| 82 | public static $legacyTemplate = '@ChamiloTheme/Layout/layout_one_col.html.twig'; |
||
| 83 | private static $settingsManager; |
||
| 84 | private static $userManager; |
||
| 85 | private static $siteManager; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param ContainerInterface $container |
||
| 89 | */ |
||
| 90 | public static function setContainer($container) |
||
| 91 | { |
||
| 92 | self::$container = $container; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $parameter |
||
| 97 | */ |
||
| 98 | public static function getParameter($parameter) |
||
| 99 | { |
||
| 100 | if (self::$container->hasParameter($parameter)) { |
||
| 101 | return self::$container->getParameter($parameter); |
||
| 102 | } |
||
| 103 | |||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public static function getEnvironment() |
||
| 111 | { |
||
| 112 | return self::$container->get('kernel')->getEnvironment(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return RoleHierarchy |
||
| 117 | */ |
||
| 118 | public static function getRoles() |
||
| 119 | { |
||
| 120 | return self::$container->get('security.role_hierarchy'); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public static function getLogDir() |
||
| 127 | { |
||
| 128 | return self::$container->get('kernel')->getLogDir(); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public static function getCacheDir() |
||
| 135 | { |
||
| 136 | return self::$container->get('kernel')->getCacheDir().'/'; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public static function getProjectDir() |
||
| 143 | { |
||
| 144 | if (isset(self::$container)) { |
||
| 145 | return self::$container->get('kernel')->getProjectDir().'/'; |
||
| 146 | } |
||
| 147 | |||
| 148 | return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/'; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public static function isInstalled() |
||
| 155 | { |
||
| 156 | return self::$container->get('kernel')->isInstalled(); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return Environment |
||
| 161 | */ |
||
| 162 | public static function getTwig() |
||
| 163 | { |
||
| 164 | return self::$container->get('twig'); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return \Symfony\Bundle\TwigBundle\TwigEngine |
||
| 169 | */ |
||
| 170 | public static function getTemplating() |
||
| 171 | { |
||
| 172 | return self::$container->get('templating'); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return Editor |
||
| 177 | */ |
||
| 178 | public static function getHtmlEditor() |
||
| 179 | { |
||
| 180 | return self::$container->get('chamilo_core.html_editor'); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return object|Request |
||
| 185 | */ |
||
| 186 | public static function getRequest() |
||
| 187 | { |
||
| 188 | if (null === self::$container) { |
||
| 189 | return null; |
||
| 190 | } |
||
| 191 | |||
| 192 | if (!empty(self::$request)) { |
||
| 193 | return self::$request; |
||
| 194 | } |
||
| 195 | |||
| 196 | return self::$container->get('request_stack'); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param Request $request |
||
| 201 | */ |
||
| 202 | public static function setRequest($request) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return Session|false |
||
| 209 | */ |
||
| 210 | public static function getSession() |
||
| 211 | { |
||
| 212 | if (self::$container && self::$container->has('session')) { |
||
| 213 | return self::$container->get('session'); |
||
| 214 | } |
||
| 215 | |||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return AuthorizationChecker |
||
| 221 | */ |
||
| 222 | public static function getAuthorizationChecker() |
||
| 223 | { |
||
| 224 | return self::$container->get('security.authorization_checker'); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage |
||
| 229 | */ |
||
| 230 | public static function getTokenStorage() |
||
| 231 | { |
||
| 232 | return self::$container->get('security.token_storage'); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return TranslatorInterface |
||
| 237 | */ |
||
| 238 | public static function getTranslator() |
||
| 239 | { |
||
| 240 | if (isset(self::$translator)) { |
||
| 241 | return self::$translator; |
||
| 242 | } |
||
| 243 | |||
| 244 | if (self::$container) { |
||
| 245 | return self::$container->get('translator'); |
||
| 246 | } |
||
| 247 | |||
| 248 | return false; |
||
|
|
|||
| 249 | } |
||
| 250 | |||
| 251 | public static function getMailer() |
||
| 252 | { |
||
| 253 | return self::$container->get('Symfony\Component\Mailer\Mailer'); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return SettingsManager |
||
| 258 | */ |
||
| 259 | public static function getSettingsManager() |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param SettingsManager $manager |
||
| 266 | */ |
||
| 267 | public static function setSettingsManager($manager) |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return \Chamilo\CourseBundle\Manager\SettingsManager |
||
| 274 | */ |
||
| 275 | public static function getCourseSettingsManager() |
||
| 276 | { |
||
| 277 | return self::$container->get('Chamilo\CourseBundle\Manager\SettingsManager'); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return \Doctrine\ORM\EntityManager |
||
| 282 | */ |
||
| 283 | public static function getEntityManager() |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return UserManager |
||
| 290 | */ |
||
| 291 | public static function getUserManager() |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return CAttendanceRepository |
||
| 298 | */ |
||
| 299 | public static function getAttendanceRepository() |
||
| 300 | { |
||
| 301 | return self::$container->get('Chamilo\CourseBundle\Repository\CAttendanceRepository'); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return CAnnouncementRepository |
||
| 306 | */ |
||
| 307 | public static function getAnnouncementRepository() |
||
| 308 | { |
||
| 309 | return self::$container->get('Chamilo\CourseBundle\Repository\CAnnouncementRepository'); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return AccessUrlRepository |
||
| 314 | */ |
||
| 315 | public static function getAccessUrlRepository() |
||
| 316 | { |
||
| 317 | return self::$container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository'); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return CAnnouncementAttachmentRepository |
||
| 322 | */ |
||
| 323 | public static function getAnnouncementAttachmentRepository() |
||
| 324 | { |
||
| 325 | return self::$container->get('Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository'); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return CourseRepository |
||
| 330 | */ |
||
| 331 | public static function getCourseRepository() |
||
| 332 | { |
||
| 333 | return self::$container->get('Chamilo\CoreBundle\Repository\CourseRepository'); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return CourseCategoryRepository|object|null |
||
| 338 | */ |
||
| 339 | public static function getCourseCategoryRepository() |
||
| 340 | { |
||
| 341 | return self::$container->get('Chamilo\CoreBundle\Repository\CourseCategoryRepository'); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return CCalendarEventRepository |
||
| 346 | */ |
||
| 347 | public static function getCalendarEventRepository() |
||
| 348 | { |
||
| 349 | return self::$container->get('Chamilo\CourseBundle\Repository\CCalendarEventRepository'); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return CCalendarEventAttachmentRepository |
||
| 354 | */ |
||
| 355 | public static function getCalendarEventAttachmentRepository() |
||
| 356 | { |
||
| 357 | return self::$container->get('Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository'); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return CDocumentRepository |
||
| 362 | */ |
||
| 363 | public static function getDocumentRepository() |
||
| 364 | { |
||
| 365 | return self::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository'); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return CQuizRepository |
||
| 370 | */ |
||
| 371 | public static function getExerciseRepository() |
||
| 372 | { |
||
| 373 | return self::$container->get('Chamilo\CourseBundle\Repository\CQuizRepository'); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return CExerciseCategoryRepository |
||
| 378 | */ |
||
| 379 | public static function getExerciseCategoryRepository() |
||
| 380 | { |
||
| 381 | return self::$container->get('Chamilo\CourseBundle\Repository\CExerciseCategoryRepository'); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return CForumForumRepository |
||
| 386 | */ |
||
| 387 | public static function getForumRepository() |
||
| 388 | { |
||
| 389 | return self::$container->get('Chamilo\CourseBundle\Repository\CForumForumRepository'); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return CForumCategoryRepository |
||
| 394 | */ |
||
| 395 | public static function getForumCategoryRepository() |
||
| 396 | { |
||
| 397 | return self::$container->get('Chamilo\CourseBundle\Repository\CForumCategoryRepository'); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return CForumPostRepository |
||
| 402 | */ |
||
| 403 | public static function getForumPostRepository() |
||
| 404 | { |
||
| 405 | return self::$container->get('Chamilo\CourseBundle\Repository\CForumPostRepository'); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return CForumAttachmentRepository |
||
| 410 | */ |
||
| 411 | public static function getForumAttachmentRepository() |
||
| 412 | { |
||
| 413 | return self::$container->get('Chamilo\CourseBundle\Repository\CForumAttachmentRepository'); |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @return CForumThreadRepository |
||
| 418 | */ |
||
| 419 | public static function getForumThreadRepository() |
||
| 420 | { |
||
| 421 | return self::$container->get('Chamilo\CourseBundle\Repository\CForumThreadRepository'); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return CGroupInfoRepository |
||
| 426 | */ |
||
| 427 | public static function getGroupInfoRepository() |
||
| 428 | { |
||
| 429 | return self::$container->get('Chamilo\CourseBundle\Repository\CGroupInfoRepository'); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @return CQuizQuestionRepository |
||
| 434 | */ |
||
| 435 | public static function getQuestionRepository() |
||
| 436 | { |
||
| 437 | return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionRepository'); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return CQuizQuestionCategoryRepository |
||
| 442 | */ |
||
| 443 | public static function getQuestionCategoryRepository() |
||
| 444 | { |
||
| 445 | return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository'); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return CLinkRepository |
||
| 450 | */ |
||
| 451 | public static function getLinkRepository() |
||
| 452 | { |
||
| 453 | return self::$container->get('Chamilo\CourseBundle\Repository\CLinkRepository'); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return CLinkCategoryRepository |
||
| 458 | */ |
||
| 459 | public static function getLinkCategoryRepository() |
||
| 460 | { |
||
| 461 | return self::$container->get('Chamilo\CourseBundle\Repository\CLinkCategoryRepository'); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return CLpRepository |
||
| 466 | */ |
||
| 467 | public static function getLpRepository() |
||
| 468 | { |
||
| 469 | return self::$container->get('Chamilo\CourseBundle\Repository\CLpRepository'); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @return CLpCategoryRepository |
||
| 474 | */ |
||
| 475 | public static function getLpCategoryRepository() |
||
| 476 | { |
||
| 477 | return self::$container->get('Chamilo\CourseBundle\Repository\CLpCategoryRepository'); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return UserRepository |
||
| 482 | */ |
||
| 483 | public static function getUserRepository() |
||
| 484 | { |
||
| 485 | return self::$container->get('Chamilo\UserBundle\Repository\UserRepository'); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return IllustrationRepository |
||
| 490 | */ |
||
| 491 | public static function getIllustrationRepository() |
||
| 492 | { |
||
| 493 | return self::$container->get('Chamilo\CoreBundle\Repository\IllustrationRepository'); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return CShortcutRepository |
||
| 498 | */ |
||
| 499 | public static function getShortcutRepository() |
||
| 500 | { |
||
| 501 | return self::$container->get('Chamilo\CourseBundle\Repository\CShortcutRepository'); |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return CStudentPublicationRepository |
||
| 506 | */ |
||
| 507 | public static function getStudentPublicationRepository() |
||
| 508 | { |
||
| 509 | return self::$container->get('Chamilo\CourseBundle\Repository\CStudentPublicationRepository'); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @return CStudentPublicationAssignmentRepository |
||
| 514 | */ |
||
| 515 | public static function getStudentPublicationAssignmentRepository() |
||
| 516 | { |
||
| 517 | return self::$container->get('Chamilo\CourseBundle\Repository\CStudentPublicationAssignmentRepository'); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return CStudentPublicationCommentRepository |
||
| 522 | */ |
||
| 523 | public static function getStudentPublicationCommentRepository() |
||
| 524 | { |
||
| 525 | return self::$container->get('Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository'); |
||
| 526 | } |
||
| 527 | |||
| 528 | public static function getThematicRepository() |
||
| 529 | { |
||
| 530 | return self::$container->get('Chamilo\CourseBundle\Repository\CThematicRepository'); |
||
| 531 | } |
||
| 532 | |||
| 533 | public static function getThematicPlanRepository() |
||
| 534 | { |
||
| 535 | return self::$container->get('Chamilo\CourseBundle\Repository\CThematicPlanRepository'); |
||
| 536 | } |
||
| 537 | |||
| 538 | public static function getThematicAdvanceRepository() |
||
| 539 | { |
||
| 540 | return self::$container->get('Chamilo\CourseBundle\Repository\CThematicAdvanceRepository'); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param UserManager $manager |
||
| 545 | */ |
||
| 546 | public static function setUserManager($manager) |
||
| 547 | { |
||
| 548 | self::$userManager = $manager; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @return SiteManager |
||
| 553 | */ |
||
| 554 | public static function getSiteManager() |
||
| 555 | { |
||
| 556 | return self::$siteManager; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param UserManager $manager |
||
| 561 | */ |
||
| 562 | public static function setSiteManager($manager) |
||
| 563 | { |
||
| 564 | self::$siteManager = $manager; |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return \Sonata\UserBundle\Entity\GroupManager |
||
| 569 | */ |
||
| 570 | public static function getGroupManager() |
||
| 571 | { |
||
| 572 | return self::$container->get('fos_user.group_manager'); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher |
||
| 577 | */ |
||
| 578 | public static function getEventDispatcher() |
||
| 579 | { |
||
| 580 | return self::$container->get('event_dispatcher'); |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @return \Symfony\Component\Form\FormFactory |
||
| 585 | */ |
||
| 586 | public static function getFormFactory() |
||
| 587 | { |
||
| 588 | return self::$container->get('form.factory'); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param string $message |
||
| 593 | * @param string $type error|success|warning|danger |
||
| 594 | */ |
||
| 595 | public static function addFlash($message, $type = 'success') |
||
| 596 | { |
||
| 597 | $session = self::getSession(); |
||
| 598 | $session->getFlashBag()->add($type, $message); |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return object|\Symfony\Cmf\Component\Routing\ChainRouter |
||
| 603 | */ |
||
| 604 | public static function getRouter() |
||
| 605 | { |
||
| 606 | return self::$container->get('router'); |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @return ToolChain |
||
| 611 | */ |
||
| 612 | public static function getToolChain() |
||
| 613 | { |
||
| 614 | return self::$container->get(ToolChain::class); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @param ContainerInterface $container |
||
| 619 | * @param bool $setSession |
||
| 620 | */ |
||
| 621 | public static function setLegacyServices($container, $setSession = true) |
||
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Gets a sonata page. |
||
| 643 | * |
||
| 644 | * @param string $slug |
||
| 645 | * |
||
| 646 | * @return Page |
||
| 647 | */ |
||
| 648 | public static function getPage($slug) |
||
| 672 | } |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @throws \Exception |
||
| 676 | */ |
||
| 677 | public static function instantiateHook(string $class): HookEventInterface |
||
| 680 | } |
||
| 681 | } |
||
| 682 |