From 1e3d0b0d4382068dce2f35fb7a092b9653621997 Mon Sep 17 00:00:00 2001 From: Josantonius Date: Mon, 8 Oct 2018 19:43:45 +0200 Subject: [PATCH 1/2] Test branch --- test | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test diff --git a/test b/test new file mode 100644 index 0000000..e69de29 From e1ce64b7b4e6ae72ad7dad6aadac1283b5b13b3a Mon Sep 17 00:00:00 2001 From: Josantonius Date: Mon, 8 Oct 2018 19:50:00 +0200 Subject: [PATCH 2/2] library restructuring --- README.md | 1 - example.php | 39 ++++---------- src/CryptoMonitor.php | 38 +++++++++++--- src/PayCrypto.php | 89 ++++++++++++++++++++++++++++++++ src/Resource/GetAllRate.php | 25 ++++----- src/Resource/GetAssets.php | 20 ++++--- src/Resource/GetSpecificRate.php | 24 +++------ test | 0 8 files changed, 156 insertions(+), 80 deletions(-) create mode 100644 src/PayCrypto.php delete mode 100644 test diff --git a/README.md b/README.md index 82ce5ac..79b6c7e 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,3 @@ Monitor the crypto curreny via [CoinAPI](https://docs.coinapi.io/). - [License](#License) - [Copyright](#Copyright) - diff --git a/example.php b/example.php index 60dd554..b22ee4f 100644 --- a/example.php +++ b/example.php @@ -1,37 +1,22 @@ set(CryptoMonitor::class, new CryptoMonitor(new CryptoConnector($apiKey))); -$porter = new Porter($container); -$specificRate = new GetSpecificRate($apiKey, 'BTC', 'USD'); +$payCrypto = new PayCrypto($provider, $apiKey); -$rates = $porter->importOne(new ImportSpecification($specificRate)); +// Get specific rate +$rates = $payCrypto->getSpecificRate('ETH', 'EUR'); var_dump($rates); // Get all rates -$container = new Container; -$container->set(CryptoMonitor::class, new CryptoMonitor(new CryptoConnector($apiKey))); -$porter = new Porter($container); -$specificRate = new GetAllRate($apiKey, 'BTC'); - -$rates = $porter->import(new ImportSpecification($specificRate)); +$rates = $payCrypto->getAllRate('ETH'); foreach ($rates as $rateRecord) { echo $rateRecord['asset_id_quote'] . PHP_EOL; @@ -42,18 +27,12 @@ // Get the base id echo PHP_EOL; - $rates = $rates->findFirstCollection(); -echo $rates->getBase(); //BTC +echo $rates->getBase(); //ETH // Get Assets -$container = new Container; -$container->set(CryptoMonitor::class, new CryptoMonitor(new CryptoConnector($apiKey))); -$porter = new Porter($container); -$assets = new GetAssets($apiKey); - -$assets = $porter->import(new ImportSpecification($assets)); +$assets = $payCrypto->getAssets(); foreach ($assets as $assetRecord) { var_dump($assetRecord['asset_id']); diff --git a/src/CryptoMonitor.php b/src/CryptoMonitor.php index 2a8c214..0817678 100644 --- a/src/CryptoMonitor.php +++ b/src/CryptoMonitor.php @@ -1,28 +1,54 @@ [ + 'url' => 'https://rest.coinapi.io/v1/', + 'specific' => 'exchangerate/%s/%s/', + 'all' => 'exchangerate/%s/', + 'assets' => 'assets/' + ] + ]; + public function __construct(Connector $connector) { $this->connector = $connector; } - public static function buildExchangeApiUrl(string $url): string + public static function buildExchangeApiUrl(PayCrypto $config) : string { - return self::EXCHANGE_API_ENDPOINT . $url; + $url = self::$providers[$config->provider]['url']; + + $params = self::$providers[$config->provider][$config->type]; + + switch ($config->type) { + case 'specific': + $url .= sprintf($params, $config->base, $config->quote); + break; + case 'all': + $url .= sprintf($params, $config->base); + break; + case 'assets': + $url .= $params; + break; + default: + break; + } + + return $url; } - public function getConnector(): Connector + public function getConnector() : Connector { return $this->connector; } diff --git a/src/PayCrypto.php b/src/PayCrypto.php new file mode 100644 index 0000000..60d915f --- /dev/null +++ b/src/PayCrypto.php @@ -0,0 +1,89 @@ +provider = $provider; + $this->apiKey = $apiKey; + + $this->porter = new Porter( + $this->setContainer() + ); + } + + public function getSpecificRate(string $base = 'BTC', string $quote = 'USD') : array + { + $this->base = $base; + $this->quote = $quote; + $this->type = 'specific'; + + return $this->porter->importOne( + new ImportSpecification( + new GetSpecificRate($this) + ) + ); + } + + public function getAllRate(string $base = 'BTC') : object + { + $this->base = $base; + $this->type = 'all'; + + return $this->porter->import( + new ImportSpecification( + new GetAllRate($this) + ) + ); + } + + public function getAssets() : object + { + $this->type = 'assets'; + + return $this->porter->import( + new ImportSpecification( + new GetAssets($this) + ) + ); + } + + public function getConnector() : Connector + { + return $this->connector; + } + + private function setContainer() + { + $container = new Container; + + return $container->set( + CryptoMonitor::class, + new CryptoMonitor( + new CryptoConnector($this->apiKey) + ) + ); + } +} diff --git a/src/Resource/GetAllRate.php b/src/Resource/GetAllRate.php index 2e4cfe9..bcb7538 100644 --- a/src/Resource/GetAllRate.php +++ b/src/Resource/GetAllRate.php @@ -3,6 +3,7 @@ namespace PayCrypto\Resource; +use PayCrypto\PayCrypto; use PayCrypto\CryptoMonitor; use PayCrypto\Collection\AllRatesRecord; use ScriptFUSION\Porter\Connector\ImportConnector; @@ -12,14 +13,11 @@ class GetAllRate implements ProviderResource { - private $apiKey; + public $config; - private $base; - - public function __construct(string $apiKey, string $base = 'BTC') + public function __construct(PayCrypto $config) { - $this->apiKey = $apiKey; - $this->base = $base; + $this->config = $config; } public function getProviderClassName(): string @@ -29,17 +27,14 @@ public function getProviderClassName(): string public function fetch(ImportConnector $connector): \Iterator { - $response = \json_decode( - (string) $connector->fetch( - CryptoMonitor::buildExchangeApiUrl( - sprintf("v1/exchangerate/%s", $this->base) - ) - ), - true + $response = (string) $connector->fetch( + CryptoMonitor::buildExchangeApiUrl($this->config) ); - $rates = new \ArrayIterator($response['rates']); + $data = json_decode($response, true); + + $rates = new \ArrayIterator($data['rates']); - return new AllRatesRecord($rates, $this->base, count($rates), $this); + return new AllRatesRecord($rates, $this->config->base, count($rates), $this); } } diff --git a/src/Resource/GetAssets.php b/src/Resource/GetAssets.php index 3dfa47e..04c8369 100644 --- a/src/Resource/GetAssets.php +++ b/src/Resource/GetAssets.php @@ -3,6 +3,7 @@ namespace PayCrypto\Resource; +use PayCrypto\PayCrypto; use PayCrypto\CryptoMonitor; use ScriptFUSION\Porter\Collection\CountableProviderRecords; use ScriptFUSION\Porter\Connector\ImportConnector; @@ -12,11 +13,11 @@ class GetAssets implements ProviderResource { - private $apiKey; + public $config; - public function __construct(string $apiKey) + public function __construct(PayCrypto $config) { - $this->apiKey = $apiKey; + $this->config = $config; } public function getProviderClassName(): string @@ -26,16 +27,13 @@ public function getProviderClassName(): string public function fetch(ImportConnector $connector): \Iterator { - $response = \json_decode( - (string) $connector->fetch( - CryptoMonitor::buildExchangeApiUrl( - 'v1/assets' - ) - ), - true + $response = (string) $connector->fetch( + CryptoMonitor::buildExchangeApiUrl($this->config) ); - $assets = new \ArrayIterator($response); + $data = json_decode($response, true); + + $assets = new \ArrayIterator($data); return new CountableProviderRecords($assets, count($assets), $this); } diff --git a/src/Resource/GetSpecificRate.php b/src/Resource/GetSpecificRate.php index ba0dc44..fc6ffe3 100644 --- a/src/Resource/GetSpecificRate.php +++ b/src/Resource/GetSpecificRate.php @@ -3,6 +3,7 @@ namespace PayCrypto\Resource; +use PayCrypto\PayCrypto; use PayCrypto\CryptoMonitor; use PayCrypto\Collection\SpecificRateRecord; use ScriptFUSION\Porter\Connector\ImportConnector; @@ -12,17 +13,11 @@ class GetSpecificRate implements ProviderResource { - private $apiKey; + public $config; - private $base; - - private $quote; - - public function __construct(string $apiKey, string $base = 'BTC', string $quote = 'USD') + public function __construct(PayCrypto $config) { - $this->apiKey = $apiKey; - $this->base = $base; - $this->quote = $quote; + $this->config = $config; } public function getProviderClassName(): string @@ -32,15 +27,10 @@ public function getProviderClassName(): string public function fetch(ImportConnector $connector): \Iterator { - $response = \json_decode( - (string) $connector->fetch( - CryptoMonitor::buildExchangeApiUrl( - sprintf("v1/exchangerate/%s/%s", $this->base, $this->quote) - ) - ), - true + $json = (string) $connector->fetch( + CryptoMonitor::buildExchangeApiUrl($this->config) ); - yield $response; + yield json_decode($json, true); } } diff --git a/test b/test deleted file mode 100644 index e69de29..0000000