| Total Complexity | 47 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Token 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.
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 Token, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Token |
||
| 31 | { |
||
| 32 | use ErrorCollectorTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Token::ALPHANUMERIC_STRING |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | const ALPHANUMERIC_STRING = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Token::ALPHAUPPERCASE_STRING |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | const ALPHAUPPERCASE_STRING = 1; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Token::ALPHALOWERCASE_STRING |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | const ALPHALOWERCASE_STRING = 2; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Token::ALPHAHASH_STRING |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | const ALPHAHASH_STRING = 3; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Token::NUMERIC_STRING |
||
| 64 | * |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | const NUMERIC_STRING = 4; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Token::$key |
||
| 71 | * |
||
| 72 | * @var string|null |
||
| 73 | */ |
||
| 74 | protected $key = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Allow the current timestamp to be specified. |
||
| 78 | * Useful for fixing a value within unit testing. |
||
| 79 | * |
||
| 80 | * Will default to PHP time() value if null. |
||
| 81 | */ |
||
| 82 | protected $timestamp; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Token::$algorithm |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $algorithm = 'HMAC-SHA256'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Token::$headers |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $headers = []; |
||
| 97 | |||
| 98 | // ------------------------------------------------------------------------ |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Token::__construct |
||
| 102 | */ |
||
| 103 | public function __construct() |
||
| 104 | { |
||
| 105 | if (class_exists('O2System\Framework', false)) { |
||
| 106 | $this->key = config()->getItem('security')->encryptionKey; |
||
|
|
|||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | // ------------------------------------------------------------------------ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Token::generate |
||
| 114 | * |
||
| 115 | * @param int $length Token string length. |
||
| 116 | * @param int $type Token string type. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | * @throws \Exception |
||
| 120 | */ |
||
| 121 | public static function generate($length = 8, $type = self::ALPHANUMERIC_STRING) |
||
| 183 | } |
||
| 184 | |||
| 185 | // ------------------------------------------------------------------------ |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Token::setKey |
||
| 189 | * |
||
| 190 | * @param string $key |
||
| 191 | * |
||
| 192 | * @return static |
||
| 193 | */ |
||
| 194 | public function setKey($key) |
||
| 199 | } |
||
| 200 | |||
| 201 | // ------------------------------------------------------------------------ |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Token::setAlgorithm |
||
| 205 | * |
||
| 206 | * @param string $algorithm |
||
| 207 | * |
||
| 208 | * @return static |
||
| 209 | */ |
||
| 210 | public function setAlgorithm($algorithm) |
||
| 211 | { |
||
| 212 | $algorithm = strtoupper($algorithm); |
||
| 213 | |||
| 214 | if (Algorithm::validate($algorithm)) { |
||
| 215 | $this->algorithm = $algorithm; |
||
| 216 | } |
||
| 217 | |||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | // ------------------------------------------------------------------------ |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Token::setTimestamp |
||
| 225 | * |
||
| 226 | * @param int|string $timestamp |
||
| 227 | * |
||
| 228 | * @return static |
||
| 229 | */ |
||
| 230 | public function setTimestamp($timestamp) |
||
| 235 | } |
||
| 236 | |||
| 237 | // ------------------------------------------------------------------------ |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Token::encode |
||
| 241 | * |
||
| 242 | * @param array $payload |
||
| 243 | * @param null $key |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | * @throws \O2System\Spl\Exceptions\Logic\DomainException |
||
| 247 | */ |
||
| 248 | public function encode(array $payload, $key = null) |
||
| 249 | { |
||
| 250 | $key = empty($key) ? $this->key : $key; |
||
| 251 | |||
| 252 | $this->addHeader('algorithm', $this->algorithm); |
||
| 253 | |||
| 254 | // Create Header Segment |
||
| 255 | $segments[] = Base64::encode(Json::encode($this->headers)); |
||
| 256 | |||
| 257 | // Create Payload Segment |
||
| 258 | $segments[] = Base64::encode(Json::encode($payload)); |
||
| 259 | |||
| 260 | // Create Signature Segment |
||
| 261 | $segments[] = Base64::encode(Signature::generate($segments, $key, $this->algorithm)); |
||
| 262 | |||
| 263 | return implode('.', $segments); |
||
| 264 | } |
||
| 265 | |||
| 266 | // ------------------------------------------------------------------------ |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Token::addHeader |
||
| 270 | * |
||
| 271 | * @param string $key |
||
| 272 | * @param mixed $value |
||
| 273 | * |
||
| 274 | * @return static |
||
| 275 | */ |
||
| 276 | public function addHeader($key, $value) |
||
| 281 | } |
||
| 282 | |||
| 283 | // ------------------------------------------------------------------------ |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Token::decode |
||
| 287 | * |
||
| 288 | * @param string $token |
||
| 289 | * @param null $key |
||
| 290 | * |
||
| 291 | * @return bool|\O2System\Spl\DataStructures\SplArrayObject|string|null |
||
| 292 | */ |
||
| 293 | public function decode($token, $key = null) |
||
| 384 | } |
||
| 385 | } |