Total Complexity | 79 |
Total Lines | 371 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like request 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 request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class request extends Curl |
||
12 | { |
||
13 | private $result_request = []; |
||
14 | /** |
||
15 | * request instances. |
||
16 | * |
||
17 | * @var request |
||
18 | */ |
||
19 | private static $_instance = null; |
||
20 | |||
21 | public function __construct($base = null) |
||
22 | { |
||
23 | parent::__construct($base); |
||
24 | } |
||
25 | |||
26 | public function disableSSL() |
||
27 | { |
||
28 | $this->setOpt(CURLOPT_SSL_VERIFYPEER, false); |
||
29 | $this->setOpt(CURLOPT_SSL_VERIFYHOST, false); |
||
30 | |||
31 | return $this; |
||
32 | } |
||
33 | |||
34 | public $cache = false; |
||
35 | |||
36 | /** |
||
37 | * Execute curl. |
||
38 | * |
||
39 | * @return string|array|null |
||
40 | */ |
||
41 | public function execute(string $method, string $path, bool $rewrite = false) |
||
42 | { |
||
43 | $self = $this; |
||
44 | $self->set_method($method); |
||
45 | $self->set_url($path); |
||
46 | if ($self->cache) { |
||
47 | if (!$rewrite) { |
||
48 | if (file_exists($self->cache_path())) { |
||
49 | $this->response = $self->get_cache(); |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | |||
54 | if (!$this->response || empty($this->response)) { |
||
55 | $this->exec(); |
||
56 | } else { |
||
57 | $this->error = false; |
||
58 | } |
||
59 | |||
60 | if (!$this->error) { |
||
61 | if ($this->cache) { |
||
62 | \Filemanager\file::file($this->cache_path(), $this->response, true); |
||
63 | } |
||
64 | if (is_object($this->response)) { |
||
65 | $this->response = (array) $this->response; |
||
66 | } |
||
67 | |||
68 | return $this->response; |
||
69 | } |
||
70 | |||
71 | return null; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get Cache Path. |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | private function cache_path() |
||
80 | { |
||
81 | return ROOT . '/tmp/curl/' . md5($this->url); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get cached content. |
||
86 | * |
||
87 | * @return string|array|null |
||
88 | */ |
||
89 | private function get_cache() |
||
90 | { |
||
91 | if (file_exists($this->cache_path())) { |
||
92 | return \Filemanager\file::get($this->cache_path(), true); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function set_url(string $url) |
||
97 | { |
||
98 | $this->setUrl($url); |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | public function set_method(string $method) |
||
104 | { |
||
105 | $this->setOpt(CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public static function getInstance($base = null) |
||
111 | { |
||
112 | if (null === self::$_instance) { |
||
113 | self::$_instance = new self($base); |
||
114 | } |
||
115 | |||
116 | return self::$_instance; |
||
117 | } |
||
118 | |||
119 | public static function static_request($opt) |
||
120 | { |
||
121 | return self::getInstance()->request($opt); |
||
122 | //return self::request($opt); |
||
123 | } |
||
124 | |||
125 | private $require_content_length = false; |
||
126 | private $dumpNow = false; |
||
127 | |||
128 | public function isDUMPNow() |
||
129 | { |
||
130 | return true === $this->dumpNow; |
||
131 | } |
||
132 | |||
133 | public function DUMPNow(...$what) |
||
134 | { |
||
135 | if ($this->isDUMPNow()) { |
||
136 | $this->exitJSON($what); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public function exitJSON(...$what) |
||
141 | { |
||
142 | foreach ($what as $these) { |
||
143 | json::json($these); |
||
144 | echo "\n\n"; |
||
145 | } |
||
146 | exit; |
||
|
|||
147 | } |
||
148 | |||
149 | /** |
||
150 | * cURL shooter request. |
||
151 | * |
||
152 | * @param array $opt |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function request($opt) |
||
317 | } |
||
318 | |||
319 | public static function getCurlOpt($ch, int $what) |
||
320 | { |
||
321 | return curl_getinfo($ch, $what); |
||
322 | } |
||
323 | |||
324 | public function set_header_array($headers, $trim = false) |
||
336 | } |
||
337 | |||
338 | public function set_cookie_file($cookie = null) |
||
339 | { |
||
340 | if (!$cookie) { |
||
341 | $cookie = __DIR__ . '/cookie/default.txt'; |
||
342 | if (isset($_SERVER['HTTP_USER_AGENT'])) { |
||
343 | $cookie = __DIR__ . '/cookie/' . $_SERVER['HTTP_USER_AGENT'] . '.txt'; |
||
344 | } |
||
345 | } |
||
346 | if ($cookie) { |
||
347 | \Filemanager\file::file($cookie, '#cookie'); |
||
348 | } |
||
349 | $this->setCookieFile($cookie); |
||
350 | $this->setCookieJar($cookie); |
||
351 | $this->result_request['cookie'] = $cookie; |
||
352 | |||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Check array is assoc |
||
358 | * * var_dump(isAssoc(['a', 'b', 'c'])); // false |
||
359 | * * var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false |
||
360 | * * var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true |
||
361 | * * var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true. |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function isAssoc(array $arr) |
||
366 | { |
||
367 | if ([] === $arr) { |
||
368 | return false; |
||
369 | } |
||
370 | |||
371 | return array_keys($arr) !== range(0, count($arr) - 1); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Check array has some string key. |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function has_string_keys(array $array) |
||
382 | } |
||
383 | } |
||
384 | |||
385 | if (!function_exists('is_iterable')) { |
||
386 | function is_iterable($var) |
||
387 | { |
||
388 | return is_array($var) || $var instanceof \Traversable; |
||
389 | } |
||
390 | } |
||
391 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.