Complex classes like JWEBuilder 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 JWEBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class JWEBuilder |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var JsonConverterInterface |
||
| 38 | */ |
||
| 39 | private $jsonConverter; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $payload; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|null |
||
| 48 | */ |
||
| 49 | private $aad; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $recipients = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var AlgorithmManager |
||
| 58 | */ |
||
| 59 | private $keyEncryptionAlgorithmManager; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var AlgorithmManager |
||
| 63 | */ |
||
| 64 | private $contentEncryptionAlgorithmManager; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var CompressionMethodManager |
||
| 68 | */ |
||
| 69 | private $compressionManager; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $sharedProtectedHeaders = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private $sharedHeaders = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var null|CompressionMethodInterface |
||
| 83 | */ |
||
| 84 | private $compressionMethod = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var null|ContentEncryptionAlgorithmInterface |
||
| 88 | */ |
||
| 89 | private $contentEncryptionAlgorithm = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var null|string |
||
| 93 | */ |
||
| 94 | private $keyManagementMode = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * JWEBuilder constructor. |
||
| 98 | * |
||
| 99 | * @param JsonConverterInterface $jsonConverter |
||
| 100 | * @param AlgorithmManager $keyEncryptionAlgorithmManager |
||
| 101 | * @param AlgorithmManager $contentEncryptionAlgorithmManager |
||
| 102 | * @param CompressionMethodManager $compressionManager |
||
| 103 | */ |
||
| 104 | public function __construct(JsonConverterInterface $jsonConverter, AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Reset the current data. |
||
| 114 | * |
||
| 115 | * @return JWEBuilder |
||
| 116 | */ |
||
| 117 | public function create(): JWEBuilder |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return AlgorithmManager |
||
| 133 | */ |
||
| 134 | public function getKeyEncryptionAlgorithmManager(): AlgorithmManager |
||
| 135 | { |
||
| 136 | return $this->keyEncryptionAlgorithmManager; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return AlgorithmManager |
||
| 141 | */ |
||
| 142 | public function getContentEncryptionAlgorithmManager(): AlgorithmManager |
||
| 143 | { |
||
| 144 | return $this->contentEncryptionAlgorithmManager; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return CompressionMethodManager |
||
| 149 | */ |
||
| 150 | public function getCompressionManager(): CompressionMethodManager |
||
| 151 | { |
||
| 152 | return $this->compressionManager; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param mixed $payload |
||
| 157 | * |
||
| 158 | * @return JWEBuilder |
||
| 159 | */ |
||
| 160 | public function withPayload($payload): JWEBuilder |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string|null $aad |
||
| 174 | * |
||
| 175 | * @return JWEBuilder |
||
| 176 | */ |
||
| 177 | public function withAAD(?string $aad): JWEBuilder |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param array $sharedProtectedHeaders |
||
| 187 | * |
||
| 188 | * @return JWEBuilder |
||
| 189 | */ |
||
| 190 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param array $sharedHeaders |
||
| 204 | * |
||
| 205 | * @return JWEBuilder |
||
| 206 | */ |
||
| 207 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param JWK $recipientKey |
||
| 221 | * @param array $recipientHeaders |
||
| 222 | * |
||
| 223 | * @return JWEBuilder |
||
| 224 | */ |
||
| 225 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return JWE |
||
| 264 | */ |
||
| 265 | public function build(): JWE |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param array $completeHeaders |
||
| 294 | */ |
||
| 295 | protected function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $recipient |
||
| 307 | * @param string $cek |
||
| 308 | * @param array $additionalHeaders |
||
| 309 | * |
||
| 310 | * @return Recipient |
||
| 311 | */ |
||
| 312 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $cek |
||
| 329 | * @param string $encodedSharedProtectedHeaders |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | private function preparePayload(): ?string |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param array $completeHeaders |
||
| 365 | * @param string $cek |
||
| 366 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 367 | * @param JWK $recipientKey |
||
| 368 | * @param array $additionalHeaders |
||
| 369 | * |
||
| 370 | * @return string|null |
||
| 371 | */ |
||
| 372 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $completeHeaders |
||
| 391 | * @param string $cek |
||
| 392 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
| 393 | * @param array $additionalHeaders |
||
| 394 | * @param JWK $recipientKey |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param array $completeHeaders |
||
| 405 | * @param string $cek |
||
| 406 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
| 407 | * @param JWK $recipientKey |
||
| 408 | * @param array $additionalHeaders |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param array $completeHeaders |
||
| 419 | * @param string $cek |
||
| 420 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
| 421 | * @param JWK $recipientKey |
||
| 422 | * @param array $additionalHeaders |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 433 | * @param JWK $recipientKey |
||
| 434 | */ |
||
| 435 | protected function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param array $additionalHeaders |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | private function determineCEK(array &$additionalHeaders): string |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param array $completeHeaders |
||
| 485 | * |
||
| 486 | * @return CompressionMethodInterface|null |
||
| 487 | */ |
||
| 488 | protected function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param string $current |
||
| 499 | * @param string $new |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | protected function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param int $size |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | private function createCEK(int $size): string |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param int $size |
||
| 530 | * |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | private function createIV(int $size): string |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param array $completeHeaders |
||
| 540 | * |
||
| 541 | * @return KeyEncryptionAlgorithmInterface |
||
| 542 | */ |
||
| 543 | protected function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param array $completeHeaders |
||
| 558 | * |
||
| 559 | * @return ContentEncryptionAlgorithmInterface |
||
| 560 | */ |
||
| 561 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param array $header1 |
||
| 576 | * @param array $header2 |
||
| 577 | */ |
||
| 578 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
| 585 | } |
||
| 586 |