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