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 | use CommonCipheringMethods; |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public static function createEncrypter(array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF', 'ZLIB', 'GZ']) |
||
49 | |||
50 | /** |
||
51 | * Decrypter constructor. |
||
52 | * |
||
53 | * @param string[]|\Jose\Algorithm\KeyEncryptionAlgorithmInterface[] $key_encryption_algorithms |
||
54 | * @param string[]|\Jose\Algorithm\ContentEncryptionAlgorithmInterface[] $content_encryption_algorithms |
||
55 | * @param string[]|\Jose\Compression\CompressionInterface[] $compression_methods |
||
56 | */ |
||
57 | public function __construct( |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function encrypt(JWEInterface &$jwe) |
||
125 | |||
126 | /** |
||
127 | * @param \Jose\Object\JWEInterface $jwe |
||
128 | * @param \Jose\Object\RecipientInterface $recipient |
||
129 | * @param string $cek |
||
130 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
131 | * @param array $additional_headers |
||
132 | */ |
||
133 | private function processRecipient(JWEInterface $jwe, |
||
177 | |||
178 | /** |
||
179 | * @param \Jose\Object\JWEInterface $jwe |
||
180 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
181 | * @param string $key_management_mode |
||
182 | * @param array $additional_headers |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | private function determineCEK(JWEInterface $jwe, |
||
217 | |||
218 | /** |
||
219 | * @param \Jose\Object\JWEInterface $jwe |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | private function getKeyManagementMode(JWEInterface $jwe) |
||
251 | |||
252 | /** |
||
253 | * @param \Jose\Object\JWEInterface $jwe |
||
254 | * |
||
255 | * @return \Jose\Compression\CompressionInterface|null |
||
256 | */ |
||
257 | private function getCompressionMethod(JWEInterface $jwe) |
||
292 | |||
293 | /** |
||
294 | * @param \Jose\Object\JWEInterface $jwe |
||
295 | * |
||
296 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
297 | */ |
||
298 | private function getContentEncryptionAlgorithm(JWEInterface $jwe) |
||
321 | |||
322 | /** |
||
323 | * @param \Jose\Object\JWEInterface $jwe |
||
324 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
325 | * @param string $cek |
||
326 | * @param string $iv |
||
327 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
328 | */ |
||
329 | private function encryptJWE(JWEInterface &$jwe, |
||
330 | ContentEncryptionAlgorithmInterface $content_encryption_algorithm, |
||
331 | $cek, |
||
332 | $iv, |
||
333 | CompressionInterface $compression_method = null |
||
334 | ) { |
||
335 | if (!empty($jwe->getSharedProtectedHeaders())) { |
||
336 | $jwe = $jwe->withEncodedSharedProtectedHeaders(Base64Url::encode(json_encode($jwe->getSharedProtectedHeaders()))); |
||
337 | } |
||
338 | |||
339 | // We encrypt the payload and get the tag |
||
340 | $tag = null; |
||
341 | $payload = $this->preparePayload($jwe->getPayload(), $compression_method); |
||
342 | |||
343 | $ciphertext = $content_encryption_algorithm->encryptContent( |
||
344 | $payload, |
||
345 | $cek, |
||
346 | $iv, |
||
347 | null === $jwe->getAAD() ? null : Base64Url::encode($jwe->getAAD()), |
||
348 | $jwe->getEncodedSharedProtectedHeaders(), |
||
349 | $tag |
||
350 | ); |
||
351 | |||
352 | $jwe = $jwe->withCiphertext($ciphertext); |
||
353 | $jwe = $jwe->withIV($iv); |
||
354 | |||
355 | // Tag |
||
356 | if (null !== $tag) { |
||
357 | $jwe = $jwe->withTag($tag); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
363 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
364 | * @param \Jose\Object\JWKInterface $recipient_key |
||
365 | */ |
||
366 | private function checkKeys(KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key) |
||
375 | |||
376 | /** |
||
377 | * @param string $current |
||
378 | * @param string $new |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | private function areKeyManagementModesCompatible($current, $new) |
||
414 | |||
415 | /** |
||
416 | * @param string $payload |
||
417 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | private function preparePayload($payload, CompressionInterface $compression_method = null) |
||
435 | |||
436 | /** |
||
437 | * @param array $complete_headers |
||
438 | * @param string $cek |
||
439 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
440 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
441 | * @param \Jose\Object\JWKInterface $recipient_key |
||
442 | * @param array $additional_headers |
||
443 | * |
||
444 | * @return string|null |
||
445 | */ |
||
446 | private function getEncryptedKey(array $complete_headers, $cek, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key) |
||
458 | |||
459 | /** |
||
460 | * @param array $complete_headers |
||
461 | * @param string $cek |
||
462 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
463 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
464 | * @param array $additional_headers |
||
465 | * @param \Jose\Object\JWKInterface $recipient_key |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $complete_headers, $cek, KeyAgreementWrappingInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key) |
||
475 | |||
476 | /** |
||
477 | * @param array $complete_headers |
||
478 | * @param string $cek |
||
479 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
480 | * @param \Jose\Object\JWKInterface $recipient_key |
||
481 | * @param array $additional_headers |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key, array &$additional_headers) |
||
494 | |||
495 | /** |
||
496 | * @param array $complete_headers |
||
497 | * @param string $cek |
||
498 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
499 | * @param \Jose\Object\JWKInterface $recipient_key |
||
500 | * @param array $additional_headers |
||
501 | * |
||
502 | * @return string |
||
503 | */ |
||
504 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key, &$additional_headers) |
||
513 | |||
514 | /** |
||
515 | * @param array $complete_headers |
||
516 | * |
||
517 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
518 | */ |
||
519 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
528 | |||
529 | /** |
||
530 | * @param int $size |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | private function createCEK($size) |
||
538 | |||
539 | /** |
||
540 | * @param int $size |
||
541 | * |
||
542 | * @return string |
||
543 | */ |
||
544 | private function createIV($size) |
||
548 | } |
||
549 |