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