Complex classes like Encrypter 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Encrypter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class Encrypter implements EncrypterInterface |
||
| 35 | { |
||
| 36 | use HasKeyChecker; |
||
| 37 | use HasJWAManager; |
||
| 38 | use HasCompressionManager; |
||
| 39 | use HasLogger; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Encrypter constructor. |
||
| 43 | * |
||
| 44 | * @param \Jose\Algorithm\JWAManagerInterface $jwa_manager |
||
| 45 | * @param \Jose\Compression\CompressionManagerInterface $compression_manager |
||
| 46 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 47 | */ |
||
| 48 | public function __construct( |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | public function encrypt(JWEInterface &$jwe) |
||
| 65 | { |
||
| 66 | Assertion::greaterThan($jwe->countRecipients(), 0, 'The JWE does not contain recipient.'); |
||
| 67 | |||
| 68 | // Content Encryption Algorithm |
||
| 69 | $content_encryption_algorithm = $this->getContentEncryptionAlgorithm($jwe); |
||
| 70 | |||
| 71 | // Compression Method |
||
| 72 | $compression_method = $this->getCompressionMethod($jwe); |
||
| 73 | |||
| 74 | // Key Management Mode |
||
| 75 | $key_management_mode = $this->getKeyManagementMode($jwe); |
||
| 76 | |||
| 77 | // Additional Headers |
||
| 78 | $additional_headers = []; |
||
| 79 | |||
| 80 | // CEK |
||
| 81 | $cek = $this->determineCEK( |
||
| 82 | $jwe, |
||
| 83 | $content_encryption_algorithm, |
||
|
|
|||
| 84 | $key_management_mode, |
||
| 85 | $additional_headers |
||
| 86 | ); |
||
| 87 | |||
| 88 | for ($i = 0; $i < $jwe->countRecipients(); $i++) { |
||
| 89 | $this->processRecipient( |
||
| 90 | $jwe, |
||
| 91 | $jwe->getRecipient($i), |
||
| 92 | $cek, |
||
| 93 | $content_encryption_algorithm, |
||
| 94 | $additional_headers |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!empty($additional_headers) && 1 === $jwe->countRecipients()) { |
||
| 99 | $jwe = $jwe->withSharedProtectedHeaders(array_merge( |
||
| 100 | $jwe->getSharedProtectedHeaders(), |
||
| 101 | $additional_headers |
||
| 102 | )); |
||
| 103 | } |
||
| 104 | |||
| 105 | // IV |
||
| 106 | if (null !== $iv_size = $content_encryption_algorithm->getIVSize()) { |
||
| 107 | $iv = $this->createIV($iv_size); |
||
| 108 | $jwe = $jwe->withIV($iv); |
||
| 109 | } |
||
| 110 | |||
| 111 | $jwe = $jwe->withContentEncryptionKey($cek); |
||
| 112 | |||
| 113 | $this->encryptJWE($jwe, $content_encryption_algorithm, $compression_method); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param \Jose\Object\JWEInterface $jwe |
||
| 118 | * @param \Jose\Object\RecipientInterface $recipient |
||
| 119 | * @param string $cek |
||
| 120 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 121 | * @param array $additional_headers |
||
| 122 | */ |
||
| 123 | private function processRecipient(JWEInterface $jwe, |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param \Jose\Object\JWEInterface $jwe |
||
| 165 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 166 | * @param string $key_management_mode |
||
| 167 | * @param array $additional_headers |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | private function determineCEK(JWEInterface $jwe, |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param \Jose\Object\JWEInterface $jwe |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | private function getKeyManagementMode(JWEInterface $jwe) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param \Jose\Object\JWEInterface $jwe |
||
| 238 | * |
||
| 239 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 240 | */ |
||
| 241 | private function getCompressionMethod(JWEInterface $jwe) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param \Jose\Object\JWEInterface $jwe |
||
| 278 | * |
||
| 279 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 280 | */ |
||
| 281 | private function getContentEncryptionAlgorithm(JWEInterface $jwe) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param \Jose\Object\JWEInterface $jwe |
||
| 307 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 308 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
| 309 | */ |
||
| 310 | private function encryptJWE(JWEInterface &$jwe, |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 340 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 341 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 342 | */ |
||
| 343 | private function checkKeys(KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $current |
||
| 355 | * @param string $new |
||
| 356 | * |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | private function areKeyManagementModesCompatible($current, $new) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $payload |
||
| 394 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | private function preparePayload($payload, CompressionInterface $compression_method = null) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param array $complete_headers |
||
| 415 | * @param string $cek |
||
| 416 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 417 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 418 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 419 | * @param array $additional_headers |
||
| 420 | * |
||
| 421 | * @return string|null |
||
| 422 | */ |
||
| 423 | private function getEncryptedKey(array $complete_headers, $cek, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param array $complete_headers |
||
| 438 | * @param string $cek |
||
| 439 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 440 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 441 | * @param array $additional_headers |
||
| 442 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $complete_headers, $cek, KeyAgreementWrappingInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param array $complete_headers |
||
| 455 | * @param string $cek |
||
| 456 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 457 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 458 | * @param array $additional_headers |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key, array &$additional_headers) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param array $complete_headers |
||
| 474 | * @param string $cek |
||
| 475 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 476 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 477 | * @param array $additional_headers |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key, &$additional_headers) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param array $complete_headers |
||
| 493 | * |
||
| 494 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 495 | */ |
||
| 496 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param int $size |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | private function createCEK($size) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param int $size |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | private function createIV($size) |
||
| 525 | } |
||
| 526 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: