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 |
||
| 36 | final class Encrypter implements EncrypterInterface |
||
| 37 | { |
||
| 38 | use HasKeyChecker; |
||
| 39 | use HasJWAManager; |
||
| 40 | use HasCompressionManager; |
||
| 41 | use HasLogger; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Encrypter constructor. |
||
| 45 | * |
||
| 46 | * @param \Jose\Algorithm\JWAManagerInterface $jwa_manager |
||
| 47 | * @param \Jose\Compression\CompressionManagerInterface $compression_manager |
||
| 48 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 49 | */ |
||
| 50 | public function __construct( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | public function addRecipient(JWEInterface &$jwe, JWKInterface $recipient_key, JWKInterface $sender_key = null, array $recipient_headers = []) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param \Jose\Object\JWEInterface $jwe |
||
| 116 | * @param array $complete_headers |
||
| 117 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 118 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 119 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 120 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 121 | */ |
||
| 122 | private function encryptJWE(JWEInterface &$jwe, |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 171 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 172 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 173 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 174 | */ |
||
| 175 | private function checkKeys(KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param \Jose\Object\JWEInterface $jwe |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | private function getCurrentKeyManagementMode(JWEInterface $jwe) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param \Jose\Object\JWEInterface $jwe |
||
| 212 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 213 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 214 | * @param array $complete_headers |
||
| 215 | * @param array $recipient_headers |
||
| 216 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 217 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 218 | * |
||
| 219 | * @return \Jose\Object\RecipientInterface |
||
| 220 | */ |
||
| 221 | 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) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $current |
||
| 250 | * @param string $new |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | private function areKeyManagementModesCompatible($current, $new) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $payload |
||
| 289 | * @param array $complete_headers |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | private function preparePayload($payload, array $complete_headers) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param array $complete_headers |
||
| 318 | * @param string $cek |
||
| 319 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 320 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 321 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 322 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 323 | * @param array $additional_headers |
||
| 324 | * |
||
| 325 | * @return string|null |
||
| 326 | */ |
||
| 327 | 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) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param array $complete_headers |
||
| 342 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 343 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 344 | * @param array $additional_headers |
||
| 345 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 346 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 347 | * |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | private function getEncryptedKeyFromKeyAgreementAlgorithm(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param array $complete_headers |
||
| 369 | * @param string $cek |
||
| 370 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 371 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 372 | * @param array $additional_headers |
||
| 373 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 374 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $complete_headers, $cek, KeyAgreementWrappingInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param array $complete_headers |
||
| 390 | * @param string $cek |
||
| 391 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 392 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 393 | * @param array $additional_headers |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key, array &$additional_headers) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $complete_headers |
||
| 409 | * @param string $cek |
||
| 410 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 411 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param array $complete_headers |
||
| 426 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 427 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 428 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 429 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param array $complete_headers |
||
| 452 | * |
||
| 453 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 454 | */ |
||
| 455 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param array $complete_headers |
||
| 469 | * |
||
| 470 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 471 | */ |
||
| 472 | private function findContentEncryptionAlgorithm(array $complete_headers) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param array $complete_headers |
||
| 488 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 489 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 490 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 491 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param int $size |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | private function createCEK($size) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param int $size |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | private function createIV($size) |
||
| 531 | } |
||
| 532 |