Skip to content

Commit 9453fdc

Browse files
committed
WIP
1 parent 2564c1c commit 9453fdc

16 files changed

+195
-125
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ docs
55
vendor
66
coverage
77
phpunit.xml
8+
.php_cs.cache
9+
.phpunit.result.cache

.php_cs.cache

-1
This file was deleted.

.phpunit.result.cache

-1
This file was deleted.

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
All notable changes to `laravel-currencylayer` will be documented in this file
44

5-
## 1.0.0 - 2019-09-11
5+
## 1.0.0 - 2019-09-13
66

77
- Initial release

database/factories/CurrencyFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99
$factory->define(Currency::class, function (Faker $faker) {
1010
return [
1111
'code' => $faker->unique()->randomLetter,
12-
'name' => $faker->sentence,
1312
];
1413
});

database/factories/RateFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
return factory(Currency::class)->create();
1818
},
1919
'rate' => $faker->randomFloat(12, 1, 10),
20-
'rate_for' => Carbon::now()->unix(),
20+
'timestamp' => Carbon::now()->unix(),
2121
];
2222
});

database/migrations/create_currencylayer_currencies_table.php.stub

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class CreateCurrencylayerCurrenciesTable extends Migration
1313
{
1414
Schema::create('currencylayer_currencies', function (Blueprint $table) {
1515
$table->bigIncrements('id');
16-
$table->string('name');
1716
$table->string('code', 3)->unique();
1817
});
1918
}

database/migrations/create_currencylayer_currency_rates_table.php.stub

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ use Illuminate\Support\Facades\Schema;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Database\Migrations\Migration;
66

