Skip to content

Commit

Permalink
refactored CpfVerificationService to extract Http Service #804
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermednt committed Aug 20, 2018
1 parent d0fa5ea commit 907be12
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* This file is part of the login-cidadao project or it's bundles.
*
* (c) Guilherme Donato <guilhermednt on github>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PROCERGS\LoginCidadao\CpfVerificationBundle\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfNotSubscribedToNfgException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfVerificationException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\WrongAnswerException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Model\ChallengeInterface;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

class CpfVerificationHttpService
{
/** @var Client */
private $client;

/**
* CpfVerificationHttpService constructor.
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @param ChallengeInterface $challenge
* @param string $answer
* @return bool
* @throws CpfVerificationException
*/
public function submitAnswer(ChallengeInterface $challenge, string $answer)
{
try {
$response = $this->client->post($this->getChallengePath($challenge), [
'form_params' => ['answer' => $answer],
]);
} catch (RequestException $e) {
$response = $e->getResponse();
}
$statusCode = $response->getStatusCode();
$body = (string)$response->getBody();
$response = json_decode($body, true);

if ($statusCode === 200 || $statusCode === 204) {
return true;
}

if ($statusCode === 429) {
throw $this->getTooManyRequestsException($response['message'] ?? null);
}

if ($statusCode === 403) {
if ($response['error'] === WrongAnswerException::ERROR_CODE) {
throw new WrongAnswerException($challenge, $response['message'] ?? "Wrong answer: {$answer}");
}
}

throw $this->getInvalidResponseException($statusCode, $body);
}

/**
* @param string $uri
* @return string
* @throws CpfVerificationException
*/
public function sendGetRequest(string $uri): string
{
try {
$response = $this->client->get($uri);
} catch (RequestException $e) {
$response = $e->getResponse();
}

$statusCode = $response->getStatusCode();
$body = (string)$response->getBody();
if ($statusCode === 200) {
return $body;
}

$response = json_decode($body, true);
if ($statusCode === 403) {
if ($response['error'] === CpfNotSubscribedToNfgException::ERROR_CODE) {
throw new CpfNotSubscribedToNfgException($response['cpf'], $response['message'] ?? '');
}
}

if ($statusCode === 429) {
throw $this->getTooManyRequestsException($response['message'] ?? null);
}

throw $this->getInvalidResponseException($statusCode, $body);
}

private function getInvalidResponseException($statusCode, $body): \LogicException
{
return new \LogicException("Invalid response code \"{$statusCode}\" with body \"{$body}\"");
}

private function getTooManyRequestsException($message = null): TooManyRequestsHttpException
{
return new TooManyRequestsHttpException(null, $message);
}

public function getListChallengesPath(string $cpf): string
{
return "cpf/{$cpf}/challenges";
}

public function getChallengePath(ChallengeInterface $challenge): string
{
return "cpf/{$challenge->getCpf()}/challenges/{$challenge->getName()}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@

namespace PROCERGS\LoginCidadao\CpfVerificationBundle\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfNotSubscribedToNfgException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfVerificationException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\WrongAnswerException;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Model\ChallengeInterface;
use PROCERGS\LoginCidadao\CpfVerificationBundle\Parser\ChallengeParser;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

class CpfVerificationService
{
/** @var Client */
private $client;
/** @var CpfVerificationHttpService */
private $httpService;

/**
* CpfVerificationService constructor.
* @param Client $client
* @param CpfVerificationHttpService $httpService
*/
public function __construct(Client $client)
public function __construct(CpfVerificationHttpService $httpService)
{
$this->client = $client;
$this->httpService = $httpService;
}

/**
Expand All @@ -40,7 +35,7 @@ public function __construct(Client $client)
*/
public function listAvailableChallenges(string $cpf): array
{
$body = $this->sendGetRequest($this->getListChallengesPath($cpf));
$body = $this->httpService->sendGetRequest($this->httpService->getListChallengesPath($cpf));

return $this->parseChallengesList($body);
}
Expand All @@ -52,7 +47,7 @@ public function listAvailableChallenges(string $cpf): array
*/
public function selectChallenge(ChallengeInterface $challenge): ChallengeInterface
{
$body = $this->sendGetRequest($this->getChallengePath($challenge));
$body = $this->httpService->sendGetRequest($this->httpService->getChallengePath($challenge));

return ChallengeParser::parseJson($body);
}
Expand All @@ -65,43 +60,7 @@ public function selectChallenge(ChallengeInterface $challenge): ChallengeInterfa
*/
public function answerChallenge(ChallengeInterface $challenge, $answer): bool
{
return $this->submitAnswer($challenge, $answer);
}

/**
* @param ChallengeInterface $challenge
* @param string $answer
* @return bool
* @throws CpfVerificationException
*/
private function submitAnswer(ChallengeInterface $challenge, string $answer)
{
try {
$response = $this->client->post($this->getChallengePath($challenge), [
'form_params' => ['answer' => $answer],
]);
} catch (RequestException $e) {
$response = $e->getResponse();
}
$statusCode = $response->getStatusCode();
$body = (string)$response->getBody();
$response = json_decode($body, true);

if ($statusCode === 200 || $statusCode === 204) {
return true;
}

if ($statusCode === 429) {
throw $this->getTooManyRequestsException($response['message'] ?? null);
}

if ($statusCode === 403) {
if ($response['error'] === WrongAnswerException::ERROR_CODE) {
throw new WrongAnswerException($challenge, $response['message'] ?? "Wrong answer: {$answer}");
}
}

throw $this->getInvalidResponseException($statusCode, $body);
return $this->httpService->submitAnswer($challenge, $answer);
}

/**
Expand All @@ -121,57 +80,4 @@ private function parseChallengesList(string $json): array

return $challenges;
}

private function getListChallengesPath(string $cpf): string
{
return "cpf/{$cpf}/challenges";
}

private function getChallengePath(ChallengeInterface $challenge): string
{
return "cpf/{$challenge->getCpf()}/challenges/{$challenge->getName()}";
}

/**
* @param string $uri
* @return string
* @throws CpfVerificationException
*/
private function sendGetRequest(string $uri): string
{
try {
$response = $this->client->get($uri);
} catch (RequestException $e) {
$response = $e->getResponse();
}

$statusCode = $response->getStatusCode();
$body = (string)$response->getBody();
if ($statusCode === 200) {
return $body;
}

$response = json_decode($body);
if ($statusCode === 403) {
if ($response['error'] === CpfNotSubscribedToNfgException::ERROR_CODE) {
throw new CpfNotSubscribedToNfgException($response['cpf'], $response['message'] ?? null);
}
}

if ($statusCode === 429) {
throw $this->getTooManyRequestsException($response['message'] ?? null);
}

throw $this->getInvalidResponseException($statusCode, $body);
}

private function getInvalidResponseException($statusCode, $body): \LogicException
{
return new \LogicException("Invalid response code \"{$statusCode}\" with body \"{$body}\"");
}

private function getTooManyRequestsException($message = null): TooManyRequestsHttpException
{
return new TooManyRequestsHttpException(null, $message);
}
}
Loading

0 comments on commit 907be12

Please sign in to comment.