Code quality refactoring for AbstractApi and PostFactory.

This commit is contained in:
Alexey Skobkin 2017-01-15 19:53:17 +03:00
parent a816ec59fc
commit 1d88ccb6a3
2 changed files with 46 additions and 53 deletions

View File

@ -51,54 +51,12 @@ class AbstractApi
$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
*/
public function getGetResponseBody($path, array $parameters = []): StreamInterface
{
$response = $this->sendGetRequest($path, $parameters);
$this->checkResponse($response);
return $response->getBody();
return $this->sendGetRequest($path, $parameters)->getBody();
}
/**
@ -106,11 +64,7 @@ class AbstractApi
*/
public function getPostResponseBody(string $path, array $parameters = []): StreamInterface
{
$response = $this->sendPostRequest($path, $parameters);
$this->checkResponse($response);
return $response->getBody();
return $this->sendPostRequest($path, $parameters)->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 NotFoundException
@ -157,20 +154,16 @@ class AbstractApi
switch ($code) {
case SymfonyResponse::HTTP_UNAUTHORIZED:
throw new UnauthorizedException($reason, $code);
break;
case SymfonyResponse::HTTP_FORBIDDEN:
throw new ForbiddenException($reason, $code);
break;
case SymfonyResponse::HTTP_NOT_FOUND:
throw new NotFoundException($reason, $code);
break;
case SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR:
case SymfonyResponse::HTTP_NOT_IMPLEMENTED:
case SymfonyResponse::HTTP_BAD_GATEWAY:
case SymfonyResponse::HTTP_SERVICE_UNAVAILABLE:
case SymfonyResponse::HTTP_GATEWAY_TIMEOUT:
throw new ServerProblemException($reason, $code);
break;
}
}
}

View File

@ -76,7 +76,7 @@ class PostFactory extends AbstractFactory
$hasNew = false;
foreach ($page->getPosts() as $postData) {
foreach ((array) $page->getPosts() as $postData) {
try {
if (null === $this->postRepository->find($postData->getPost()->getId())) {
$hasNew = true;