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