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, |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 162 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 163 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 164 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 165 | */ |
||
| 166 | private function checkKeys(KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param \Jose\Object\JWEInterface $jwe |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | private function getCurrentKeyManagementMode(JWEInterface $jwe) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param \Jose\Object\JWEInterface $jwe |
||
| 203 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 204 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 205 | * @param array $complete_headers |
||
| 206 | * @param array $recipient_headers |
||
| 207 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 208 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 209 | * |
||
| 210 | * @return \Jose\Object\RecipientInterface |
||
| 211 | */ |
||
| 212 | 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) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $current |
||
| 241 | * @param string $new |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | private function areKeyManagementModesCompatible($current, $new) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $payload |
||
| 280 | * @param array $complete_headers |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | private function preparePayload($payload, array $complete_headers) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $complete_headers |
||
| 309 | * @param string $cek |
||
| 310 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 311 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 312 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 313 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 314 | * @param array $additional_headers |
||
| 315 | * |
||
| 316 | * @return string|null |
||
| 317 | */ |
||
| 318 | 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) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $complete_headers |
||
| 333 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 334 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 335 | * @param array $additional_headers |
||
| 336 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 337 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 338 | * |
||
| 339 | * @return mixed |
||
| 340 | */ |
||
| 341 | private function getEncryptedKeyFroKeyAgreementAlgorithm(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param array $complete_headers |
||
| 353 | * @param string $cek |
||
| 354 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 355 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 356 | * @param array $additional_headers |
||
| 357 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 358 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | 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) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $complete_headers |
||
| 374 | * @param string $cek |
||
| 375 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 376 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function getEncryptedKeyFroKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $complete_headers |
||
| 391 | * @param string $cek |
||
| 392 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 393 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | private function getEncryptedKeyFroKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param array $complete_headers |
||
| 408 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 409 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 410 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 411 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param array $complete_headers |
||
| 434 | * |
||
| 435 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 436 | */ |
||
| 437 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param array $complete_headers |
||
| 451 | * |
||
| 452 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 453 | */ |
||
| 454 | private function findContentEncryptionAlgorithm(array $complete_headers) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param array $complete_headers |
||
| 470 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 471 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 472 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 473 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param int $size |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | private function createCEK($size) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param int $size |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | private function createIV($size) |
||
| 507 | } |
||
| 508 |