Code quality refactoring for AbstractApi and PostFactory.
This commit is contained in:
parent
a816ec59fc
commit
1d88ccb6a3
|
@ -51,54 +51,12 @@ class AbstractApi
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path Request path
|
|
||||||
* @param array $parameters Key => Value array of query parameters
|
|
||||||
*
|
|
||||||
* @return ResponseInterface
|
|
||||||
*
|
|
||||||
* @throws NetworkException
|
|
||||||
*/
|
|
||||||
public function sendGetRequest(string $path, array $parameters = []): ResponseInterface
|
|
||||||
{
|
|
||||||
$this->logger->debug('Sending GET request', ['path' => $path, 'parameters' => $parameters]);
|
|
||||||
|
|
||||||
try {
|
|
||||||
return $this->client->request('GET', $path, ['query' => $parameters]);
|
|
||||||
} catch (TransferException $e) {
|
|
||||||
throw new NetworkException('Request error', $e->getCode(), $e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path Request path
|
|
||||||
* @param array $parameters Key => Value array of request data
|
|
||||||
*
|
|
||||||
* @return ResponseInterface
|
|
||||||
*
|
|
||||||
* @throws NetworkException
|
|
||||||
*/
|
|
||||||
public function sendPostRequest(string $path, array $parameters = []): ResponseInterface
|
|
||||||
{
|
|
||||||
$this->logger->debug('Sending POST request', ['path' => $path, 'parameters' => $parameters]);
|
|
||||||
|
|
||||||
try {
|
|
||||||
return $this->client->request('POST', $path, ['form_params' => $parameters]);
|
|
||||||
} catch (TransferException $e) {
|
|
||||||
throw new NetworkException('Request error', $e->getCode(), $e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make GET request and return response body
|
* Make GET request and return response body
|
||||||
*/
|
*/
|
||||||
public function getGetResponseBody($path, array $parameters = []): StreamInterface
|
public function getGetResponseBody($path, array $parameters = []): StreamInterface
|
||||||
{
|
{
|
||||||
$response = $this->sendGetRequest($path, $parameters);
|
return $this->sendGetRequest($path, $parameters)->getBody();
|
||||||
|
|
||||||
$this->checkResponse($response);
|
|
||||||
|
|
||||||
return $response->getBody();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -106,11 +64,7 @@ class AbstractApi
|
||||||
*/
|
*/
|
||||||
public function getPostResponseBody(string $path, array $parameters = []): StreamInterface
|
public function getPostResponseBody(string $path, array $parameters = []): StreamInterface
|
||||||
{
|
{
|
||||||
$response = $this->sendPostRequest($path, $parameters);
|
return $this->sendPostRequest($path, $parameters)->getBody();
|
||||||
|
|
||||||
$this->checkResponse($response);
|
|
||||||
|
|
||||||
return $response->getBody();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,6 +97,49 @@ class AbstractApi
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path Request path
|
||||||
|
* @param array $parameters Key => Value array of query parameters
|
||||||
|
*
|
||||||
|
* @return ResponseInterface
|
||||||
|
*
|
||||||
|
* @throws NetworkException
|
||||||
|
*/
|
||||||
|
private function sendGetRequest(string $path, array $parameters = []): ResponseInterface
|
||||||
|
{
|
||||||
|
$this->logger->debug('Sending GET request', ['path' => $path, 'parameters' => $parameters]);
|
||||||
|
|
||||||
|
return $this->sendRequest('GET', $path, ['query' => $parameters]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path Request path
|
||||||
|
* @param array $parameters Key => Value array of request data
|
||||||
|
*
|
||||||
|
* @return ResponseInterface
|
||||||
|
*
|
||||||
|
* @throws NetworkException
|
||||||
|
*/
|
||||||
|
private function sendPostRequest(string $path, array $parameters = []): ResponseInterface
|
||||||
|
{
|
||||||
|
$this->logger->debug('Sending POST request', ['path' => $path, 'parameters' => $parameters]);
|
||||||
|
|
||||||
|
return $this->sendRequest('POST', $path, ['form_params' => $parameters]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function sendRequest(string $method, string $path, array $parameters): ResponseInterface
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$response = $this->client->request($method, $path, ['query' => $parameters]);
|
||||||
|
|
||||||
|
$this->checkResponse($response);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
} catch (TransferException $e) {
|
||||||
|
throw new NetworkException('Request error', $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ForbiddenException
|
* @throws ForbiddenException
|
||||||
* @throws NotFoundException
|
* @throws NotFoundException
|
||||||
|
@ -157,20 +154,16 @@ class AbstractApi
|
||||||
switch ($code) {
|
switch ($code) {
|
||||||
case SymfonyResponse::HTTP_UNAUTHORIZED:
|
case SymfonyResponse::HTTP_UNAUTHORIZED:
|
||||||
throw new UnauthorizedException($reason, $code);
|
throw new UnauthorizedException($reason, $code);
|
||||||
break;
|
|
||||||
case SymfonyResponse::HTTP_FORBIDDEN:
|
case SymfonyResponse::HTTP_FORBIDDEN:
|
||||||
throw new ForbiddenException($reason, $code);
|
throw new ForbiddenException($reason, $code);
|
||||||
break;
|
|
||||||
case SymfonyResponse::HTTP_NOT_FOUND:
|
case SymfonyResponse::HTTP_NOT_FOUND:
|
||||||
throw new NotFoundException($reason, $code);
|
throw new NotFoundException($reason, $code);
|
||||||
break;
|
|
||||||
case SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR:
|
case SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR:
|
||||||
case SymfonyResponse::HTTP_NOT_IMPLEMENTED:
|
case SymfonyResponse::HTTP_NOT_IMPLEMENTED:
|
||||||
case SymfonyResponse::HTTP_BAD_GATEWAY:
|
case SymfonyResponse::HTTP_BAD_GATEWAY:
|
||||||
case SymfonyResponse::HTTP_SERVICE_UNAVAILABLE:
|
case SymfonyResponse::HTTP_SERVICE_UNAVAILABLE:
|
||||||
case SymfonyResponse::HTTP_GATEWAY_TIMEOUT:
|
case SymfonyResponse::HTTP_GATEWAY_TIMEOUT:
|
||||||
throw new ServerProblemException($reason, $code);
|
throw new ServerProblemException($reason, $code);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,7 @@ class PostFactory extends AbstractFactory
|
||||||
|
|
||||||
$hasNew = false;
|
$hasNew = false;
|
||||||
|
|
||||||
foreach ($page->getPosts() as $postData) {
|
foreach ((array) $page->getPosts() as $postData) {
|
||||||
try {
|
try {
|
||||||
if (null === $this->postRepository->find($postData->getPost()->getId())) {
|
if (null === $this->postRepository->find($postData->getPost()->getId())) {
|
||||||
$hasNew = true;
|
$hasNew = true;
|
||||||
|
|
Loading…
Reference in a new issue