-
Notifications
You must be signed in to change notification settings - Fork 8
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 #6 from bavix/develop
exceptions
- Loading branch information
Showing
14 changed files
with
291 additions
and
27 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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap; | ||
|
||
use Bavix\WalletSwap\Exception\CacheException; | ||
use Bavix\WalletSwap\Exception\NonBreakingInvalidArgumentException; | ||
use Bavix\WalletSwap\Exception\SwapException; | ||
use Bavix\WalletSwap\Exception\SwapRuntimeException; | ||
use Bavix\WalletSwap\Exception\UnsupportedCurrencyPairException; | ||
use Bavix\WalletSwap\Exception\UnsupportedDateException; | ||
use Bavix\WalletSwap\Exception\UnsupportedExchangeQueryException; | ||
use Exchanger\Exception\CacheException as ExchangerCacheException; | ||
use Exchanger\Exception\Exception as ExchangerException; | ||
use Exchanger\Exception\NonBreakingInvalidArgumentException as ExchangerNonBreakingInvalidArgumentException; | ||
use Exchanger\Exception\UnsupportedCurrencyPairException as ExchangerUnsupportedCurrencyPairException; | ||
use Exchanger\Exception\UnsupportedDateException as ExchangerUnsupportedDateException; | ||
use Exchanger\Exception\UnsupportedExchangeQueryException as ExchangerUnsupportedExchangeQueryException; | ||
use Swap\Swap; | ||
|
||
final class CurrencyService implements CurrencyServiceInterface | ||
{ | ||
private Swap $swapService; | ||
|
||
public function __construct(Swap $swapService) | ||
{ | ||
$this->swapService = $swapService; | ||
} | ||
|
||
/** | ||
* @throws CacheException | ||
* @throws NonBreakingInvalidArgumentException | ||
* @throws UnsupportedExchangeQueryException | ||
* @throws UnsupportedCurrencyPairException | ||
* @throws UnsupportedDateException | ||
* @throws SwapException | ||
* @throws SwapRuntimeException | ||
*/ | ||
public function rate(string $fromCurrency, string $toCurrency): string | ||
{ | ||
if ($fromCurrency === $toCurrency) { | ||
return '1'; | ||
} | ||
|
||
try { | ||
return (string) $this->swapService | ||
->latest($fromCurrency.'/'.$toCurrency) | ||
->getValue() | ||
; | ||
} catch (ExchangerCacheException $exception) { | ||
throw new CacheException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (ExchangerNonBreakingInvalidArgumentException $exception) { | ||
throw new NonBreakingInvalidArgumentException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (ExchangerUnsupportedExchangeQueryException $exception) { | ||
throw new UnsupportedExchangeQueryException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (ExchangerUnsupportedCurrencyPairException $exception) { | ||
throw new UnsupportedCurrencyPairException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (ExchangerUnsupportedDateException $exception) { | ||
throw new UnsupportedDateException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (ExchangerException $exception) { | ||
throw new SwapException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (\Throwable $exception) { | ||
throw new SwapRuntimeException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap; | ||
|
||
use Bavix\WalletSwap\Exception\CacheException; | ||
use Bavix\WalletSwap\Exception\NonBreakingInvalidArgumentException; | ||
use Bavix\WalletSwap\Exception\SwapException; | ||
use Bavix\WalletSwap\Exception\SwapRuntimeException; | ||
use Bavix\WalletSwap\Exception\UnsupportedCurrencyPairException; | ||
use Bavix\WalletSwap\Exception\UnsupportedDateException; | ||
use Bavix\WalletSwap\Exception\UnsupportedExchangeQueryException; | ||
|
||
interface CurrencyServiceInterface | ||
{ | ||
/** | ||
* @throws CacheException | ||
* @throws NonBreakingInvalidArgumentException | ||
* @throws UnsupportedExchangeQueryException | ||
* @throws UnsupportedCurrencyPairException | ||
* @throws UnsupportedDateException | ||
* @throws SwapException | ||
* @throws SwapRuntimeException | ||
*/ | ||
public function rate(string $fromCurrency, string $toCurrency): string; | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use Exception; | ||
|
||
final class CacheException extends Exception implements ExceptionInterface | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use InvalidArgumentException; | ||
|
||
final class NonBreakingInvalidArgumentException extends InvalidArgumentException implements ExceptionInterface | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use Exception; | ||
|
||
final class SwapException extends Exception implements ExceptionInterface | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use RuntimeException; | ||
|
||
final class SwapRuntimeException extends RuntimeException implements ExceptionInterface | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use Exception; | ||
|
||
final class UnsupportedCurrencyPairException extends Exception implements ExceptionInterface | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use Exception; | ||
|
||
final class UnsupportedDateException extends Exception implements ExceptionInterface | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Exception; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use Exception; | ||
|
||
final class UnsupportedExchangeQueryException extends Exception implements ExceptionInterface | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\WalletSwap\Test; | ||
|
||
use Bavix\WalletSwap\CurrencyService; | ||
use Bavix\WalletSwap\Exception\CacheException; | ||
use Bavix\WalletSwap\Exception\NonBreakingInvalidArgumentException; | ||
use Bavix\WalletSwap\Exception\SwapException; | ||
use Bavix\WalletSwap\Exception\SwapRuntimeException; | ||
use Bavix\WalletSwap\Exception\UnsupportedCurrencyPairException; | ||
use Bavix\WalletSwap\Exception\UnsupportedDateException; | ||
use Bavix\WalletSwap\Exception\UnsupportedExchangeQueryException; | ||
use DateTimeImmutable; | ||
use Exchanger\CurrencyPair; | ||
use Exchanger\Exception\CacheException as ExchangerCacheException; | ||
use Exchanger\Exception\ChainException; | ||
use Exchanger\Exception\Exception as ExchangerException; | ||
use Exchanger\Exception\NonBreakingInvalidArgumentException as ExchangerNonBreakingInvalidArgumentException; | ||
use Exchanger\Exception\UnsupportedCurrencyPairException as ExchangerUnsupportedCurrencyPairException; | ||
use Exchanger\Exception\UnsupportedDateException as ExchangerUnsupportedDateException; | ||
use Exchanger\Exception\UnsupportedExchangeQueryException as ExchangerUnsupportedExchangeQueryException; | ||
use Exchanger\ExchangeRateQuery; | ||
use Exchanger\Service\PhpArray; | ||
use RuntimeException; | ||
use Swap\Swap; | ||
use Throwable; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CurrencyTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider exceptionDataProvider | ||
*/ | ||
public function testExceptions(string $expect, Throwable $throwable): void | ||
{ | ||
$this->expectException($expect); | ||
|
||
$mockSwapService = $this->createMock(Swap::class); | ||
$mockSwapService->method('latest')->willThrowException($throwable); | ||
|
||
$currencyService = new CurrencyService($mockSwapService); | ||
$currencyService->rate('USD', 'RUB'); | ||
} | ||
|
||
public function exceptionDataProvider(): iterable | ||
{ | ||
$currencyPair = new CurrencyPair('USD', 'EUR'); | ||
$service = new PhpArray([]); | ||
|
||
yield [CacheException::class, new ExchangerCacheException()]; | ||
yield [NonBreakingInvalidArgumentException::class, new ExchangerNonBreakingInvalidArgumentException()]; | ||
|
||
yield [ | ||
UnsupportedExchangeQueryException::class, | ||
new ExchangerUnsupportedExchangeQueryException(new ExchangeRateQuery($currencyPair), $service), | ||
]; | ||
|
||
yield [ | ||
UnsupportedCurrencyPairException::class, | ||
new ExchangerUnsupportedCurrencyPairException($currencyPair, $service), | ||
]; | ||
|
||
yield [ | ||
UnsupportedDateException::class, | ||
new ExchangerUnsupportedDateException(new DateTimeImmutable(), $service), | ||
]; | ||
|
||
yield [ | ||
SwapException::class, | ||
new ExchangerException(), | ||
]; | ||
|
||
yield [ | ||
SwapException::class, | ||
new ChainException([]), | ||
]; | ||
|
||
yield [ | ||
SwapRuntimeException::class, | ||
new RuntimeException(), | ||
]; | ||
} | ||
} |
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