7-
class CreateCurrencylayerCurrencyRatesTable extends Migration
7+
class CreateCurrencylayerRatesTable extends Migration
88
{
99
/**
1010
* Run the migrations.
1111
*/
1212
public function up()
1313
{
14-
Schema::create('currencylayer_currency_rates', function (Blueprint $table) {
14+
Schema::create('currencylayer_rates', function (Blueprint $table) {
1515
$table->bigIncrements('id');
1616
$table->unsignedBigInteger('source_currency_id')->index();
1717
$table->foreign('source_currency_id')->references('id')->on('currencylayer_currencies')->onDelete('cascade');
1818
$table->unsignedBigInteger('target_currency_id')->index();
1919
$table->foreign('target_currency_id')->references('id')->on('currencylayer_currencies')->onDelete('cascade');
2020
$table->decimal('rate', 15, 15);
21-
$table->datetime('rate_for');
21+
$table->timestamp('timestamp');
2222
});
2323
}
2424

@@ -27,6 +27,6 @@ class CreateCurrencylayerCurrencyRatesTable extends Migration
2727
*/
2828
public function down()
2929
{
30-
Schema::dropIfExists('currencylayer_currency_rates');
30+
Schema::dropIfExists('currencylayer_rates');
3131
}
3232
}

src/Currencylayer.php

+70-57
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Carbon\Carbon;
66
use Illuminate\Contracts\Cache\Repository;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
89
use OceanApplications\currencylayer\client;
910
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;
1011

@@ -29,6 +30,35 @@ public function __construct(client $client)
2930
$this->client = $client;
3031
}
3132

33+
public function live($source, ...$currencies)
34+
{
35+
$currencies = Arr::flatten($currencies);
36+
37+
$response = $this->apiRate($source, implode(',', $currencies));
38+
39+
$rates = [];
40+
$sourceCurrency = Currency::firstOrCreate(['code' => $source]);
41+
foreach ($response['quotes'] as $code => $rate) {
42+
$targetCurrency = Currency::firstOrCreate(['code' => $targetCurrencyCode = substr($code, -3)]);
43+
44+
$createdRate = $sourceCurrency->rates()->create([
45+
'target_currency_id' => $targetCurrency->id,
46+
'rate' => $rate,
47+
'timestamp' => $response['timestamp'],
48+
]);
49+
$rates[$targetCurrencyCode] = $createdRate->rate;
50+
}
51+
52+
return count($currencies) === 1 ? array_values($rates)[0] : $rates;
53+
}
54+
55+
private function apiRate(string $source, string $currencies, ?string $date = null): array
56+
{
57+
$client = $this->client->source($source)->currencies($currencies);
58+
59+
return $date ? $client->date($date)->historical() : $client->live();
60+
}
61+
3262
/**
3363
* @param Carbon|string $date
3464
*
@@ -51,65 +81,48 @@ public function date($date): self
5181
// return $this;
5282
// }
5383

54-
/**
55-
* @param Currency|string $source
56-
* @param Currency|string $target
57-
*/
58-
public function rate($source, $target)
59-
{
60-
if (! $source instanceof Currency) {
61-
$source = Currency::where('code', $source)->first();
62-
}
63-
64-
if (! $target instanceof Currency) {
65-
$target = Currency::where('code', $target)->first();
66-
}
67-
68-
if (! $source || ! $target) {
69-
throw new \InvalidArgumentException(
70-
'Source or target currency is not available. Did you fetch all currencies? ' .
71-
'Call currencies() method to fetch all available currencies.'
72-
);
73-
}
74-
75-
$source->rates()->where('target_currency_id', $target->id)->latest()->first();
76-
77-
// $currencies = Arr::flatten($currencies);
84+
// /**
85+
// * @param Currency|string $source
86+
// * @param Currency|string $target
87+
// * @param Carbon|string|null $date
88+
// *
89+
// * @return float
90+
// */
91+
// public function rate($source, $target, $date = null)
92+
// {
93+
// if (! $source instanceof Currency) {
94+
// $source = Currency::where('code', $source)->first();
95+
// }
7896
//
79-
// return $this->client
80-
// ->source($source)
81-
// ->currencies(implode(',', $currencies))
82-
// ->live();
83-
}
84-
85-
public function fetch(string $source, ...$currencies)
86-
{
87-
$client = $this->client->source($source)->currencies(implode(',', Arr::flatten($currencies)));
88-
$response = $this->date ? $client->date($this->date)->historical() : $client->live();
89-
90-
$sourceCurrency = Currency::where('code', $response['source'])->first();
97+
// if (! $target instanceof Currency) {
98+
// $target = Currency::where('code', $target)->first();
99+
// }
100+
//
101+
// if (! $source || ! $target) {
102+
// throw new \InvalidArgumentException(
103+
// 'Source or target currency is not available. Did you fetch all currencies? ' .
104+
// 'Call currencies() method to fetch all available currencies.'
105+
// );
106+
// }
107+
//
108+
// if (! $date) {
109+
// $this->fetch($source->code, $target->code);
110+
// }
111+
//
112+
// return $source->rate($target)->rate;
113+
// }
91114

92-
foreach ($response['quotes'] as $code => $rate) {
93-
$targetCurrency = Currency::where('code', substr($code, -3))->first();
94-
if ($sourceCurrency && $targetCurrency) {
95-
$sourceCurrency->targetRates()->create([
96-
'target_currency_id' => $targetCurrency->id,
97-
'rate' => $rate,
98-
'rate_for' => $response['timestamp'],
99-
]);
100-
}
101-
}
102-
}
103115

104-
public function currencies()
105-
{
106-
$response = $this->client->list();
107116

108-
foreach ($response['currencies'] as $code => $name) {
109-
$currency = Currency::where('code', $code)->first();
110-
if (! $currency) {
111-
Currency::create(['code' => $code, 'name' => $name]);
112-
}
113-
}
114-
}
117+
// public function currencies()
118+
// {
119+
// $response = $this->client->list();
120+
//
121+
// foreach ($response['currencies'] as $code => $name) {
122+
// $currency = Currency::where('code', $code)->first();
123+
// if (! $currency) {
124+
// Currency::create(['code' => $code, 'name' => $name]);
125+
// }
126+
// }
127+
// }
115128
}

src/Models/Currency.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ class Currency extends Model
1616
'code',
1717
];
1818

19-
public function targetRates(): HasMany
19+
public function rates(): HasMany
2020
{
2121
return $this->hasMany(Rate::class, 'source_currency_id');
2222
}
23+
24+
public function rate(Currency $target, $date = null)
25+
{
26+
return $this->targetRates()->where('target_currency_id', $target->id)->orderByDesc('rate_for')->first();
27+
}
2328
}

src/Models/Rate.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
class Rate extends Model
99
{
10-
protected $table = 'currencylayer_currency_rates';
10+
protected $table = 'currencylayer_rates';
1111

1212
public $timestamps = false;
1313

1414
protected $fillable = [
1515
'target_currency_id',
1616
'rate',
17-
'rate_for',
17+
'timestamp',
1818
];
1919

2020
protected $dates = [
21-
'rate_for',
21+
'timestamp',
2222
];
2323

2424
protected $casts = [

0 commit comments

Comments
 (0)