-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Substituição dos métodos estáticos e refatoração nos testes
- Loading branch information
Showing
10 changed files
with
154 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
} | ||
], | ||
"require": { | ||
"php": "^7.0", | ||
"guzzlehttp/guzzle": "~6.0", | ||
"ext-simplexml": "*", | ||
"ext-json": "*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,17 @@ | |
|
||
namespace Claudsonm\CepPromise; | ||
|
||
use Claudsonm\CepPromise\Exceptions\CepPromiseException; | ||
use Claudsonm\CepPromise\Providers\CepAbertoProvider; | ||
use Claudsonm\CepPromise\Providers\CorreiosProvider; | ||
use Claudsonm\CepPromise\Providers\ViaCepProvider; | ||
use Exception; | ||
use GuzzleHttp\Promise; | ||
use GuzzleHttp\Promise\FulfilledPromise; | ||
use Claudsonm\CepPromise\Providers\ViaCepProvider; | ||
use Claudsonm\CepPromise\Providers\CorreiosProvider; | ||
use Claudsonm\CepPromise\Providers\CepAbertoProvider; | ||
use Claudsonm\CepPromise\Exceptions\CepPromiseException; | ||
|
||
/** | ||
* Classe responsável por receber o CEP e disparar as requisições aos providers. | ||
* Efetua a consulta pelas informações de um CEP em diferentes serviços de | ||
* forma concorrente, retornando a resposta mais rápida.. | ||
* | ||
* @author Claudson Martins <[email protected]> | ||
*/ | ||
|
@@ -24,15 +25,24 @@ class CepPromise | |
const ERROR_VALIDATION_CODE = 1; | ||
|
||
/** | ||
* Normaliza o CEP dado e efetua as requisições. | ||
* | ||
* @param $cepRawValue | ||
* Busca as informações referente ao CEP informado. | ||
* | ||
* @throws \Claudsonm\CepPromise\Exceptions\CepPromiseException | ||
* @param string|int $cep | ||
* @return Address | ||
* @throws CepPromiseException | ||
*/ | ||
public static function fetch($cep) | ||
{ | ||
return (new self())->run($cep); | ||
} | ||
|
||
/** | ||
* Dispara a cadeia de execução para obtenção das informações do CEP dado. | ||
* | ||
* @return \Claudsonm\CepPromise\Address | ||
* @param string|int $cepRawValue | ||
* @return Address | ||
*/ | ||
public static function fetch($cepRawValue) | ||
public function run($cepRawValue): Address | ||
{ | ||
$promise = new FulfilledPromise($cepRawValue); | ||
$cepData = $promise | ||
|
@@ -48,60 +58,34 @@ public static function fetch($cepRawValue) | |
return Address::create($cepData); | ||
} | ||
|
||
private static function fetchCepFromProviders() | ||
{ | ||
return function (string $cepWithLeftPad) { | ||
$promises = array_merge( | ||
ViaCepProvider::createPromiseArray($cepWithLeftPad), | ||
CepAbertoProvider::createPromiseArray($cepWithLeftPad), | ||
CorreiosProvider::createPromiseArray($cepWithLeftPad) | ||
); | ||
|
||
return Promise\any($promises); | ||
}; | ||
} | ||
|
||
private static function handleProvidersError() | ||
private function validateInputType() | ||
{ | ||
return function (Exception $onRejected) { | ||
if ($onRejected instanceof Promise\AggregateException) { | ||
throw new CepPromiseException( | ||
'Todos os serviços de CEP retornaram erro.', | ||
self::ERROR_PROVIDER_CODE, | ||
$onRejected->getReason() | ||
); | ||
return function ($cepRawValue) { | ||
if (is_string($cepRawValue) || is_int($cepRawValue)) { | ||
return $cepRawValue; | ||
} | ||
|
||
throw $onRejected; | ||
}; | ||
} | ||
|
||
private static function leftPadWithZeros() | ||
{ | ||
return function (string $cepCleanValue) { | ||
return str_pad($cepCleanValue, self::CEP_SIZE, '0', STR_PAD_LEFT); | ||
throw new CepPromiseException( | ||
'Erro ao inicializar a instância do CepPromise.', | ||
self::ERROR_VALIDATION_CODE, | ||
[ | ||
[ | ||
'message' => 'Você deve informar o CEP utilizando uma string ou um inteiro.', | ||
'service' => 'cep_validation', | ||
], | ||
] | ||
); | ||
}; | ||
} | ||
|
||
private static function removeSpecialCharacters() | ||
private function removeSpecialCharacters() | ||
{ | ||
return function (string $cepRawValue) { | ||
return preg_replace('/\D+/', '', $cepRawValue); | ||
}; | ||
} | ||
|
||
private static function throwApplicationError() | ||
{ | ||
return function (Exception $exception) { | ||
throw new CepPromiseException( | ||
$exception->getMessage(), | ||
$exception->getCode(), | ||
$exception->getErrors() ?? [] | ||
); | ||
}; | ||
} | ||
|
||
private static function validateInputLength() | ||
private function validateInputLength() | ||
{ | ||
return function (string $cepNumbers) { | ||
if (strlen($cepNumbers) <= self::CEP_SIZE) { | ||
|
@@ -121,22 +105,48 @@ private static function validateInputLength() | |
}; | ||
} | ||
|
||
private static function validateInputType() | ||
private function leftPadWithZeros() | ||
{ | ||
return function ($cepRawValue) { | ||
if (is_string($cepRawValue) || is_int($cepRawValue)) { | ||
return $cepRawValue; | ||
return function (string $cepSanitized) { | ||
return str_pad($cepSanitized, self::CEP_SIZE, '0', STR_PAD_LEFT); | ||
}; | ||
} | ||
|
||
private function fetchCepFromProviders() | ||
{ | ||
return function (string $cepWithLeftPad) { | ||
$promises = array_merge( | ||
ViaCepProvider::createPromiseArray($cepWithLeftPad), | ||
CepAbertoProvider::createPromiseArray($cepWithLeftPad), | ||
CorreiosProvider::createPromiseArray($cepWithLeftPad) | ||
); | ||
|
||
return Promise\any($promises); | ||
}; | ||
} | ||
|
||
private function handleProvidersError() | ||
{ | ||
return function (Exception $onRejected) { | ||
if ($onRejected instanceof Promise\AggregateException) { | ||
throw new CepPromiseException( | ||
'Todos os serviços de CEP retornaram erro.', | ||
self::ERROR_PROVIDER_CODE, | ||
$onRejected->getReason() | ||
); | ||
} | ||
|
||
throw $onRejected; | ||
}; | ||
} | ||
|
||
private function throwApplicationError() | ||
{ | ||
return function (Exception $exception) { | ||
throw new CepPromiseException( | ||
'Erro ao inicializar a instância do CepPromise.', | ||
self::ERROR_VALIDATION_CODE, | ||
[ | ||
[ | ||
'message' => 'Você deve informar o CEP utilizando uma string ou um inteiro.', | ||
'service' => 'cep_validation', | ||
], | ||
] | ||
$exception->getMessage(), | ||
$exception->getCode(), | ||
$exception->getErrors() ?? [] | ||
); | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.