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

Approve code review #6

Merged
merged 7 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/Collection/AllRatesRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,4 @@ public function getBase(): string
{
return $this->base;
}

public function toAssociativeArray(): array
{
return iterator_to_array($this);
}
}
23 changes: 0 additions & 23 deletions src/Collection/SpecificRateRecord.php

This file was deleted.

19 changes: 5 additions & 14 deletions src/Resource/GetAllRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class GetAllRate implements ProviderResource
{
private $apiKey;

public $base = 'BTC';
private $base;

public function __construct(string $apiKey)
public function __construct(string $apiKey, string $base = 'BTC')
{
$this->apiKey = $apiKey;
$this->base = $base;
}

public function getProviderClassName(): string
Expand All @@ -37,18 +38,8 @@ public function fetch(ImportConnector $connector): \Iterator
true
);

$rates = $response['rates'];
$rates = new \ArrayIterator($response['rates']);

$rate = function () use ($rates) {
foreach ($rates as $key => $value) {
yield [
'asset_id_quote' => $rates[$key]['asset_id_quote'],
'rate' => $rates[$key]['rate'],
'time' => $rates[$key]['time'],
];
}
};

return new AllRatesRecord($rate(), $response['asset_id_base'], count($rates), $this);
return new AllRatesRecord($rates, $this->base, count($rates), $this);
}
}
19 changes: 6 additions & 13 deletions src/Resource/GetSpecificRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class GetSpecificRate implements ProviderResource
{
private $apiKey;

public $base = 'BTC';
private $base;

public $quote = 'USD';
private $quote;

public function __construct(string $apiKey)
public function __construct(string $apiKey, string $base = 'BTC', string $quote = 'USD')
{
$this->apiKey = $apiKey;
$this->base = $base;
$this->quote = $quote;
}

public function getProviderClassName(): string
Expand All @@ -39,15 +41,6 @@ public function fetch(ImportConnector $connector): \Iterator
true
);

$rate = function () use ($response) {
yield [
'asset_id_base' => $response['asset_id_base'],
'asset_id_quote' => $response['asset_id_quote'],
'rate' => $response['rate'],
'time' => $response['time'],
];
};

return new SpecificRateRecord($rate(), count($response), $this);
yield $response;
}
}
31 changes: 14 additions & 17 deletions tests/Functional/GetRateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,28 @@ final class GetRateTest extends TestCase

public function testGetSpecificRateRecords()
{
/** @var SpecificRateRecord $rates */
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey)))
->findFirstCollection();
$rate = FixtureFactory::createPorter()->importOne(new ImportSpecification(new GetSpecificRate($this->apiKey)));

$rateRecords = $rates->toAssociativeArray();

$this->assertCount(1, $rateRecords);
$this->assertArrayHasKey('asset_id_base', $rateRecords[0]);
$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]);
$this->assertArrayHasKey('rate', $rateRecords[0]);
$this->assertArrayHasKey('time', $rateRecords[0]);
$this->assertArrayHasKey('asset_id_base', $rate);
$this->assertArrayHasKey('asset_id_quote', $rate);
$this->assertArrayHasKey('rate', $rate);
$this->assertArrayHasKey('time', $rate);
}

public function testGetAllRateRecords()
{
/** @var AllRatesRecord $rates */
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey)))
->findFirstCollection();
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey)));

$this->assertSame('BTC', $rates->getBase());
$this->assertGreaterThan(1000, count($rates));

$rateRecords = $rates->toAssociativeArray();
foreach ($rates as $rateRecord) {
$this->assertArrayHasKey('asset_id_quote', $rateRecord);
$this->assertArrayHasKey('rate', $rateRecord);
$this->assertArrayHasKey('time', $rateRecord);
}

$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]);
$this->assertArrayHasKey('rate', $rateRecords[0]);
$this->assertArrayHasKey('time', $rateRecords[0]);
$rates = $rates->findFirstCollection();
$this->assertSame('BTC', $rates->getBase());
}
}