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