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