Skip to content

Commit

Permalink
Merge pull request #519 from wizaplace/develop
Browse files Browse the repository at this point in the history
1.63.0
  • Loading branch information
steevanb authored Jun 5, 2019
2 parents 8b0cc39 + a523a9d commit f518e59
Show file tree
Hide file tree
Showing 22 changed files with 1,535 additions and 83 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## 1.63.0

### New features

- Added `Wizaplace\SDK\Currency\CurrencyService::getAll`
- Added `Wizaplace\SDK\Currency\CurrencyService::getByFilters`
- Added `Wizaplace\SDK\Currency\CurrencyService::getByCountryCode`
- Added `Wizaplace\SDK\Currency\CurrencyService::getCurrency`
- Added `Wizaplace\SDK\Currency\CurrencyService::getCountries`
- Added `Wizaplace\SDK\Currency\CurrencyService::addCountry`
- Added `Wizaplace\SDK\Currency\CurrencyService::removeCountry`
- Added `Wizaplace\SDK\Currency\CurrencyService::updateCurrency`
- Added `\Wizaplace\SDK\User\UserService::patchUser`
- Updated `\Wizaplace\SDK\User\User` Add 'currencyCode' to user profile
- Updated `\Wizaplace\SDK\User\UserService::updateUser` Add 'currencyCode' property

## 1.62.0

Expand Down
106 changes: 106 additions & 0 deletions src/Currency/Currency.php
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;
}
}
34 changes: 34 additions & 0 deletions src/Currency/CurrencyCountries.php
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;
}
}
182 changes: 182 additions & 0 deletions src/Currency/CurrencyService.php
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;
}
}
}
}
22 changes: 22 additions & 0 deletions src/User/UpdateUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ final class UpdateUserCommand
/** @var \DateTimeInterface|null */
private $birthday;

/** @var string|null */
private $currencyCode;

/**
* @return int
*/
Expand Down Expand Up @@ -81,6 +84,14 @@ public function getBirthday(): ?\DateTimeInterface
return $this->birthday;
}

/**
* @return string|null
*/
public function getCurrencyCode(): ?string
{
return $this->currencyCode;
}

/**
* @param int $userId
*
Expand Down Expand Up @@ -153,6 +164,17 @@ public function setBirthday(?\DateTimeInterface $birthday): self
return $this;
}

/**
* @param string $currencyCode
* @return UpdateUserCommand
*/
public function setCurrencyCode(string $currencyCode): self
{
$this->currencyCode = $currencyCode;

return $this;
}

/**
* @throws SomeParametersAreInvalid
*/
Expand Down
Loading

0 comments on commit f518e59

Please sign in to comment.