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