| Total Complexity | 42 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LanguageMiddleware 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 LanguageMiddleware, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class LanguageMiddleware |
||
| 21 | { |
||
| 22 | use TranslatorAwareTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $defaultLanguage; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $browserLanguage; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $excludedPath; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | private $usePath; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $pathRegexp; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | private $useBrowser; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var boolean |
||
| 56 | */ |
||
| 57 | private $useSession; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string[] |
||
| 61 | */ |
||
| 62 | private $sessionKey; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | private $useParams; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string[] |
||
| 71 | */ |
||
| 72 | private $paramKey; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var boolean |
||
| 76 | */ |
||
| 77 | private $useHost; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $hostMap; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean |
||
| 86 | */ |
||
| 87 | private $setLocale; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param array $data The middleware options. |
||
| 91 | */ |
||
| 92 | public function __construct(array $data) |
||
| 93 | { |
||
| 94 | $this->setTranslator($data['translator']); |
||
| 95 | |||
| 96 | $data = array_replace($this->defaults(), $data); |
||
| 97 | |||
| 98 | $this->defaultLanguage = $data['default_language']; |
||
| 99 | $this->browserLanguage = $data['browser_language']; |
||
| 100 | |||
| 101 | $this->usePath = !!$data['use_path']; |
||
| 102 | $this->excludedPath = (array)$data['excluded_path']; |
||
| 103 | $this->pathRegexp = $data['path_regexp']; |
||
| 104 | |||
| 105 | $this->useParams = !!$data['use_params']; |
||
| 106 | $this->paramKey = (array)$data['param_key']; |
||
| 107 | |||
| 108 | $this->useSession = !!$data['use_session']; |
||
| 109 | $this->sessionKey = (array)$data['session_key']; |
||
| 110 | |||
| 111 | $this->useBrowser = !!$data['use_browser']; |
||
| 112 | |||
| 113 | $this->useHost = !!$data['use_host']; |
||
| 114 | $this->hostMap = (array)$data['host_map']; |
||
| 115 | |||
| 116 | $this->setLocale = !!$data['set_locale']; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Default middleware options. |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function defaults() |
||
| 125 | { |
||
| 126 | return [ |
||
| 127 | 'default_language' => null, |
||
| 128 | 'browser_language' => null, |
||
| 129 | |||
| 130 | 'use_path' => true, |
||
| 131 | 'excluded_path' => [ '^/admin\b' ], |
||
| 132 | 'path_regexp' => '^/([a-z]{2})\b', |
||
| 133 | |||
| 134 | 'use_params' => false, |
||
| 135 | 'param_key' => 'current_language', |
||
| 136 | |||
| 137 | 'use_session' => true, |
||
| 138 | 'session_key' => 'current_language', |
||
| 139 | |||
| 140 | 'use_browser' => true, |
||
| 141 | |||
| 142 | 'use_host' => false, |
||
| 143 | 'host_map' => [], |
||
| 144 | |||
| 145 | 'set_locale' => true |
||
| 146 | ]; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
| 151 | * @param ResponseInterface $response The PSR-7 HTTP response. |
||
| 152 | * @param callable $next The next middleware callable in the stack. |
||
| 153 | * @return ResponseInterface |
||
| 154 | */ |
||
| 155 | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
||
| 156 | { |
||
| 157 | // Test if path is excluded from middleware. |
||
| 158 | $uri = $request->getUri(); |
||
| 159 | $path = $uri->getPath(); |
||
| 160 | foreach ($this->excludedPath as $excluded) { |
||
| 161 | if (preg_match('@'.$excluded.'@', $path)) { |
||
| 162 | return $next($request, $response); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $language = $this->getLanguage($request); |
||
| 167 | $this->setLanguage($language); |
||
| 168 | |||
| 169 | return $next($request, $response); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
| 174 | * @return null|string |
||
| 175 | */ |
||
| 176 | private function getLanguage(RequestInterface $request) |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | private function getLanguageFromHost(RequestInterface $request) |
||
| 221 | { |
||
| 222 | $uriHost = $request->getUri()->getHost(); |
||
| 223 | foreach ($this->hostMap as $lang => $host) { |
||
| 224 | if (stripos($uriHost, $host) !== false) { |
||
| 225 | return $lang; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | return ''; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | private function getLanguageFromPath(RequestInterface $request) |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | private function getLanguageFromParams(RequestInterface $request) |
||
| 257 | { |
||
| 258 | if ($request instanceof ServerRequestInterface) { |
||
| 259 | $locales = $this->translator()->availableLocales(); |
||
| 260 | $params = $request->getQueryParams(); |
||
| 261 | foreach ($this->paramKey as $key) { |
||
| 262 | if (isset($params[$key]) && in_array($params[$key], $locales)) { |
||
| 263 | return $params[$key]; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | return ''; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | private function getLanguageFromSession() |
||
| 275 | { |
||
| 276 | $locales = $this->translator()->availableLocales(); |
||
| 277 | foreach ($this->sessionKey as $key) { |
||
| 278 | if (isset($_SESSION[$key]) && in_array($_SESSION[$key], $locales)) { |
||
| 279 | return $_SESSION[$key]; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return ''; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | private function getLanguageFromBrowser() |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $lang The language code to set. |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | private function setLanguage($lang) |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $lang The language code to set. |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | private function setLocale($lang) |
||
| 346 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.