getStatusCode(); $reason = $response->getReasonPhrase(); if (in_array($status, $invalidStatus)) { $contents = $response->getBody()->getContents(); $this->logger->warning('Invalid Status', [ 'uri' => $request_uri, 'code' => $status, 'reason' => $reason, 'contents' => $contents ]); $exception = new HttpException("{$reason}\n{$contents}", $status); throw new EmptyResponse($request_uri, $exception); } if (!in_array($status, $validStatus)) { $contents = $response->getBody()->getContents(); $this->logger->warning('Not Valid Status', [ 'uri' => $request_uri, 'code' => $status, 'reason' => $reason, 'contents' => $contents ]); $exception = new HttpException("{$reason}\n{$contents}", $status); throw new EmptyResponse($request_uri, $exception); } } /** * @param string $request_uri * @param array $validStatus * @param array $invalidStatus * @return array * @throws EmptyResponse */ protected function sendGet(string $request_uri, array $validStatus, array $invalidStatus): array { try { $response = $this->client->get($request_uri); } catch (ClientExceptionInterface $exception) { throw new EmptyResponse($request_uri, $exception); } $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); return json_decode($response->getBody()->getContents(), true); } /** * @param string $request_uri * @param array $data * @param array $validStatus * @param array $invalidStatus * @return bool * @throws EmptyResponse */ protected function sendAdd(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool { $params = $this->mapParams($data); $this->logger->info('Send Add', ['uri' => $request_uri, 'params' => $params]); try { $response = $this->client->post($request_uri, ['json' => $params]); } catch (ClientExceptionInterface $exception) { throw new EmptyResponse($request_uri, $exception); } $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); $contents = $response->getBody()->getContents(); if (trim($contents) === '') { $this->logger->warning("Empty contents", [ 'uri' => $request_uri, 'data' => $data, 'code' => $response->getStatusCode(), 'reason' => $response->getReasonPhrase(), 'contents' => $contents ]); throw new EmptyResponse($request_uri); } $json = json_decode($contents, true); $this->logger->info('Add Response', $json); return $this->save($json); } /** * @param string $request_uri * @param array $data * @param array $validStatus * @param array $invalidStatus * @return bool * @throws EmptyResponse */ protected function sendEdit(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool { $params = $this->mapParams($data); try { $response = $this->client->put($request_uri, ['json' => $params]); } catch (ClientExceptionInterface $exception) { throw new EmptyResponse($request_uri, $exception); } $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); $contents = $response->getBody()->getContents(); $json = json_decode($contents, true); return $this->save($json); } /** * @param string $request_uri * @param array $validStatus * @param array $invalidStatus * @return void * @throws EmptyResponse */ protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void { try { $response = $this->client->delete($request_uri); } catch (ClientExceptionInterface $exception) { throw new EmptyResponse($request_uri, $exception); } $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); } protected function doSave(Repository $repository, array $data): bool { try { $repository->fetchByTokuId($data['id']); return true; } catch (EmptyResult) { $mappedData = $this->mapSave($data); $filteredData = $repository->filterData($mappedData); $model = $repository->create($filteredData); try { $repository->save($model); return true; } catch (PDOException $exception) { $this->logger->warning($exception); return false; } } } /** * @param callable $callable * @param string $id * @param string $message * @return array * @throws InvalidResult */ protected function doGetById(callable $callable, string $id, string $message): array { try { $model = $callable($id); return json_decode(json_encode($model), true); } catch (EmptyResult $exception) { throw new InvalidResult($message, 404, $exception); } } abstract public function save(array $data): bool; abstract protected function mapParams(array $data): array; abstract protected function mapSave(array $data): array; }