| Conditions | 23 |
| Paths | 544 |
| Total Lines | 94 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 169 | public function onKernelRequest(Event\GetResponseEvent $event) |
||
| 170 | { |
||
| 171 | if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | |||
| 175 | $request = $event->getRequest(); |
||
| 176 | $publicUrl = rawurldecode($request->getRequestUri()); |
||
| 177 | |||
| 178 | if ($this->isExcluded($publicUrl)) { |
||
| 179 | // don't process urls which are marked as excluded. |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | $queryString = ''; |
||
| 184 | if (false !== ($queryMark = strpos($publicUrl, '?'))) { |
||
| 185 | $queryString = substr($publicUrl, $queryMark); |
||
| 186 | $publicUrl = substr($publicUrl, 0, $queryMark); |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($this->isParamsEnabled) { |
||
| 190 | $parts = explode('/', $publicUrl); |
||
| 191 | $params = array(); |
||
| 192 | while (false !== strpos(end($parts), '=')) { |
||
| 193 | array_push($params, array_pop($parts)); |
||
| 194 | } |
||
| 195 | if ($params) { |
||
| 196 | $publicUrl = join('/', $parts); |
||
| 197 | |||
| 198 | $parser = new UriParser(); |
||
| 199 | $request->query->add($parser->parseUri(join('/', array_reverse($params)))); |
||
| 200 | |||
| 201 | if (!$this->aliasing->hasInternalAlias($publicUrl, false)) { |
||
| 202 | $this->rewriteRequest($event, $publicUrl . $queryString); |
||
| 203 | |||
| 204 | return; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | $tryPublicUrls = [$publicUrl => null]; |
||
| 210 | if ($queryString !== '') { |
||
| 211 | $tryPublicUrls[$publicUrl . $queryString] = null; |
||
| 212 | } |
||
| 213 | if ($this->slashSuffixHandling !== static::SLASH_SUFFIX_IGNORE && substr($publicUrl, -1) === '/') { |
||
| 214 | $tryPublicUrls[rtrim($publicUrl, '/')] = $this->slashSuffixHandling; |
||
| 215 | if ($queryString !== '') { |
||
| 216 | $tryPublicUrls[rtrim($publicUrl, '/') . $queryString] = $this->slashSuffixHandling; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | $qb = $this->aliasing->getRepository()->createQueryBuilder('u'); |
||
| 221 | $qb->where($qb->expr()->in('u.public_url', array_keys($tryPublicUrls))) |
||
| 222 | ->indexBy('u', 'u.public_url'); |
||
| 223 | /** @var UrlAlias[] $urlAliases */ |
||
| 224 | $urlAliases = $qb->getQuery()->getResult(); |
||
| 225 | |||
| 226 | if (count($urlAliases) === 0) { |
||
| 227 | return; |
||
| 228 | } |
||
| 229 | |||
| 230 | foreach ($tryPublicUrls as $tryPublicUrl => $handlingMode) { |
||
| 231 | if (!array_key_exists($tryPublicUrl, $urlAliases)) { |
||
| 232 | continue; |
||
| 233 | } |
||
| 234 | |||
| 235 | $urlAlias = $urlAliases[$tryPublicUrl]; |
||
| 236 | |||
| 237 | switch ($handlingMode) { |
||
| 238 | case static::SLASH_SUFFIX_REDIRECT_TEMP: |
||
| 239 | $url = $urlAlias->getMode() === UrlAlias::ALIAS ? $urlAlias->getInternalUrl() : $urlAlias->getPublicUrl(); // Same mode? Use internal URL directly, don't go redirecting twice... |
||
| 240 | $event->setResponse(new RedirectResponse($url, Response::HTTP_FOUND)); |
||
| 241 | break; |
||
| 242 | |||
| 243 | case static::SLASH_SUFFIX_REDIRECT_PERM: |
||
| 244 | $url = $urlAlias->getMode() === UrlAlias::MOVE ? $urlAlias->getInternalUrl() : $urlAlias->getPublicUrl(); // Same mode? Use internal URL directly, don't go redirecting twice... |
||
| 245 | $event->setResponse(new RedirectResponse($url, Response::HTTP_MOVED_PERMANENTLY)); |
||
| 246 | break; |
||
| 247 | |||
| 248 | case static::SLASH_SUFFIX_ACCEPT: |
||
| 249 | // Continue as if the correct URL (without '/' suffix) was requested. Could result in duplicate content disqualifications |
||
| 250 | default: // $handlingMode === null |
||
| 251 | switch ($urlAlias->getMode()) { |
||
| 252 | case UrlAlias::REWRITE: |
||
| 253 | $this->rewriteRequest($event, $urlAlias->getInternalUrl()); |
||
| 254 | break; |
||
| 255 | case UrlAlias::MOVE: |
||
| 256 | case UrlAlias::ALIAS: |
||
| 257 | $event->setResponse(new RedirectResponse($urlAlias->getInternalUrl(), $urlAlias->getMode())); |
||
| 258 | break; |
||
| 259 | default: |
||
| 260 | throw new \UnexpectedValueException(sprintf("Invalid mode %s for UrlAlias %s.", $urlAlias->getMode(), json_encode($urlAlias))); |
||
| 261 | } |
||
| 262 | break; |
||
| 263 | } |
||
| 330 |