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