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