linkprofit-cpa /
tracker-client
| 1 | <?php |
||
| 2 | |||
| 3 | namespace linkprofit\Tracker; |
||
| 4 | |||
| 5 | use GuzzleHttp\ClientInterface; |
||
| 6 | use GuzzleHttp\Psr7\Request; |
||
| 7 | use GuzzleHttp\Psr7\Response; |
||
| 8 | use linkprofit\Tracker\exception\ConnectionExceptionHandler; |
||
| 9 | use linkprofit\Tracker\exception\TrackerException; |
||
| 10 | use linkprofit\Tracker\request\ConnectionQuery; |
||
| 11 | use linkprofit\Tracker\request\RouteInterface; |
||
| 12 | use linkprofit\Tracker\response\ResponseHandlerInterface; |
||
| 13 | use linkprofit\Tracker\response\ArrayResponseHandler; |
||
| 14 | use Psr\SimpleCache\CacheInterface; |
||
| 15 | use Symfony\Component\Cache\Simple\FilesystemCache; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class Client |
||
| 19 | * |
||
| 20 | * @package linkprofit\Tracker |
||
| 21 | */ |
||
| 22 | class Client |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $apiUrl; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ConnectionQuery |
||
| 31 | */ |
||
| 32 | protected $connection; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var CacheInterface |
||
| 36 | */ |
||
| 37 | protected $cache; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \GuzzleHttp\Client|ClientInterface |
||
| 41 | */ |
||
| 42 | protected $httpClient; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ResponseHandlerInterface |
||
| 46 | */ |
||
| 47 | protected $responseHandler; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Client constructor. |
||
| 51 | * |
||
| 52 | * @param Connection $connection |
||
| 53 | * @param ClientInterface|null $httpClient |
||
| 54 | * @param ResponseHandlerInterface|null $responseHandler |
||
| 55 | * @param CacheInterface|null $cache |
||
| 56 | */ |
||
| 57 | 9 | public function __construct( |
|
| 58 | Connection $connection, |
||
| 59 | ClientInterface $httpClient = null, |
||
| 60 | ResponseHandlerInterface $responseHandler = null, |
||
| 61 | CacheInterface $cache = null |
||
| 62 | ) |
||
| 63 | { |
||
| 64 | 9 | $this->connection = new ConnectionQuery($connection); |
|
| 65 | 9 | $this->apiUrl = $connection->apiUrl; |
|
| 66 | 9 | $this->httpClient = ($httpClient === null) ? $this->getDefaultHttpClient() : $httpClient; |
|
| 67 | 9 | $this->responseHandler = ($responseHandler === null) ? $this->getDefaultResponseHandler() : $responseHandler; |
|
| 68 | 9 | $this->cache = $cache; |
|
| 69 | 9 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param RouteInterface $route |
||
| 73 | * |
||
| 74 | * @return ArrayResponseHandler|ResponseHandlerInterface|null |
||
| 75 | * @throws TrackerException |
||
| 76 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 77 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 78 | */ |
||
| 79 | 2 | public function exec(RouteInterface $route) |
|
| 80 | { |
||
| 81 | 2 | if ($this->getAuthToken() === null && $this->connect() === false) { |
|
| 82 | return null; |
||
| 83 | } |
||
| 84 | |||
| 85 | 2 | $route->setAccessLevel($this->connection->getAccessLevel()); |
|
| 86 | 2 | $route->setAuthToken($this->getAuthToken()); |
|
| 87 | |||
| 88 | 2 | if ($this->cache !== null && $this->cache->has($route->getHash())) { |
|
| 89 | 1 | $response = $this->getResponseFromCache($route->getHash()); |
|
| 90 | 1 | } else { |
|
| 91 | 2 | $request = $this->createRequest($route); |
|
| 92 | 2 | $response = $this->httpClient->send($request); |
|
| 93 | |||
| 94 | 2 | if ($this->cache !== null) { |
|
| 95 | 1 | $this->setResponseToCache($response, $route->getHash()); |
|
| 96 | 1 | } |
|
| 97 | } |
||
| 98 | |||
| 99 | 2 | $this->responseHandler->add($response); |
|
| 100 | 2 | $this->responseHandler->setExceptionHandler($route->getExceptionHandler()); |
|
| 101 | |||
| 102 | 2 | return $this->responseHandler; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * TODO сделать переподключение при ошибке N-ное кол-во раз |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | * @throws TrackerException |
||
| 110 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 111 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 112 | */ |
||
| 113 | 5 | public function connect() |
|
| 114 | { |
||
| 115 | 5 | $request = $this->createRequest($this->connection); |
|
| 116 | |||
| 117 | 5 | $result = $this->httpClient->send($request); |
|
| 118 | |||
| 119 | 5 | $responseHandler = new ArrayResponseHandler($result); |
|
| 120 | 5 | $responseHandler->setExceptionHandler(new ConnectionExceptionHandler()); |
|
| 121 | |||
| 122 | 5 | $response = $responseHandler->handle(); |
|
| 123 | |||
| 124 | 4 | if (isset($response['authToken'])) { |
|
| 125 | 4 | $this->setAuthToken($response['authToken']); |
|
| 126 | |||
| 127 | 4 | return true; |
|
| 128 | } |
||
| 129 | |||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param CacheInterface $cache |
||
| 135 | */ |
||
| 136 | 3 | public function setCache(CacheInterface $cache) |
|
| 137 | { |
||
| 138 | 3 | $this->cache = $cache; |
|
| 139 | 3 | } |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param ClientInterface $httpClient |
||
| 143 | */ |
||
| 144 | 5 | public function setHttpClient(ClientInterface $httpClient) |
|
| 145 | { |
||
| 146 | 5 | $this->httpClient = $httpClient; |
|
| 147 | 5 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param ResponseHandlerInterface $responseHandler |
||
| 151 | */ |
||
| 152 | 1 | public function setResponseHandler(ResponseHandlerInterface $responseHandler) |
|
| 153 | { |
||
| 154 | 1 | $this->responseHandler = $responseHandler; |
|
| 155 | 1 | } |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param null $path |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 159 | * |
||
| 160 | * @return FilesystemCache |
||
| 161 | */ |
||
| 162 | 4 | public function getDefaultFileCache($path = null) |
|
| 163 | { |
||
| 164 | 4 | if ($path === null) { |
|
| 165 | 1 | $path = dirname(__DIR__) . '/cache'; |
|
| 166 | 1 | } |
|
| 167 | |||
| 168 | 4 | return new FilesystemCache('', 0, $path); |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return ArrayResponseHandler |
||
| 173 | */ |
||
| 174 | 9 | public function getDefaultResponseHandler() |
|
| 175 | { |
||
| 176 | 9 | return new ArrayResponseHandler(); |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return \GuzzleHttp\Client |
||
| 181 | */ |
||
| 182 | 9 | public function getDefaultHttpClient() |
|
| 183 | { |
||
| 184 | 9 | return new \GuzzleHttp\Client(); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param RouteInterface $route |
||
| 189 | * @return Request |
||
| 190 | */ |
||
| 191 | 6 | protected function createRequest(RouteInterface $route) |
|
| 192 | { |
||
| 193 | 6 | $request = new Request( |
|
| 194 | 6 | $route->getMethod(), |
|
| 195 | 6 | $this->apiUrl . $route->getUrl(), |
|
| 196 | 6 | $headers = [], |
|
| 197 | 6 | $route->getBody() |
|
| 198 | 6 | ); |
|
| 199 | |||
| 200 | 6 | return $request; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param int $statusCode |
||
| 205 | * @param array $headers |
||
| 206 | * @param string|null $body |
||
| 207 | * |
||
| 208 | * @return Response |
||
| 209 | */ |
||
| 210 | 1 | protected function createResponse($statusCode = 200, $headers = [], $body = null) |
|
| 211 | { |
||
| 212 | 1 | $response = new Response( |
|
| 213 | 1 | $statusCode, |
|
| 214 | 1 | $headers, |
|
| 215 | $body |
||
| 216 | 1 | ); |
|
| 217 | |||
| 218 | 1 | return $response; |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param Response $response |
||
| 223 | * @param $key |
||
| 224 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 225 | */ |
||
| 226 | 1 | protected function setResponseToCache(Response $response, $key) |
|
| 227 | { |
||
| 228 | 1 | $value = json_encode([ |
|
| 229 | 1 | 'statusCode' => $response->getStatusCode(), |
|
| 230 | 1 | 'headers' => $response->getHeaders(), |
|
| 231 | 1 | 'body' => (string) $response->getBody() |
|
| 232 | 1 | ]); |
|
| 233 | |||
| 234 | 1 | $this->cache->set($key, $value); |
|
| 235 | 1 | } |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param $key |
||
| 239 | * @return Response|null |
||
| 240 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 241 | */ |
||
| 242 | 2 | protected function getResponseFromCache($key) |
|
| 243 | { |
||
| 244 | 2 | $value = json_decode($this->cache->get($key), 1); |
|
| 245 | |||
| 246 | 2 | if (is_array($value)) { |
|
| 247 | 1 | return $this->createResponse($value['statusCode'], $value['headers'], $value['body']); |
|
| 248 | } |
||
| 249 | |||
| 250 | 1 | return null; |
|
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $authToken |
||
| 255 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 256 | */ |
||
| 257 | 4 | protected function setAuthToken($authToken) |
|
| 258 | { |
||
| 259 | 4 | $this->connection->setAuthToken($authToken); |
|
| 260 | |||
| 261 | 4 | if ($this->cache !== null) { |
|
| 262 | 2 | $this->cache->set($this->connection->getHash(), $authToken); |
|
| 263 | 2 | } |
|
| 264 | 4 | } |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @return mixed|string |
||
| 268 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 269 | */ |
||
| 270 | 4 | protected function getAuthToken() |
|
| 271 | { |
||
| 272 | 4 | $authTokenName = $this->connection->getHash(); |
|
| 273 | 4 | if ($this->cache !== null && $this->cache->has($authTokenName)) { |
|
| 274 | 2 | return $this->cache->get($authTokenName); |
|
| 275 | } |
||
| 276 | |||
| 277 | 3 | return $this->connection->getAuthToken(); |
|
| 278 | } |
||
| 279 | } |
||
| 280 |