Total Complexity | 47 |
Total Lines | 354 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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) |
||
122 | { |
||
123 | if ($type !== self::ALPHAHASH_STRING) { |
||
124 | switch ($type) { |
||
125 | default: |
||
126 | case self::ALPHANUMERIC_STRING: |
||
127 | $codeAlphabet = implode(range('A', 'Z')); // Uppercase Alphabet |
||
128 | $codeAlphabet .= implode(range('a', 'z')); // Lowercase Alphabet |
||
129 | $codeAlphabet .= implode(range(0, 9)); // Numeric Alphabet |
||
130 | break; |
||
131 | case self::ALPHAUPPERCASE_STRING: |
||
132 | $codeAlphabet = implode(range('A', 'Z')); // Uppercase Alphabet |
||
133 | break; |
||
134 | case self::ALPHALOWERCASE_STRING: |
||
135 | $codeAlphabet = implode(range('a', 'z')); // Lowercase Alphabet |
||
136 | break; |
||
137 | case self::NUMERIC_STRING: |
||
138 | $codeAlphabet = implode(range(0, 9)); // Numeric Alphabet |
||
139 | break; |
||
140 | } |
||
141 | |||
142 | $token = ''; |
||
143 | $max = strlen($codeAlphabet); |
||
144 | |||
145 | for ($i = 0; $i < $length; $i++) { |
||
146 | $token .= $codeAlphabet[ random_int(0, $max - 1) ]; |
||
147 | } |
||
148 | |||
149 | return $token; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * ALPHABIN2HEX_STRING |
||
154 | */ |
||
155 | if (function_exists('random_bytes')) { |
||
156 | $randomData = random_bytes(20); |
||
157 | if ($randomData !== false && strlen($randomData) === 20) { |
||
158 | return bin2hex($randomData); |
||
159 | } |
||
160 | } |
||
161 | if (function_exists('openssl_random_pseudo_bytes')) { |
||
162 | $randomData = openssl_random_pseudo_bytes(20); |
||
163 | if ($randomData !== false && strlen($randomData) === 20) { |
||
164 | return bin2hex($randomData); |
||
165 | } |
||
166 | } |
||
167 | if (function_exists('mcrypt_create_iv')) { |
||
168 | $randomData = mcrypt_create_iv(20, MCRYPT_DEV_URANDOM); |
||
169 | if ($randomData !== false && strlen($randomData) === 20) { |
||
170 | return bin2hex($randomData); |
||
171 | } |
||
172 | } |
||
173 | if (@file_exists('/dev/urandom')) { // Get 100 bytes of random data |
||
174 | $randomData = file_get_contents('/dev/urandom', false, null, 0, 20); |
||
175 | if ($randomData !== false && strlen($randomData) === 20) { |
||
176 | return bin2hex($randomData); |
||
177 | } |
||
178 | } |
||
179 | // Last resort which you probably should just get rid of: |
||
180 | $randomData = mt_rand() . mt_rand() . mt_rand() . mt_rand() . microtime(true) . uniqid(mt_rand(), true); |
||
181 | |||
182 | return substr(hash('sha512', $randomData), 0, $length); |
||
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 | } |