Complex classes like KeyConverter 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 KeyConverter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | final class KeyConverter |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @param string $file |
||
| 24 | * |
||
| 25 | * @throws \InvalidArgumentException |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public static function loadKeyFromCertificateFile($file) |
||
| 30 | { |
||
| 31 | if (!file_exists($file)) { |
||
| 32 | throw new \InvalidArgumentException(sprintf('File "%s" does not exist.', $file)); |
||
| 33 | } |
||
| 34 | $content = file_get_contents($file); |
||
| 35 | |||
| 36 | return self::loadKeyFromCertificate($content); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $certificate |
||
| 41 | * |
||
| 42 | * @throws \InvalidArgumentException |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | public static function loadKeyFromCertificate($certificate) |
||
| 47 | { |
||
| 48 | try { |
||
| 49 | $res = openssl_x509_read($certificate); |
||
| 50 | } catch (\Exception $e) { |
||
| 51 | $certificate = self::convertDerToPem($certificate); |
||
| 52 | $res = openssl_x509_read($certificate); |
||
| 53 | } |
||
| 54 | if (false === $res) { |
||
| 55 | throw new \InvalidArgumentException('Unable to load the certificate'); |
||
| 56 | } |
||
| 57 | $values = self::loadKeyFromX509Resource($res); |
||
| 58 | openssl_x509_free($res); |
||
| 59 | |||
| 60 | return $values; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param resource $res |
||
| 65 | * |
||
| 66 | * @throws \Exception |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public static function loadKeyFromX509Resource($res) |
||
| 71 | { |
||
| 72 | $key = openssl_get_publickey($res); |
||
| 73 | |||
| 74 | $details = openssl_pkey_get_details($key); |
||
| 75 | if (isset($details['key'])) { |
||
| 76 | $values = self::loadKeyFromPEM($details['key']); |
||
| 77 | if (function_exists('openssl_x509_fingerprint')) { |
||
| 78 | $values['x5t'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha1', true)); |
||
| 79 | $values['x5t#256'] = Base64Url::encode(openssl_x509_fingerprint($res, 'sha256', true)); |
||
| 80 | } else { |
||
| 81 | openssl_x509_export($res, $pem); |
||
| 82 | $values['x5t'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha1', true)); |
||
| 83 | $values['x5t#256'] = Base64Url::encode(self::calculateX509Fingerprint($pem, 'sha256', true)); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $values; |
||
| 87 | } |
||
| 88 | throw new \InvalidArgumentException('Unable to load the certificate'); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $file |
||
| 93 | * @param null|string $password |
||
| 94 | * |
||
| 95 | * @throws \Exception |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public static function loadFromKeyFile($file, $password = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $key |
||
| 108 | * @param null|string $password |
||
| 109 | * |
||
| 110 | * @throws \Exception |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public static function loadFromKey($key, $password = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $der |
||
| 125 | * @param null|string $password |
||
| 126 | * |
||
| 127 | * @throws \Exception |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | private static function loadKeyFromDER($der, $password = null) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $pem |
||
| 140 | * @param null|string $password |
||
| 141 | * |
||
| 142 | * @throws \Exception |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | private static function loadKeyFromPEM($pem, $password = null) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param array $data |
||
| 205 | * |
||
| 206 | * @throws \Exception |
||
| 207 | * |
||
| 208 | * @return \phpseclib\Crypt\RSA |
||
| 209 | */ |
||
| 210 | public static function fromArrayToRSACrypt(array $data) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param array $x5c |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public static function loadFromX5C(array $x5c) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array $data |
||
| 269 | * |
||
| 270 | * @throws \Exception |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public static function fromArrayToXML(array $data) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param $key |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | private static function getElement($key) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $pem |
||
| 327 | * @param string[] $matches |
||
| 328 | * @param null|string $password |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | private static function decodePem($pem, array $matches, $password = null) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $der_data |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | private static function convertDerToPem($der_data) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $pem |
||
| 371 | * @param string $algorithm |
||
| 372 | * @param bool $binary |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | private static function calculateX509Fingerprint($pem, $algorithm, $binary = false) |
||
| 383 | } |
||
| 384 |