Skip to content

Added ability to disable auto update for a currency. #96

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

Open
wants to merge 3 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
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@
"psr-4": {
"Torann\\Currency\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Torann\\Currency\\CurrencyServiceProvider"
],
"aliases": {
"Currency": "Torann\\Currency\\Facades\\Currency"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Illuminate\Database\Migrations\Migration;

class AlterCurrencyTableAddUpdateColumn extends Migration
{
/**
* Currencies table name
*
* @var string
*/
protected $table_name;

/**
* Create a new migration instance.
*/
public function __construct()
{
$this->table_name = config('currency.drivers.database.table');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table($this->table_name, function ($table) {
$table->boolean('auto_update')->default(true)->after('active');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table($this->table_name, function ($table) {
$table->dropColumn('auto_update');
});
}
}
23 changes: 12 additions & 11 deletions src/Contracts/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ public function all();
* @return mixed
*/
public function find($code, $active = 1);

/**
* Update given currency.
*
* @param string $code
* @param array $attributes
* @param DateTime $timestamp
*
* @return int
*/
public function update($code, array $attributes, DateTime $timestamp = null);

/**
* Update given currency.
*
* @param string $code
* @param array $attributes
* @param DateTime $timestamp
* @param bool $auto Whether or not it's performed automatically
*
* @return int
*/
public function update($code, array $attributes, DateTime $timestamp = null, $auto = false);

/**
* Remove given currency from storage.
Expand Down
17 changes: 13 additions & 4 deletions src/Drivers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function create(array $params)
'format' => '',
'exchange_rate' => 1,
'active' => 0,
'auto_update' => 1,
'created_at' => $created,
'updated_at' => $created,
], $params);
Expand Down Expand Up @@ -97,7 +98,7 @@ public function find($code, $active = 1)
/**
* {@inheritdoc}
*/
public function update($code, array $attributes, DateTime $timestamp = null)
public function update($code, array $attributes, DateTime $timestamp = null, $auto = false)
{
$table = $this->config('table');

Expand All @@ -106,9 +107,17 @@ public function update($code, array $attributes, DateTime $timestamp = null)
$attributes['updated_at'] = new DateTime('now');
}

return $this->database->table($table)
->where('code', strtoupper($code))
->update($attributes);
//Only apply to those with auto update if it's automatically updating
if ($auto) {
return $this->database->table($table)
->where('code', strtoupper($code))
->where('auto_update', 1)
->update($attributes);
} else {
return $this->database->table($table)
->where('code', strtoupper($code))
->update($attributes);
}
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Drivers/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function create(array $params)
'format' => '',
'exchange_rate' => 1,
'active' => 0,
'auto_update' => 1,
'created_at' => $created,
'updated_at' => $created,
], $params);
Expand Down Expand Up @@ -95,7 +96,7 @@ public function find($code, $active = 1)
/**
* {@inheritdoc}
*/
public function update($code, array $attributes, DateTime $timestamp = null)
public function update($code, array $attributes, DateTime $timestamp = null, $auto = false)
{
// Get blacklist path
$path = $this->config('path');
Expand All @@ -114,7 +115,14 @@ public function update($code, array $attributes, DateTime $timestamp = null)
}

// Merge values
$currencies[$code] = array_merge($currencies[$code], $attributes);
if ($auto) {
//If auto update disabled
if ($currencies[$code]['auto_update']) {
$currencies[$code] = array_merge($currencies[$code], $attributes);
}
} else {
$currencies[ $code ] = array_merge($currencies[ $code ], $attributes);
}

return $this->filesystem->put($path, json_encode($currencies, JSON_PRETTY_PRINT));
}
Expand Down