-
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.
Merge pull request #519 from wizaplace/develop
1.63.0
- Loading branch information
Showing
22 changed files
with
1,535 additions
and
83 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
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* @author Wizacha DevTeam <[email protected]> | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Wizaplace\SDK\Currency; | ||
|
||
/** | ||
* Class Currency | ||
* @package Wizaplace\SDK\Currency | ||
*/ | ||
final class Currency | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $code; | ||
|
||
/** | ||
* @var ?float | ||
*/ | ||
private $exchangeRate; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $enabled; | ||
|
||
/** | ||
* @var ?string | ||
*/ | ||
private $symbol; | ||
|
||
/** | ||
* @var CurrencyCountries[] | ||
*/ | ||
private $countryCodes; | ||
|
||
|
||
public function __construct(array $data) | ||
{ | ||
$this->setCode($data['code']); | ||
$this->setEnabled($data['enabled']); | ||
$this->setExchangeRate($data['exchangeRate'] ?? null); | ||
$this->setSymbol($data['symbol'] ?? null); | ||
$this->countryCodes = array_map(function (array $itemData): CurrencyCountries { | ||
return new CurrencyCountries($itemData); | ||
}, $data['countries']); | ||
} | ||
|
||
public function setExchangeRate(?float $exchangeRate): self | ||
{ | ||
$this->exchangeRate = $exchangeRate; | ||
|
||
return $this; | ||
} | ||
|
||
public function setEnabled(bool $enabled): self | ||
{ | ||
$this->enabled = $enabled; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCode(): string | ||
{ | ||
return $this->code; | ||
} | ||
|
||
public function getExchangeRate(): ?float | ||
{ | ||
return $this->exchangeRate; | ||
} | ||
|
||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function getSymbol(): ?string | ||
{ | ||
return $this->symbol; | ||
} | ||
|
||
public function getCountryCodes(): array | ||
{ | ||
return $this->countryCodes; | ||
} | ||
|
||
protected function setCode(string $code): self | ||
{ | ||
$this->code = $code; | ||
|
||
return $this; | ||
} | ||
|
||
protected function setSymbol(?string $symbol): self | ||
{ | ||
$this->symbol = $symbol; | ||
|
||
return $this; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* @author Wizacha DevTeam <[email protected]> | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Wizaplace\SDK\Currency; | ||
|
||
final class CurrencyCountries | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $countryCode; | ||
|
||
public function __construct(array $data) | ||
{ | ||
$this->countryCode = $data['code']; | ||
} | ||
|
||
public function setCountryCode(string $countryCode): self | ||
{ | ||
$this->countryCode = $countryCode; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCountryCode(): string | ||
{ | ||
return $this->countryCode; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
/** | ||
* @author Wizacha DevTeam <[email protected]> | ||
* @copyright Copyright (c) Wizacha | ||
* @license Proprietary | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Wizaplace\SDK\Currency; | ||
|
||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\RequestOptions; | ||
use Wizaplace\SDK\AbstractService; | ||
use Wizaplace\SDK\Exception\AccessDenied; | ||
use Wizaplace\SDK\Exception\NotFound; | ||
use Wizaplace\SDK\Exception\SomeParametersAreInvalid; | ||
|
||
class CurrencyService extends AbstractService | ||
{ | ||
/** @return Currency[] */ | ||
public function getAll(): array | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
try { | ||
$currencies = $this->client->get('currencies'); | ||
|
||
return array_map( | ||
function (array $data): Currency { | ||
return new Currency($data); | ||
}, | ||
$currencies | ||
); | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
/** @return CurrencyCountries[] */ | ||
public function getCountries(string $currencyCode): array | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
try { | ||
$currencyCountriesData = $this->client->get('currencies/'.$currencyCode.'/countries'); | ||
$data = []; | ||
foreach ($currencyCountriesData as $code) { | ||
$data[] = new CurrencyCountries($code); | ||
} | ||
|
||
return $data; | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
case 404: | ||
throw new NotFound("Currency '$currencyCode' not found."); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
public function addCountry(string $currencyCode, string $countryCode): array | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
|
||
try { | ||
return $this->client->post('currencies/'.$currencyCode.'/countries', [ | ||
RequestOptions::FORM_PARAMS => [ | ||
'countryCode' => $countryCode, | ||
], | ||
]); | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
case 404: | ||
throw new NotFound("Currency '$currencyCode' not found."); | ||
case 400: | ||
throw new SomeParametersAreInvalid("CountryCode '".$countryCode."' already exist for currency '".$currencyCode."'."); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
public function removeCountry(string $currencyCode, string $countryCode): self | ||
{ | ||
try { | ||
$this->client->mustBeAuthenticated(); | ||
$this->client->delete("currencies/{$currencyCode}/countries/{$countryCode}"); | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
case 404: | ||
throw new NotFound("Currency '$currencyCode' not found."); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** @return Currency[] */ | ||
public function getByFilters(array $filters): array | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
try { | ||
$currencies = $this->client->get('currencies?'.http_build_query($filters)); | ||
|
||
return array_map( | ||
function (array $data): Currency { | ||
return new Currency($data); | ||
}, | ||
$currencies | ||
); | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
public function getByCountryCode(string $code): ?Currency | ||
{ | ||
$currencies = $this->getByFilters(['countryCode' => $code]); | ||
|
||
return count($currencies) > 0 ? array_shift($currencies) : null; | ||
} | ||
|
||
public function updateCurrency(Currency $currency): array | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
|
||
try { | ||
return $this->client->patch("currencies/{$currency->getCode()}", [ | ||
RequestOptions::JSON => [ | ||
'enabled' => $currency->isEnabled(), | ||
'exchangeRate' => $currency->getExchangeRate(), | ||
], | ||
|
||
]); | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
case 404: | ||
throw new NotFound("Currency '{$currency->getCode()}' not found."); | ||
case 400: | ||
throw new SomeParametersAreInvalid($e->getMessage()); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
public function getCurrency(string $currencyCode): Currency | ||
{ | ||
$this->client->mustBeAuthenticated(); | ||
try { | ||
return new Currency($this->client->get('currencies/'.$currencyCode)); | ||
} catch (ClientException $e) { | ||
switch ($e->getResponse()->getStatusCode()) { | ||
case 403: | ||
throw new AccessDenied("You must be authenticated as an admin."); | ||
case 404: | ||
throw new NotFound("Currency '$currencyCode' not found."); | ||
default: | ||
throw $e; | ||
} | ||
} | ||
} | ||
} |
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.