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 = []) |
||
159 | |||
160 | /** |
||
161 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $algorithm |
||
162 | * @param \Jose\Object\JWKInterface $recipient_key |
||
163 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
164 | */ |
||
165 | private function checkKeys(KeyEncryptionAlgorithmInterface $algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
174 | |||
175 | /** |
||
176 | * @param \Jose\Object\JWEInterface $jwe |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | private function getCurrentKeyManagementMode(JWEInterface $jwe) |
||
191 | |||
192 | /** |
||
193 | * @param \Jose\Object\JWEInterface $jwe |
||
194 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
195 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
196 | * @param array $complete_headers |
||
197 | * @param array $recipient_headers |
||
198 | * @param \Jose\Object\JWKInterface $recipient_key |
||
199 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
200 | * |
||
201 | * @return \Jose\Object\RecipientInterface |
||
202 | */ |
||
203 | 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) |
||
229 | |||
230 | /** |
||
231 | * @param string $current |
||
232 | * @param string $new |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | private function areKeyManagementModesCompatible($current, $new) |
||
268 | |||
269 | /** |
||
270 | * @param string $payload |
||
271 | * @param array $complete_headers |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | private function preparePayload($payload, array $complete_headers) |
||
297 | |||
298 | /** |
||
299 | * @param array $complete_headers |
||
300 | * @param string $cek |
||
301 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
302 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
303 | * @param \Jose\Object\JWKInterface $recipient_key |
||
304 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
305 | * @param array $additional_headers |
||
306 | * |
||
307 | * @return string|null |
||
308 | */ |
||
309 | 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) |
||
325 | |||
326 | /** |
||
327 | * @param array $complete_headers |
||
328 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
329 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
330 | * @param array $additional_headers |
||
331 | * @param \Jose\Object\JWKInterface $recipient_key |
||
332 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
333 | * |
||
334 | * @return mixed |
||
335 | */ |
||
336 | private function getEncryptedKeyFroKeyAgreementAlgorithm(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
345 | |||
346 | /** |
||
347 | * @param array $complete_headers |
||
348 | * @param string $cek |
||
349 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
350 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
351 | * @param array $additional_headers |
||
352 | * @param \Jose\Object\JWKInterface $recipient_key |
||
353 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | 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) |
||
366 | |||
367 | /** |
||
368 | * @param array $complete_headers |
||
369 | * @param string $cek |
||
370 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
371 | * @param \Jose\Object\JWKInterface $recipient_key |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | private function getEncryptedKeyFroKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
383 | |||
384 | /** |
||
385 | * @param array $complete_headers |
||
386 | * @param string $cek |
||
387 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
388 | * @param \Jose\Object\JWKInterface $recipient_key |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | private function getEncryptedKeyFroKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key) |
||
400 | |||
401 | /** |
||
402 | * @param array $complete_headers |
||
403 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
404 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
405 | * @param \Jose\Object\JWKInterface $recipient_key |
||
406 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
424 | |||
425 | /** |
||
426 | * @param array $complete_headers |
||
427 | * |
||
428 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
429 | */ |
||
430 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
441 | |||
442 | /** |
||
443 | * @param array $complete_headers |
||
444 | * |
||
445 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
446 | */ |
||
447 | private function findContentEncryptionAlgorithm(array $complete_headers) |
||
460 | |||
461 | /** |
||
462 | * @param array $complete_headers |
||
463 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
||
464 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
465 | * @param \Jose\Object\JWKInterface $recipient_key |
||
466 | * @param \Jose\Object\JWKInterface|null $sender_key |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
||
480 | |||
481 | /** |
||
482 | * @param int $size |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | private function createCEK($size) |
||
490 | |||
491 | /** |
||
492 | * @param int $size |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | private function createIV($size) |
||
500 | |||
501 | /** |
||
502 | * @param int $length |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | private function generateRandomString($length) |
||
514 | } |
||
515 |