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 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | public function addRecipient(JWEInterface &$jwe, JWKInterface $recipient_key, JWKInterface $sender_key = null, array $recipient_headers = []) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $algorithm |
||
| 155 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 156 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 157 | */ |
||
| 158 | private function checkKeys(KeyEncryptionAlgorithmInterface $algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param \Jose\Object\JWEInterface $jwe |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | private function getCurrentKeyManagementMode(JWEInterface $jwe) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param \Jose\Object\JWEInterface $jwe |
||
| 187 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 188 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 189 | * @param array $complete_headers |
||
| 190 | * @param array $recipient_headers |
||
| 191 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 192 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 193 | * |
||
| 194 | * @return \Jose\Object\RecipientInterface |
||
| 195 | */ |
||
| 196 | 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) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $current |
||
| 225 | * @param string $new |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | private function areKeyManagementModesCompatible($current, $new) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $payload |
||
| 264 | * @param array $complete_headers |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | private function preparePayload($payload, array $complete_headers) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param array $complete_headers |
||
| 293 | * @param string $cek |
||
| 294 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 295 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 296 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 297 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 298 | * @param array $additional_headers |
||
| 299 | * |
||
| 300 | * @return string|null |
||
| 301 | */ |
||
| 302 | 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) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $complete_headers |
||
| 317 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 318 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 319 | * @param array $additional_headers |
||
| 320 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 321 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 322 | * |
||
| 323 | * @return mixed |
||
| 324 | */ |
||
| 325 | private function getEncryptedKeyFroKeyAgreementAlgorithm(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $complete_headers |
||
| 337 | * @param string $cek |
||
| 338 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 339 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 340 | * @param array $additional_headers |
||
| 341 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 342 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | 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) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $complete_headers |
||
| 358 | * @param string $cek |
||
| 359 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 360 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | private function getEncryptedKeyFroKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param array $complete_headers |
||
| 375 | * @param string $cek |
||
| 376 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 377 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | private function getEncryptedKeyFroKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param array $complete_headers |
||
| 392 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 393 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 394 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 395 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param array $complete_headers |
||
| 416 | * |
||
| 417 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 418 | */ |
||
| 419 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param array $complete_headers |
||
| 433 | * |
||
| 434 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 435 | */ |
||
| 436 | private function findContentEncryptionAlgorithm(array $complete_headers) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param array $complete_headers |
||
| 452 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
| 453 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 454 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 455 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param int $size |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | private function createCEK($size) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param int $size |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | private function createIV($size) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param int $length |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | private function generateRandomString($length) |
||
| 503 | } |
||
| 504 |