1 | <?php |
||
14 | class HttpClient |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $accessKey; |
||
20 | |||
21 | /** |
||
22 | * @var ClientInterface |
||
23 | */ |
||
24 | private $httpClient; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $secretKey; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $host; |
||
35 | |||
36 | /** |
||
37 | * true if the last response returns |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $isSocket; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * HttpClient constructor. |
||
45 | */ |
||
46 | public function __construct() |
||
53 | |||
54 | /** |
||
55 | * get isSocket |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function isSocket() |
||
63 | |||
64 | /** |
||
65 | * @param $uri |
||
66 | * @param bool $withHost |
||
67 | * |
||
68 | * @return int|mixed |
||
69 | */ |
||
70 | public function get($uri, $withHost = false) |
||
81 | |||
82 | /** |
||
83 | * @param $uri |
||
84 | * @param array $data |
||
85 | * @param bool $withHost |
||
86 | * |
||
87 | * @return mixed|ResponseInterface |
||
88 | */ |
||
89 | public function post($uri, array $data = [], $withHost = false) |
||
95 | |||
96 | /** |
||
97 | * @param ClientInterface $httpClient |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setHttpClient(ClientInterface $httpClient) |
||
107 | |||
108 | /** |
||
109 | * @param $method |
||
110 | * @param $uri |
||
111 | * @param $withHost |
||
112 | * @param array $data |
||
113 | * @return mixed|ResponseInterface |
||
114 | */ |
||
115 | private function request($method, $uri, $withHost, $data = []) |
||
129 | |||
130 | /** |
||
131 | * @param ResponseInterface $response |
||
132 | */ |
||
133 | private function parseResponse(ResponseInterface $response) |
||
137 | } |
||
138 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: