Skip to content

Firefly III: a personal finances manager

New converters for #180 (Currency)

James Colec14fa1021ca2
diff --git a/app/Helpers/Csv/Converter/CurrencyCode.php b/app/Helpers/Csv/Converter/CurrencyCode.php
index 98e01e5874..7054b1f5d5 100644
--- a/app/Helpers/Csv/Converter/CurrencyCode.php
+++ b/app/Helpers/Csv/Converter/CurrencyCode.php
@@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/**
* Class CurrencyCode
@@ -17,10 +18,14 @@ class CurrencyCode extends BasicConverter implements ConverterInterface
*/
public function convert()
{
+ /** @var CurrencyRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
+
+
if (isset($this->mapped[$this->index][$this->value])) {
- $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]);
+ $currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
- $currency = TransactionCurrency::whereCode($this->value)->first();
+ $currency = $repository->findByCode($this->value);
}
return $currency;
diff --git a/app/Helpers/Csv/Converter/CurrencyId.php b/app/Helpers/Csv/Converter/CurrencyId.php
index ae1fb9af47..ce21771f12 100644
--- a/app/Helpers/Csv/Converter/CurrencyId.php
+++ b/app/Helpers/Csv/Converter/CurrencyId.php
@@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/**
* Class CurrencyId
@@ -17,10 +18,13 @@ class CurrencyId extends BasicConverter implements ConverterInterface
*/
public function convert()
{
+ /** @var CurrencyRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
+
if (isset($this->mapped[$this->index][$this->value])) {
- $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]);
+ $currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
- $currency = TransactionCurrency::find($this->value);
+ $currency = $repository->find($this->value);
}
return $currency;
diff --git a/app/Helpers/Csv/Converter/CurrencyName.php b/app/Helpers/Csv/Converter/CurrencyName.php
index 4bdbe79c3a..336c533e6b 100644
--- a/app/Helpers/Csv/Converter/CurrencyName.php
+++ b/app/Helpers/Csv/Converter/CurrencyName.php
@@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/**
* Class CurrencyName
@@ -17,10 +18,14 @@ class CurrencyName extends BasicConverter implements ConverterInterface
*/
public function convert()
{
+ /** @var CurrencyRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
+
if (isset($this->mapped[$this->index][$this->value])) {
- $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]);
+
+ $currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
- $currency = TransactionCurrency::whereName($this->value)->first();
+ $currency = $repository->findByName($this->value);
}
return $currency;
diff --git a/app/Helpers/Csv/Converter/CurrencySymbol.php b/app/Helpers/Csv/Converter/CurrencySymbol.php
index 62cc92bcd6..b27c122db3 100644
--- a/app/Helpers/Csv/Converter/CurrencySymbol.php
+++ b/app/Helpers/Csv/Converter/CurrencySymbol.php
@@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/**
* Class CurrencySymbol
@@ -17,10 +18,13 @@ class CurrencySymbol extends BasicConverter implements ConverterInterface
*/
public function convert()
{
+ /** @var CurrencyRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
+
if (isset($this->mapped[$this->index][$this->value])) {
- $currency = TransactionCurrency::find($this->mapped[$this->index][$this->value]);
+ $currency = $repository->find($this->mapped[$this->index][$this->value]);
} else {
- $currency = TransactionCurrency::whereSymbol($this->value)->first();
+ $currency = $repository->findBySymbol($this->value);
}
return $currency;
diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php
index 28c4d35ca4..02328f291a 100644
--- a/app/Repositories/Currency/CurrencyRepository.php
+++ b/app/Repositories/Currency/CurrencyRepository.php
@@ -26,6 +26,75 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency->transactionJournals()->count();
}
+ /**
+ * Find by ID
+ *
+ * @param int $currencyId
+ *
+ * @return TransactionCurrency
+ */
+ public function find(int $currencyId) : TransactionCurrency
+ {
+ $currency = TransactionCurrency::find($currencyId);
+ if (is_null($currency)) {
+ $currency = new TransactionCurrency;
+
+ }
+
+ return $currency;
+ }
+
+ /**
+ * Find by currency code
+ *
+ * @param string $currencyCode
+ *
+ * @return TransactionCurrency
+ */
+ public function findByCode(string $currencyCode) : TransactionCurrency
+ {
+ $currency = TransactionCurrency::whereCode($currencyCode)->first();
+ if (is_null($currency)) {
+ $currency = new TransactionCurrency;
+ }
+
+ return $currency;
+ }
+
+ /**
+ * Find by currency name
+ *
+ * @param string $currencyName
+ *
+ * @return TransactionCurrency
+ */
+ public function findByName(string $currencyName) : TransactionCurrency
+ {
+ $preferred = TransactionCurrency::whereName($currencyName)->first();
+ if (is_null($preferred)) {
+ $preferred = new TransactionCurrency;
+ }
+
+ return $preferred;
+ }
+
+ /**
+ * Find by currency symbol
+ *
+ * @param string $currencySymbol
+ *
+ * @return TransactionCurrency
+ */
+ public function findBySymbol(string $currencySymbol) : TransactionCurrency
+ {
+ $currency = TransactionCurrency::whereSymbol($currencySymbol)->first();
+ if (is_null($currency)) {
+ $currency = new TransactionCurrency;
+ }
+
+ return $currency;
+ }
+
/**
* @return Collection
*/
diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.php
index 9284ad24c7..3b82882dd3 100644
--- a/app/Repositories/Currency/CurrencyRepositoryInterface.php
+++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php
@@ -15,7 +15,6 @@ use Illuminate\Support\Collection;
*/
interface CurrencyRepositoryInterface
{
-
/**
* @param TransactionCurrency $currency
*
@@ -23,6 +22,42 @@ interface CurrencyRepositoryInterface
*/
public function countJournals(TransactionCurrency $currency);
+ /**
+ * Find by ID
+ *
+ * @param int $currencyId
+ *
+ * @return TransactionCurrency
+ */
+ public function find(int $currencyId) : TransactionCurrency;
+
+ /**
+ * Find by currency code
+ *
+ * @param string $currencyCode
+ *
+ * @return TransactionCurrency
+ */
+ public function findByCode(string $currencyCode) : TransactionCurrency;
+
+ /**
+ * Find by currency name
+ *
+ * @param string $currencyName
+ *
+ * @return TransactionCurrency
+ */
+ public function findByName(string $currencyName) : TransactionCurrency;
+
+ /**
+ * Find by currency symbol
+ *
+ * @param string $currencySymbol
+ *
+ * @return TransactionCurrency
+ */
+ public function findBySymbol(string $currencySymbol) : TransactionCurrency;
+
/**
* @return Collection
*/