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 | |||
| 40 | /** |
||
| 41 | * Encrypter constructor. |
||
| 42 | * |
||
| 43 | * @param \Jose\Algorithm\JWAManagerInterface $jwa_manager |
||
| 44 | * @param \Jose\Compression\CompressionManagerInterface $compression_manager |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | public function addRecipient(JWEInterface &$jwe, JWKInterface $recipient_key, JWKInterface $sender_key = null, array $recipient_headers = []) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param \Jose\Object\JWEInterface $jwe |
||
| 107 | * @param array $complete_headers |
||
| 108 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 109 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 110 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 111 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 112 | */ |
||
| 113 | private function encryptJWE(JWEInterface &$jwe, |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $algorithm |
||
| 163 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 164 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 165 | */ |
||
| 166 | private function checkKeys(KeyEncryptionAlgorithmInterface $algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param \Jose\Object\JWEInterface $jwe |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | private function getCurrentKeyManagementMode(JWEInterface $jwe) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param \Jose\Object\JWEInterface $jwe |
||
| 195 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 196 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 197 | * @param array $complete_headers |
||
| 198 | * @param array $recipient_headers |
||
| 199 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 200 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 201 | * |
||
| 202 | * @return \Jose\Object\RecipientInterface |
||
| 203 | */ |
||
| 204 | private function computeRecipient(JWEInterface $jwe, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array $complete_headers, array $recipient_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $current |
||
| 233 | * @param string $new |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | private function areKeyManagementModesCompatible($current, $new) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $payload |
||
| 272 | * @param array $complete_headers |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | private function preparePayload($payload, array $complete_headers) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param array $complete_headers |
||
| 301 | * @param string $cek |
||
| 302 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 303 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 304 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 305 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 306 | * @param array $additional_headers |
||
| 307 | * |
||
| 308 | * @return string|null |
||
| 309 | */ |
||
| 310 | private function getEncryptedKey(array $complete_headers, $cek, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array $complete_headers |
||
| 325 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 326 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 327 | * @param array $additional_headers |
||
| 328 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 329 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 330 | * |
||
| 331 | * @return mixed |
||
| 332 | */ |
||
| 333 | private function getEncryptedKeyFroKeyAgreementAlgorithm(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param array $complete_headers |
||
| 345 | * @param string $cek |
||
| 346 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 347 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 348 | * @param array $additional_headers |
||
| 349 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 350 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | private function getEncryptedKeyFroKeyAgreementAndKeyWrappingAlgorithm(array $complete_headers, $cek, KeyAgreementWrappingInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param array $complete_headers |
||
| 366 | * @param string $cek |
||
| 367 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 368 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | private function getEncryptedKeyFroKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param array $complete_headers |
||
| 383 | * @param string $cek |
||
| 384 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 385 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | private function getEncryptedKeyFroKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param array $complete_headers |
||
| 400 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 401 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 402 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 403 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param array $complete_headers |
||
| 426 | * |
||
| 427 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 428 | */ |
||
| 429 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param array $complete_headers |
||
| 443 | * |
||
| 444 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 445 | */ |
||
| 446 | private function findContentEncryptionAlgorithm(array $complete_headers) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param array $complete_headers |
||
| 462 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 463 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 464 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 465 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param int $size |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | private function createCEK($size) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param int $size |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | private function createIV($size) |
||
| 499 | } |
||
| 500 |