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