Skip to content

Commit

Permalink
Filesystem cache added
Browse files Browse the repository at this point in the history
  • Loading branch information
merorafael committed Jun 3, 2017
1 parent 14c8a73 commit 0be1db4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require": {
"php": ">=5.4.9",
"ext-soap": "*"
"ext-soap": "*",
"symfony/cache": "~2.8|~3.0|~4.0"
},
"require-dev": {
"satooshi/php-coveralls": "~0.6.1",
Expand Down
42 changes: 26 additions & 16 deletions src/Mero/Correios/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mero\Correios\Exception\AddressNotFoundException;
use Mero\Correios\Exception\InvalidZipCodeException;
use Mero\Correios\Model\Address;
use Symfony\Component\Cache\Simple\FilesystemCache;

class Client
{
Expand Down Expand Up @@ -36,24 +37,33 @@ public function findAddressByZipCode($zipCode)
if (strlen($zipCode) != 8) {
throw new InvalidZipCodeException('The brazilian zip code should have exacly 8 characters.');
}
try {
$wsdlConnection = $this->createWsdlConnection();
$address = $wsdlConnection->__soapCall('consultaCEP', [
'consultaCEP' => [
'cep' => $zipCode
]
]);

return new Address(
$address->return->end,
$address->return->bairro,
$address->return->cidade,
$address->return->uf,
$address->return->cep
);
} catch (\SoapFault $e) {
throw new AddressNotFoundException($e->getMessage());
$cache = new FilesystemCache();
if (!$cache->has("mero_correios.{$zipCode}")) {
try {
$wsdlConnection = $this->createWsdlConnection();
$address = $wsdlConnection->__soapCall('consultaCEP', [
'consultaCEP' => [
'cep' => $zipCode
]
]);

$address = new Address(
$address->return->end,
$address->return->bairro,
$address->return->cidade,
$address->return->uf,
$address->return->cep
);
$cache->set("mero_correios.{$zipCode}", $address);

return $address;
} catch (\SoapFault $e) {
throw new AddressNotFoundException($e->getMessage());
}
}

return $cache->get("mero_correios.{$zipCode}");
}

/**
Expand Down

0 comments on commit 0be1db4

Please sign in to comment.