| Total Complexity | 70 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Changes | 23 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Context |
||
| 22 | { |
||
| 23 | private static $context; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Returns the current context |
||
| 28 | * |
||
| 29 | * @param \Aimeos\Base\Config\Iface Configuration object |
||
| 30 | * @return \Aimeos\MShop\ContextIface Context object |
||
| 31 | */ |
||
| 32 | public static function get(\Aimeos\Base\Config\Iface $config) : \Aimeos\MShop\ContextIface |
||
| 33 | { |
||
| 34 | if (self::$context === null) { |
||
| 35 | // TYPO3 specifc context with password hasher |
||
| 36 | $context = new \Aimeos\MShop\Context\Item\Typo3(); |
||
| 37 | $context->setConfig($config); |
||
| 38 | |||
| 39 | self::addDataBaseManager($context); |
||
| 40 | self::addFilesystemManager($context); |
||
| 41 | self::addMessageQueueManager($context); |
||
| 42 | self::addLogger($context); |
||
| 43 | self::addCache($context); |
||
| 44 | self::addMailer($context); |
||
| 45 | self::addNonce($context); |
||
| 46 | self::addProcess($context); |
||
| 47 | self::addSession($context); |
||
| 48 | self::addHasher($context); |
||
| 49 | self::addToken($context); |
||
| 50 | self::addUser($context); |
||
| 51 | self::addGroups($context); |
||
| 52 | self::addDateTime($context); |
||
| 53 | |||
| 54 | self::$context = $context; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Use local TS configuration from plugins |
||
| 58 | self::$context->setConfig($config); |
||
| 59 | |||
| 60 | return self::$context; |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Adds the cache object to the context |
||
| 66 | * |
||
| 67 | * @param \Aimeos\MShop\ContextIface $context Context object including config |
||
| 68 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 69 | */ |
||
| 70 | protected static function addCache(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 71 | { |
||
| 72 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_cache']) |
||
| 73 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_cache'])) |
||
| 74 | ) { |
||
| 75 | return $fcn($context); |
||
| 76 | } |
||
| 77 | |||
| 78 | $cacheName = \Aimeos\Aimeos\Base::getExtConfig('cacheName', 'Typo3'); |
||
| 79 | |||
| 80 | switch ($cacheName) { |
||
| 81 | case 'None': |
||
| 82 | $context->config()->set('client/html/basket/cache/enable', false); |
||
| 83 | $cache = \Aimeos\Base\Cache\Factory::create('None'); |
||
| 84 | break; |
||
| 85 | |||
| 86 | case 'Typo3': |
||
| 87 | $manager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class); |
||
| 88 | $cache = new \Aimeos\Base\Cache\Typo3($manager->getCache('aimeos')); |
||
| 89 | break; |
||
| 90 | |||
| 91 | default: |
||
| 92 | $cache = (new \Aimeos\MAdmin\Cache\Manager\Standard($context))->getCache(); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $context->setCache($cache); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Adds the database manager object to the context |
||
| 101 | * |
||
| 102 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 103 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 104 | */ |
||
| 105 | protected static function addDatabaseManager(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 106 | { |
||
| 107 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_dbm']) |
||
| 108 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_dbm'])) |
||
| 109 | ) { |
||
| 110 | return $fcn($context); |
||
| 111 | } |
||
| 112 | |||
| 113 | $dbm = new \Aimeos\Base\DB\Manager\Standard($context->config()->get('resource', []), 'DBAL'); |
||
| 114 | return $context->setDatabaseManager($dbm); |
||
| 115 | } |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Adds the filesystem manager object to the context |
||
| 120 | * |
||
| 121 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 122 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 123 | */ |
||
| 124 | protected static function addFilesystemManager(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 125 | { |
||
| 126 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_fsm']) |
||
| 127 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_fsm'])) |
||
| 128 | ) { |
||
| 129 | return $fcn($context); |
||
| 130 | } |
||
| 131 | |||
| 132 | $fsm = new \Aimeos\Base\Filesystem\Manager\Standard($context->config()->get('resource', [])); |
||
| 133 | return $context->setFilesystemManager($fsm); |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds the password hasher object to the context |
||
| 139 | * |
||
| 140 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 141 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 142 | */ |
||
| 143 | protected static function addHasher(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 144 | { |
||
| 145 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_hasher']) |
||
| 146 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_hasher'])) |
||
| 147 | ) { |
||
| 148 | return $fcn($context); |
||
| 149 | } |
||
| 150 | |||
| 151 | $factory = GeneralUtility::makeInstance('TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory'); |
||
| 152 | return $context->setPassword(new \Aimeos\Base\Password\Typo3($factory->getDefaultHashInstance('FE'))); |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds the logger object to the context |
||
| 158 | * |
||
| 159 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 160 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 161 | */ |
||
| 162 | protected static function addLogger(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 163 | { |
||
| 164 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_logger']) |
||
| 165 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_logger'])) |
||
| 166 | ) { |
||
| 167 | return $fcn($context); |
||
| 168 | } |
||
| 169 | |||
| 170 | return $context->setLogger(\Aimeos\MAdmin::create($context, 'log')); |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Adds the mailer object to the context |
||
| 176 | * |
||
| 177 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 178 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 179 | */ |
||
| 180 | protected static function addMailer(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 190 | })); |
||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Adds the message queue manager object to the context |
||
| 196 | * |
||
| 197 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 198 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 199 | */ |
||
| 200 | protected static function addMessageQueueManager(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 201 | { |
||
| 202 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_mqueue']) |
||
| 203 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_mqueue'])) |
||
| 204 | ) { |
||
| 205 | return $fcn($context); |
||
| 206 | } |
||
| 207 | |||
| 208 | $mqm = new \Aimeos\Base\MQueue\Manager\Standard($context->config()->get('resource', [])); |
||
| 209 | return $context->setMessageQueueManager($mqm); |
||
| 210 | } |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Adds the nonce value for inline JS to the context |
||
| 215 | * |
||
| 216 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 217 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 218 | */ |
||
| 219 | protected static function addNonce(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Adds the process object to the context |
||
| 233 | * |
||
| 234 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 235 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 236 | */ |
||
| 237 | protected static function addProcess(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds the session object to the context |
||
| 260 | * |
||
| 261 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 262 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 263 | */ |
||
| 264 | protected static function addSession(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * Adds the session token to the context |
||
| 288 | * |
||
| 289 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 290 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 291 | */ |
||
| 292 | protected static function addToken(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 293 | { |
||
| 294 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_token']) |
||
| 295 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_token'])) |
||
| 296 | ) { |
||
| 297 | return $fcn($context); |
||
| 298 | } |
||
| 299 | |||
| 300 | $session = $context->session(); |
||
| 301 | |||
| 302 | if (($token = $session->get('token')) === null) { |
||
| 303 | $token = $GLOBALS['TSFE']->fe_user->id ?? md5(microtime(true) . getmypid()); |
||
| 304 | $session->set('token', $token); |
||
| 305 | } |
||
| 306 | |||
| 307 | return $context->setToken($token); |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Adds the user ID and editor name to the context |
||
| 313 | * |
||
| 314 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 315 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 316 | */ |
||
| 317 | protected static function addUser(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 318 | { |
||
| 319 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_user']) |
||
| 320 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_user'])) |
||
| 321 | ) { |
||
| 322 | return $fcn($context); |
||
| 323 | } |
||
| 324 | |||
| 325 | $appType = null; |
||
| 326 | $t3context = GeneralUtility::makeInstance('TYPO3\CMS\Core\Context\Context'); |
||
| 327 | |||
| 328 | if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) { |
||
| 329 | $appType = \TYPO3\CMS\Core\Http\ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST']); |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($appType && $appType->isFrontend() && $t3context->getPropertyFromAspect('frontend.user', 'isLoggedIn')) { |
||
| 333 | $context->setUserId($GLOBALS['TSFE']->fe_user->user[$GLOBALS['TSFE']->fe_user->userid_column]); |
||
| 334 | $context->setEditor((string) $GLOBALS['TSFE']->fe_user->user['username']); |
||
| 335 | } elseif ($appType && $appType->isBackend() && isset($GLOBALS['BE_USER']->user['username'])) { |
||
| 336 | $context->setEditor((string) $GLOBALS['BE_USER']->user['username']); |
||
| 337 | } else { |
||
| 338 | $context->setEditor((string) GeneralUtility::getIndpEnv('REMOTE_ADDR')); |
||
| 339 | } |
||
| 340 | |||
| 341 | return $context; |
||
| 342 | } |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Adds the group IDs to the context |
||
| 347 | * |
||
| 348 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 349 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 350 | */ |
||
| 351 | protected static function addGroups(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 352 | { |
||
| 353 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_groups']) |
||
| 354 | && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_groups'])) |
||
| 355 | ) { |
||
| 356 | return $fcn($context); |
||
| 357 | } |
||
| 358 | |||
| 359 | $t3context = GeneralUtility::makeInstance('TYPO3\CMS\Core\Context\Context'); |
||
| 360 | |||
| 361 | $appType = null; |
||
| 362 | if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) { |
||
| 363 | $appType = \TYPO3\CMS\Core\Http\ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST']); |
||
| 364 | } |
||
| 365 | |||
| 366 | if ($appType && $appType->isFrontend() && $t3context->getPropertyFromAspect('frontend.user', 'isLoggedIn')) { |
||
| 367 | $ids = GeneralUtility::trimExplode(',', $GLOBALS['TSFE']->fe_user->user['usergroup']); |
||
| 368 | $context->setGroupIds($ids); |
||
| 369 | } elseif ($appType && $appType->isBackend() && $GLOBALS['BE_USER']->userGroups) { |
||
| 370 | $ids = array_keys($GLOBALS['BE_USER']->userGroups); |
||
| 371 | $context->setGroupIds($ids); |
||
| 372 | } |
||
| 373 | |||
| 374 | return $context; |
||
| 375 | } |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Adds the frontend date time to the context |
||
| 380 | * |
||
| 381 | * @param \Aimeos\MShop\ContextIface $context Context object including config |
||
| 382 | * @return \Aimeos\MShop\ContextIface Modified context object |
||
| 383 | */ |
||
| 384 | protected static function addDateTime(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface |
||
| 402 | } |
||
| 403 | } |
||
| 404 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths