| Conditions | 12 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 58 | public function handle(array $options = null): ?ResponseInterface |
||
| 59 | { |
||
| 60 | $referrerType = $this->resolveReferrerType(); |
||
| 61 | // valid referrer, no more actions required |
||
| 62 | if ($referrerType & self::TYPE_REFERRER_SAME_ORIGIN) { |
||
| 63 | return null; |
||
| 64 | } |
||
| 65 | $flags = $options['flags'] ?? []; |
||
| 66 | $expiration = $options['expiration'] ?? 5; |
||
| 67 | // referrer is missing and route requested to refresh |
||
| 68 | // (created HTML refresh to enforce having referrer) |
||
| 69 | if (($this->request->getQueryParams()['referrer-refresh'] ?? 0) <= time() |
||
| 70 | && ( |
||
| 71 | in_array('refresh-always', $flags, true) |
||
| 72 | || ($referrerType & self::TYPE_REFERRER_EMPTY && in_array('refresh-empty', $flags, true)) |
||
| 73 | || ($referrerType & self::TYPE_REFERRER_SAME_SITE && in_array('refresh-same-site', $flags, true)) |
||
| 74 | ) |
||
| 75 | ) { |
||
| 76 | $refreshParameter = 'referrer-refresh=' . (time() + $expiration); |
||
| 77 | $refreshUri = $this->request->getUri(); |
||
| 78 | $query = $refreshUri->getQuery(); |
||
| 79 | $refreshUri = $refreshUri->withQuery( |
||
| 80 | $query !== '' ? $query . '&' . $refreshParameter : $refreshParameter |
||
| 81 | ); |
||
| 82 | $scriptUri = $this->resolveAbsoluteWebPath( |
||
| 83 | 'EXT:core/Resources/Public/JavaScript/ReferrerRefresh.js' |
||
| 84 | ); |
||
| 85 | // simulating navigate event by clicking anchor link |
||
| 86 | // since meta-refresh won't change `document.referrer` in e.g. Firefox |
||
| 87 | return new HtmlResponse(sprintf( |
||
| 88 | '<html>' |
||
| 89 | . '<head><link rel="icon" href="data:image/svg+xml,"></head>' |
||
| 90 | . '<body><a href="%1$s" id="referrer-refresh"> </a>' |
||
| 91 | . '<script src="%2$s"></script></body>' |
||
| 92 | . '</html>', |
||
| 93 | htmlspecialchars((string)$refreshUri), |
||
| 94 | htmlspecialchars($scriptUri) |
||
| 95 | )); |
||
| 96 | } |
||
| 97 | $subject = $options['subject'] ?? ''; |
||
| 98 | if ($referrerType & self::TYPE_REFERRER_EMPTY) { |
||
| 99 | // still empty referrer or invalid referrer, deny route invocation |
||
| 100 | throw new MissingReferrerException( |
||
| 101 | sprintf('Missing referrer%s', $subject !== '' ? ' for ' . $subject : ''), |
||
| 102 | 1588095935 |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | // referrer is given, but does not match current base URL |
||
| 106 | throw new InvalidReferrerException( |
||
| 107 | sprintf('Invalid referrer%s', $subject !== '' ? ' for ' . $subject : ''), |
||
| 108 | 1588095936 |
||
| 109 | ); |
||
| 153 |