| Total Complexity | 81 |
| Total Lines | 479 |
| 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 |
||
| 70 | class Container |
||
| 71 | { |
||
| 72 | /** |
||
| 73 | * @var ContainerInterface |
||
| 74 | */ |
||
| 75 | public static $container; |
||
| 76 | public static $session; |
||
| 77 | public static $request; |
||
| 78 | public static $configuration; |
||
| 79 | public static $environment; |
||
| 80 | public static $urlGenerator; |
||
| 81 | public static $checker; |
||
| 82 | /** @var TranslatorInterface */ |
||
| 83 | public static $translator; |
||
| 84 | public static $mailer; |
||
| 85 | public static $template; |
||
| 86 | |||
| 87 | public static $rootDir; |
||
| 88 | public static $logDir; |
||
| 89 | public static $tempDir; |
||
| 90 | public static $dataDir; |
||
| 91 | public static $courseDir; |
||
| 92 | public static $assets; |
||
| 93 | public static $htmlEditor; |
||
| 94 | public static $twig; |
||
| 95 | public static $roles; |
||
| 96 | /** @var string */ |
||
| 97 | public static $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig'; |
||
| 98 | //private static $settingsManager; |
||
| 99 | //private static $userManager; |
||
| 100 | //private static $siteManager; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param ContainerInterface $container |
||
| 104 | */ |
||
| 105 | public static function setContainer($container) |
||
| 106 | { |
||
| 107 | self::$container = $container; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $parameter |
||
| 112 | */ |
||
| 113 | public static function getParameter($parameter) |
||
| 114 | { |
||
| 115 | if (self::$container->hasParameter($parameter)) { |
||
| 116 | return self::$container->getParameter($parameter); |
||
| 117 | } |
||
| 118 | |||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public static function getEnvironment() |
||
| 126 | { |
||
| 127 | return self::$container->get('kernel')->getEnvironment(); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return RoleHierarchy |
||
| 132 | */ |
||
| 133 | public static function getRoles() |
||
| 134 | { |
||
| 135 | return self::$container->get('security.role_hierarchy'); |
||
| 136 | } |
||
| 137 | |||
| 138 | public static function getLogDir(): string |
||
| 139 | { |
||
| 140 | return self::$container->get('kernel')->getLogDir(); |
||
| 141 | } |
||
| 142 | |||
| 143 | public static function getCacheDir(): string |
||
| 144 | { |
||
| 145 | return self::$container->get('kernel')->getCacheDir().'/'; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public static function getProjectDir() |
||
| 152 | { |
||
| 153 | if (isset(self::$container)) { |
||
| 154 | return self::$container->get('kernel')->getProjectDir().'/'; |
||
| 155 | } |
||
| 156 | |||
| 157 | return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/'; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public static function isInstalled() |
||
| 164 | { |
||
| 165 | return self::$container->get('kernel')->isInstalled(); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return Environment |
||
| 170 | */ |
||
| 171 | public static function getTwig() |
||
| 172 | { |
||
| 173 | return self::$container->get('twig'); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return Environment |
||
| 178 | */ |
||
| 179 | public static function getTemplating() |
||
| 180 | { |
||
| 181 | return self::$container->get('twig'); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return Editor |
||
| 186 | */ |
||
| 187 | public static function getHtmlEditor() |
||
| 188 | { |
||
| 189 | return self::$container->get('chamilo_core.html_editor'); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return object|Request |
||
| 194 | */ |
||
| 195 | public static function getRequest() |
||
| 196 | { |
||
| 197 | if (null === self::$container) { |
||
| 198 | return null; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (!empty(self::$request)) { |
||
| 202 | return self::$request; |
||
| 203 | } |
||
| 204 | |||
| 205 | return self::$container->get('request_stack'); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param Request $request |
||
| 210 | */ |
||
| 211 | public static function setRequest($request) |
||
| 212 | { |
||
| 213 | self::$request = $request; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return Session|false |
||
| 218 | */ |
||
| 219 | public static function getSession() |
||
| 220 | { |
||
| 221 | if (self::$container && self::$container->has('session')) { |
||
| 222 | return self::$container->get('session'); |
||
| 223 | } |
||
| 224 | |||
| 225 | return false; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return AuthorizationChecker |
||
| 230 | */ |
||
| 231 | public static function getAuthorizationChecker() |
||
| 232 | { |
||
| 233 | return self::$container->get('security.authorization_checker'); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage |
||
| 238 | */ |
||
| 239 | public static function getTokenStorage() |
||
| 240 | { |
||
| 241 | return self::$container->get('security.token_storage'); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return TranslatorInterface |
||
| 246 | */ |
||
| 247 | public static function getTranslator() |
||
| 248 | { |
||
| 249 | if (isset(self::$translator)) { |
||
| 250 | return self::$translator; |
||
| 251 | } |
||
| 252 | |||
| 253 | if (self::$container) { |
||
| 254 | return self::$container->get('translator'); |
||
| 255 | } |
||
| 256 | |||
| 257 | return false; |
||
| 258 | } |
||
| 259 | |||
| 260 | public static function getMailer(): Mailer |
||
| 261 | { |
||
| 262 | return self::$container->get(Mailer::class); |
||
| 263 | } |
||
| 264 | |||
| 265 | public static function getSettingsManager(): SettingsManager |
||
| 266 | { |
||
| 267 | return self::$container->get('chamilo.settings.manager'); |
||
| 268 | } |
||
| 269 | |||
| 270 | public static function getCourseSettingsManager(): \Chamilo\CourseBundle\Manager\SettingsManager |
||
| 271 | { |
||
| 272 | return self::$container->get('Chamilo\CourseBundle\Manager\SettingsManager'); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return \Doctrine\ORM\EntityManager |
||
| 277 | */ |
||
| 278 | public static function getEntityManager() |
||
| 281 | } |
||
| 282 | |||
| 283 | public static function getUserManager(): UserRepository |
||
| 284 | { |
||
| 285 | return self::$container->get(UserRepository::class); |
||
| 286 | } |
||
| 287 | |||
| 288 | public static function getAttendanceRepository(): CAttendanceRepository |
||
| 289 | { |
||
| 290 | return self::$container->get(CAttendanceRepository::class); |
||
| 291 | } |
||
| 292 | |||
| 293 | public static function getAnnouncementRepository(): CAnnouncementRepository |
||
| 294 | { |
||
| 295 | return self::$container->get(CAnnouncementRepository::class); |
||
| 296 | } |
||
| 297 | |||
| 298 | public static function getAccessUrlRepository(): AccessUrlRepository |
||
| 299 | { |
||
| 300 | return self::$container->get(AccessUrlRepository::class); |
||
| 301 | } |
||
| 302 | |||
| 303 | public static function getAnnouncementAttachmentRepository(): CAnnouncementAttachmentRepository |
||
| 304 | { |
||
| 305 | return self::$container->get(CAnnouncementAttachmentRepository::class); |
||
| 306 | } |
||
| 307 | |||
| 308 | public static function getCourseRepository(): CourseRepository |
||
| 309 | { |
||
| 310 | return self::$container->get(CourseRepository::class); |
||
| 311 | } |
||
| 312 | |||
| 313 | public static function getSessionRepository(): SessionRepository |
||
| 314 | { |
||
| 315 | return self::$container->get(SessionRepository::class); |
||
| 316 | } |
||
| 317 | |||
| 318 | public static function getCourseCategoryRepository(): CourseCategoryRepository |
||
| 319 | { |
||
| 320 | return self::$container->get(CourseCategoryRepository::class); |
||
| 321 | } |
||
| 322 | |||
| 323 | public static function getCourseDescriptionRepository(): CCourseDescriptionRepository |
||
| 324 | { |
||
| 325 | return self::$container->get(CCourseDescriptionRepository::class); |
||
| 326 | } |
||
| 327 | |||
| 328 | public static function getGlossaryRepository(): CGlossaryRepository |
||
| 329 | { |
||
| 330 | return self::$container->get(CGlossaryRepository::class); |
||
| 331 | } |
||
| 332 | |||
| 333 | public static function getCalendarEventRepository(): CCalendarEventRepository |
||
| 334 | { |
||
| 335 | return self::$container->get(CCalendarEventRepository::class); |
||
| 336 | } |
||
| 337 | |||
| 338 | public static function getCalendarEventAttachmentRepository(): CCalendarEventAttachmentRepository |
||
| 339 | { |
||
| 340 | return self::$container->get(CCalendarEventAttachmentRepository::class); |
||
| 341 | } |
||
| 342 | |||
| 343 | public static function getDocumentRepository(): CDocumentRepository |
||
| 344 | { |
||
| 345 | return self::$container->get(CDocumentRepository::class); |
||
| 346 | } |
||
| 347 | |||
| 348 | public static function getQuizRepository(): CQuizRepository |
||
| 349 | { |
||
| 350 | return self::$container->get(CQuizRepository::class); |
||
| 351 | } |
||
| 352 | |||
| 353 | public static function getExerciseCategoryRepository(): CExerciseCategoryRepository |
||
| 354 | { |
||
| 355 | return self::$container->get(CExerciseCategoryRepository::class); |
||
| 356 | } |
||
| 357 | |||
| 358 | public static function getForumRepository(): CForumForumRepository |
||
| 359 | { |
||
| 360 | return self::$container->get(CForumForumRepository::class); |
||
| 361 | } |
||
| 362 | |||
| 363 | public static function getForumCategoryRepository(): CForumCategoryRepository |
||
| 364 | { |
||
| 365 | return self::$container->get(CForumCategoryRepository::class); |
||
| 366 | } |
||
| 367 | |||
| 368 | public static function getForumPostRepository(): CForumPostRepository |
||
| 369 | { |
||
| 370 | return self::$container->get(CForumPostRepository::class); |
||
| 371 | } |
||
| 372 | |||
| 373 | public static function getForumAttachmentRepository(): CForumAttachmentRepository |
||
| 374 | { |
||
| 375 | return self::$container->get(CForumAttachmentRepository::class); |
||
| 376 | } |
||
| 377 | |||
| 378 | public static function getForumThreadRepository(): CForumThreadRepository |
||
| 379 | { |
||
| 380 | return self::$container->get(CForumThreadRepository::class); |
||
| 381 | } |
||
| 382 | |||
| 383 | public static function getGroupRepository(): CGroupRepository |
||
| 384 | { |
||
| 385 | return self::$container->get(CGroupRepository::class); |
||
| 386 | } |
||
| 387 | |||
| 388 | public static function getGroupCategoryRepository(): CGroupCategoryRepository |
||
| 389 | { |
||
| 390 | return self::$container->get(CGroupCategoryRepository::class); |
||
| 391 | } |
||
| 392 | |||
| 393 | public static function getQuestionRepository(): CQuizQuestionRepository |
||
| 394 | { |
||
| 395 | return self::$container->get(CQuizQuestionRepository::class); |
||
| 396 | } |
||
| 397 | |||
| 398 | public static function getQuestionCategoryRepository(): CQuizQuestionCategoryRepository |
||
| 399 | { |
||
| 400 | return self::$container->get(CQuizQuestionCategoryRepository::class); |
||
| 401 | } |
||
| 402 | |||
| 403 | public static function getLinkRepository(): CLinkRepository |
||
| 404 | { |
||
| 405 | return self::$container->get(CLinkRepository::class); |
||
| 406 | } |
||
| 407 | |||
| 408 | public static function getLinkCategoryRepository(): CLinkCategoryRepository |
||
| 409 | { |
||
| 410 | return self::$container->get(CLinkCategoryRepository::class); |
||
| 411 | } |
||
| 412 | |||
| 413 | public static function getLpRepository(): CLpRepository |
||
| 414 | { |
||
| 415 | return self::$container->get(CLpRepository::class); |
||
| 416 | } |
||
| 417 | |||
| 418 | public static function getLpCategoryRepository(): CLpCategoryRepository |
||
| 419 | { |
||
| 420 | return self::$container->get(CLpCategoryRepository::class); |
||
| 421 | } |
||
| 422 | |||
| 423 | public static function getNotebookRepository(): CNotebookRepository |
||
| 424 | { |
||
| 425 | return self::$container->get(CNotebookRepository::class); |
||
| 426 | } |
||
| 427 | |||
| 428 | public static function getUserRepository(): UserRepository |
||
| 429 | { |
||
| 430 | return self::$container->get(UserRepository::class); |
||
| 431 | } |
||
| 432 | |||
| 433 | public static function getIllustrationRepository(): IllustrationRepository |
||
| 434 | { |
||
| 435 | return self::$container->get(IllustrationRepository::class); |
||
| 436 | } |
||
| 437 | |||
| 438 | public static function getShortcutRepository(): CShortcutRepository |
||
| 439 | { |
||
| 440 | return self::$container->get(CShortcutRepository::class); |
||
| 441 | } |
||
| 442 | |||
| 443 | public static function getStudentPublicationRepository(): CStudentPublicationRepository |
||
| 444 | { |
||
| 445 | return self::$container->get(CStudentPublicationRepository::class); |
||
| 446 | } |
||
| 447 | |||
| 448 | public static function getStudentPublicationAssignmentRepository(): CStudentPublicationAssignmentRepository |
||
| 449 | { |
||
| 450 | return self::$container->get(CStudentPublicationAssignmentRepository::class); |
||
| 451 | } |
||
| 452 | |||
| 453 | public static function getStudentPublicationCommentRepository(): CStudentPublicationCommentRepository |
||
| 454 | { |
||
| 455 | return self::$container->get(CStudentPublicationCommentRepository::class); |
||
| 456 | } |
||
| 457 | |||
| 458 | public static function getStudentPublicationCorrectionRepository(): CStudentPublicationCorrectionRepository |
||
| 459 | { |
||
| 460 | return self::$container->get(CStudentPublicationCorrectionRepository::class); |
||
| 461 | } |
||
| 462 | |||
| 463 | public static function getSequenceResourceRepository(): SequenceResourceRepository |
||
| 464 | { |
||
| 465 | return self::$container->get(SequenceResourceRepository::class); |
||
| 466 | } |
||
| 467 | |||
| 468 | public static function getSequenceRepository(): SequenceRepository |
||
| 469 | { |
||
| 470 | return self::$container->get(SequenceRepository::class); |
||
| 471 | } |
||
| 472 | |||
| 473 | public static function getSurveyRepository(): CSurveyRepository |
||
| 474 | { |
||
| 475 | return self::$container->get(CSurveyRepository::class); |
||
| 476 | } |
||
| 477 | |||
| 478 | public static function getSurveyQuestionRepository(): CSurveyQuestionRepository |
||
| 479 | { |
||
| 480 | return self::$container->get(CSurveyQuestionRepository::class); |
||
| 481 | } |
||
| 482 | |||
| 483 | public static function getThematicRepository(): CThematicRepository |
||
| 484 | { |
||
| 485 | return self::$container->get(CThematicRepository::class); |
||
| 486 | } |
||
| 487 | |||
| 488 | public static function getThematicPlanRepository(): CThematicPlanRepository |
||
| 489 | { |
||
| 490 | return self::$container->get(CThematicPlanRepository::class); |
||
| 491 | } |
||
| 492 | |||
| 493 | public static function getThematicAdvanceRepository(): CThematicAdvanceRepository |
||
| 494 | { |
||
| 495 | return self::$container->get(CThematicAdvanceRepository::class); |
||
| 496 | } |
||
| 497 | |||
| 498 | public static function getBlogRepository(): CBlogRepository |
||
| 499 | { |
||
| 500 | return self::$container->get(CBlogRepository::class); |
||
| 501 | } |
||
| 502 | |||
| 503 | public static function getWikiRepository(): CBlogRepository |
||
| 504 | { |
||
| 505 | return self::$container->get(CWikiRepository::class); |
||
| 506 | } |
||
| 507 | |||
| 508 | public static function getFormFactory(): FormFactory |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $message |
||
| 515 | * @param string $type error|success|warning|danger |
||
| 516 | */ |
||
| 517 | public static function addFlash($message, $type = 'success') |
||
| 521 | } |
||
| 522 | |||
| 523 | public static function getRouter(): Router |
||
| 524 | { |
||
| 525 | return self::$container->get('router'); |
||
| 526 | } |
||
| 527 | |||
| 528 | public static function getToolChain(): ToolChain |
||
| 529 | { |
||
| 530 | return self::$container->get(ToolChain::class); |
||
| 531 | } |
||
| 532 | |||
| 533 | public static function getAssetRepository(): AssetRepository |
||
| 534 | { |
||
| 535 | return self::$container->get(AssetRepository::class); |
||
| 536 | } |
||
| 537 | |||
| 538 | public static function setLegacyServices(ContainerInterface $container, bool $setSession = true) |
||
| 539 | { |
||
| 540 | \Database::setConnection($container->get('doctrine.dbal.default_connection')); |
||
| 541 | $em = $container->get('doctrine.orm.entity_manager'); |
||
| 542 | \Database::setManager($em); |
||
| 543 | \CourseManager::setEntityManager($em); |
||
| 544 | \CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager')); |
||
| 545 | // Setting course tool chain (in order to create tools to a course) |
||
| 546 | \CourseManager::setToolList($container->get(ToolChain::class)); |
||
| 547 | if ($setSession) { |
||
| 548 | self::$session = $container->get('session'); |
||
| 549 | } |
||
| 550 | } |
||
| 551 | } |
||
| 552 |