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 |
||
| 33 | final class Encrypter implements EncrypterInterface |
||
| 34 | { |
||
| 35 | use HasKeyChecker; |
||
| 36 | use HasJWAManager; |
||
| 37 | use HasCompressionManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Encrypter constructor. |
||
| 41 | * |
||
| 42 | * @param \Jose\Algorithm\JWAManagerInterface $jwa_manager |
||
| 43 | * @param \Jose\Compression\CompressionManagerInterface $compression_manager |
||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param \Jose\Object\JWEInterface $jwe |
||
| 55 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 56 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 57 | * @param array $recipient_headers |
||
| 58 | * |
||
| 59 | * @return \Jose\Object\JWEInterface |
||
| 60 | */ |
||
| 61 | public function addRecipient(JWEInterface $jwe, JWKInterface $recipient_key, JWKInterface $sender_key = null, array $recipient_headers = []) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param \Jose\Object\JWEInterface $jwe |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | private function getCurrentKeyManagementMode(JWEInterface $jwe) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param \Jose\Object\JWEInterface $jwe |
||
| 182 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 183 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 184 | * @param array $complete_headers |
||
| 185 | * @param array $recipient_headers |
||
| 186 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 187 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 188 | * |
||
| 189 | * @return \Jose\Object\RecipientInterface |
||
| 190 | */ |
||
| 191 | 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) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $current |
||
| 220 | * @param string $new |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | private function areKeyManagementModesCompatible($current, $new) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $payload |
||
| 259 | * @param array $complete_headers |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | private function preparePayload($payload, array $complete_headers) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param array $complete_headers |
||
| 288 | * @param string $cek |
||
| 289 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 290 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 291 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 292 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 293 | * @param array $additional_headers |
||
| 294 | * |
||
| 295 | * @return string|null |
||
| 296 | */ |
||
| 297 | 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) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param array $complete_headers |
||
| 316 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 317 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 318 | * @param array $additional_headers |
||
| 319 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 320 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 321 | * |
||
| 322 | * @return mixed |
||
| 323 | */ |
||
| 324 | private function getEncryptedKeyFroKeyAgreementAlgorithm(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param array $complete_headers |
||
| 336 | * @param string $cek |
||
| 337 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 338 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 339 | * @param array $additional_headers |
||
| 340 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 341 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | 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) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $complete_headers |
||
| 357 | * @param string $cek |
||
| 358 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 359 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | private function getEncryptedKeyFroKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $complete_headers |
||
| 374 | * @param string $cek |
||
| 375 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 376 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function getEncryptedKeyFroKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $complete_headers |
||
| 391 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 392 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 393 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 394 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param array $complete_headers |
||
| 415 | * |
||
| 416 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 417 | */ |
||
| 418 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param array $complete_headers |
||
| 432 | * |
||
| 433 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 434 | */ |
||
| 435 | private function findContentEncryptionAlgorithm(array $complete_headers) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param array $complete_headers |
||
| 451 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 452 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 453 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 454 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param int $size |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | private function createCEK($size) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param int $size |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | private function createIV($size) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param int $length |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | private function generateRandomString($length) |
||
| 502 | } |
||
| 503 |