| Conditions | 6 |
| Paths | 7 |
| Total Lines | 52 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 43 | public function decode(string $token): TokenInterface |
||
| 44 | { |
||
| 45 | $token = explode('.', $token, 4); |
||
| 46 | |||
| 47 | if (count($token) !== 3) { |
||
| 48 | throw new DecoderException("Failed token decoding, the token encoded format is invalid."); |
||
| 49 | } |
||
| 50 | |||
| 51 | // Collect parts of the token. |
||
| 52 | $iv = $token[0]; |
||
| 53 | $signature = $token[2]; |
||
| 54 | $token = $token[1]; |
||
| 55 | |||
| 56 | // base64 decode the token parts. |
||
| 57 | $iv = $this->base62Decode($iv); |
||
| 58 | $signature = $this->base62Decode($signature); |
||
| 59 | $token = $this->base62Decode($token); |
||
| 60 | |||
| 61 | try { |
||
| 62 | // Create the expected (valid) signature of the token payload data. |
||
| 63 | $expectedSignature = $this->signer->createSignature($token); |
||
| 64 | } catch (SignerExceptionInterface $e) { |
||
| 65 | throw new DecoderException("Failed token decoding, could not create trusted token signature.", 0, $e); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Ensure that signatures match. |
||
| 69 | if ($signature !== $expectedSignature) { |
||
| 70 | throw new DecoderException("Failed token decoding, the token signature is invalid."); |
||
| 71 | } |
||
| 72 | |||
| 73 | try { |
||
| 74 | // Load token encryption config. |
||
| 75 | $encryptionMethod = $this->config->get('redirect.token_encryption_method'); |
||
| 76 | $encryptionKey = $this->config->get('redirect.token_encryption_key'); |
||
| 77 | } catch (ConfigExceptionInterface $e) { |
||
| 78 | throw new DecoderException("Failed token decoding, the token encryption config is incomplete.", 0, $e); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Get binary SHA-256 hash of cleartext encryption key. |
||
| 82 | $encryptionKey = hash('sha256', $encryptionKey, true); |
||
| 83 | |||
| 84 | // Decrypt the token payload data. |
||
| 85 | $token = $this->decrypt($token, $encryptionMethod, $encryptionKey, $iv); |
||
| 86 | |||
| 87 | // JSON decode the token payload. |
||
| 88 | $token = json_decode($token, true); |
||
| 89 | |||
| 90 | if (!is_array($token)) { |
||
| 91 | throw new DecoderException("Failed token decoding, the token payload data is invalid."); |
||
| 92 | } |
||
| 93 | |||
| 94 | return new Token($token); |
||
| 95 | } |
||
| 142 |