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