Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crypto monitor josantonius #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ Monitor the crypto curreny via [CoinAPI](https://docs.coinapi.io/).
- [License](#License)
- [Copyright](#Copyright)


39 changes: 9 additions & 30 deletions example.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

use Joomla\DI\Container;
use ScriptFUSION\Porter\Porter;
use PayCrypto\CryptoMonitor;
use PayCrypto\Connector\CryptoConnector;
use PayCrypto\Resource\GetSpecificRate;
use PayCrypto\Resource\GetAllRate;
use PayCrypto\Resource\GetAssets;
use ScriptFUSION\Porter\Specification\ImportSpecification;
use PayCrypto\PayCrypto;

// Get specific rate
$provider = 'coinapi';

$apiKey = 'your-coin-api-key';
$apiKey = '4E861687-19D6-4894-87B9-E785B1EE3900';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this API key free-trial? If it's, I think it's fine to expose this key here.


$container = new Container;
$container->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;
Expand All @@ -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']);
Expand Down
38 changes: 32 additions & 6 deletions src/CryptoMonitor.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
<?php
declare(strict_types=1);

declare(strict_types=1);
namespace PayCrypto;

use PayCrypto\PayCrypto;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Provider\Provider;

class CryptoMonitor implements Provider
{
public const EXCHANGE_API_ENDPOINT = 'https://rest.coinapi.io/';

private $connector;

private static $providers = [
'coinapi' => [
'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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about enhancing this type condition?

I think using the switch...case is not the proper way to do.

If we have the many types to do the different action, the swittch condition will be very long.

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;
}
Expand Down
89 changes: 89 additions & 0 deletions src/PayCrypto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);
namespace PayCrypto;

use Joomla\DI\Container;
use PayCrypto\Connector\CryptoConnector;
use PayCrypto\CryptoMonitor;
use PayCrypto\Resource\GetAllRate;
use PayCrypto\Resource\GetAssets;
use PayCrypto\Resource\GetSpecificRate;
use ScriptFUSION\Porter\Porter;
use ScriptFUSION\Porter\Specification\ImportSpecification;

class PayCrypto
{
public $provider;

public $apiKey;

public $porter;

public $base;

public $quote;

public function __construct(string $provider, string $apiKey)
{
$this->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)
)
);
}
}
25 changes: 10 additions & 15 deletions src/Resource/GetAllRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace PayCrypto\Resource;

use PayCrypto\PayCrypto;
use PayCrypto\CryptoMonitor;
use PayCrypto\Collection\AllRatesRecord;
use ScriptFUSION\Porter\Connector\ImportConnector;
Expand All @@ -12,14 +13,11 @@

class GetAllRate implements ProviderResource
{
private $apiKey;
public $config;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let $config be public and it means we can modify this at any time from this class instance.

How about using the private/protected $config?

And we can also add the setConfig and getConfig to allow us to modify this private/protected $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
Expand All @@ -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);
}
}
20 changes: 9 additions & 11 deletions src/Resource/GetAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace PayCrypto\Resource;

use PayCrypto\PayCrypto;
use PayCrypto\CryptoMonitor;
use ScriptFUSION\Porter\Collection\CountableProviderRecords;
use ScriptFUSION\Porter\Connector\ImportConnector;
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
24 changes: 7 additions & 17 deletions src/Resource/GetSpecificRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace PayCrypto\Resource;

use PayCrypto\PayCrypto;
use PayCrypto\CryptoMonitor;
use PayCrypto\Collection\SpecificRateRecord;
use ScriptFUSION\Porter\Connector\ImportConnector;
Expand All @@ -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
Expand All @@ -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);
}
}