Skip to content

Commit

Permalink
Approve the code review
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed May 31, 2018
1 parent d5938a1 commit 1188f14
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 106 deletions.
24 changes: 5 additions & 19 deletions example.php
Expand Up @@ -24,17 +24,9 @@
$rates = $porter->import(new ImportSpecification($specificRate))
->findFirstCollection();

echo $rates->getBase();
echo PHP_EOL;
$rateRecords = $rates->toAssociativeArray();

echo $rates->getQuote();
echo PHP_EOL;

echo $rates->getTime();
echo PHP_EOL;

echo $rates->getRate();
echo PHP_EOL;
var_dump($rateRecords);

// Get all rates

Expand All @@ -47,14 +39,8 @@
$rates = $porter->import(new ImportSpecification($specificRate))
->findFirstCollection();

echo $rates->getBase(); // BTC
echo PHP_EOL;

var_dump($rates->getQuote()); // array
echo PHP_EOL;
$rateRecords = $rates->toAssociativeArray();

var_dump($rates->getTime()); // array
echo PHP_EOL;
echo $rates->getBase() . PHP_EOL;

var_dump($rates->getRate()); // rate
echo PHP_EOL;
var_dump($rateRecords);
31 changes: 9 additions & 22 deletions src/Collection/AllRatesRecord.php
Expand Up @@ -10,37 +10,24 @@ class AllRatesRecord extends CountableProviderRecords
{
private $base;

private $time;
public function __construct(
\Iterator $providerRecords,
string $base,
int $count,
ProviderResource $resource
) {
parent::__construct($providerRecords, $count, $resource);

private $quote;

private $rate;

public function __construct(array $time, string $base, array $quote, array $rate)
{
$this->base = $base;
$this->time = $time;
$this->quote = $quote;
$this->rate = $rate;
}

public function getBase(): string
{
return $this->base;
}

public function getTime(): array
{
return $this->time;
}

public function getQuote(): array
{
return $this->quote;
}

public function getRate(): array
public function toAssociativeArray(): array
{
return $this->rate;
return iterator_to_array($this);
}
}
39 changes: 8 additions & 31 deletions src/Collection/SpecificRateRecord.php
Expand Up @@ -8,39 +8,16 @@

class SpecificRateRecord extends CountableProviderRecords
{
private $base;

private $time;

private $quote;

private $rate;

public function __construct(string $time, string $base, string $quote, float $rate)
{
$this->base = $base;
$this->time = $time;
$this->quote = $quote;
$this->rate = $rate;
}

public function getBase(): string
{
return $this->base;
}

public function getTime(): string
{
return $this->time;
}

public function getQuote(): string
{
return $this->quote;
public function __construct(
\Iterator $providerRecords,
int $count,
ProviderResource $resource
) {
parent::__construct($providerRecords, $count, $resource);
}

public function getRate(): float
public function toAssociativeArray(): array
{
return $this->rate;
return iterator_to_array($this);
}
}
24 changes: 12 additions & 12 deletions src/Resource/GetAllRate.php
Expand Up @@ -38,17 +38,17 @@ public function fetch(ImportConnector $connector): \Iterator
);

$rates = $response['rates'];
$base = $response['asset_id_base'];
$quote = [];
$rate = [];
$time = [];

foreach($rates as $key => $value) {
$quote[] = $rates[$key]['asset_id_quote'];
$rate[] = $rates[$key]['rate'];
$time[] = $rates[$key]['time'];
}

return new AllRatesRecord($time, $base, $quote, $rate);

$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);
}
}
17 changes: 10 additions & 7 deletions src/Resource/GetSpecificRate.php
Expand Up @@ -39,12 +39,15 @@ public function fetch(ImportConnector $connector): \Iterator
true
);

$base = $response['asset_id_base'];

$quote = $response['asset_id_quote'];
$rate = $response['rate'];
$time = $response['time'];

return new SpecificRateRecord($time, $base, $quote, $rate);
$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);
}
}
29 changes: 14 additions & 15 deletions tests/Functional/GetRateTest.php
Expand Up @@ -21,10 +21,13 @@ public function testGetSpecificRateRecords()
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey)))
->findFirstCollection();

$this->assertSame('BTC', $rates->getBase());
$this->assertSame('USD', $rates->getQuote());
$this->assertLessThanOrEqual(time(), strtotime($rates->getTime()));
$this->assertInternalType('float', $rates->getRate());
$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]);
}

public function testGetAllRateRecords()
Expand All @@ -33,16 +36,12 @@ public function testGetAllRateRecords()
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey)))
->findFirstCollection();

$base = $rates->getBase();
$quote = $rates->getQuote();
$time = $rates->getTime();
$rate = $rates->getRate();

$this->assertSame('BTC', $base);
$this->assertContains('USD', $quote);
$this->assertContains('GBP', $quote);
$this->assertContains('EUR', $quote);
$this->assertLessThanOrEqual(time(), strtotime($time[0]));
$this->assertInternalType('float', $rate[0]);
$this->assertSame('BTC', $rates->getBase());

$rateRecords = $rates->toAssociativeArray();

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

0 comments on commit 1188f14

Please sign in to comment.