New converters for #180 (Currency)

This commit is contained in:
James Cole 2016-04-01 14:10:08 +02:00
parent 5e78cc02bd
commit c14fa1021c
6 changed files with 131 additions and 9 deletions

View File

@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/** /**
* Class CurrencyCode * Class CurrencyCode
@ -17,10 +18,14 @@ class CurrencyCode extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) { 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 { } else {
$currency = TransactionCurrency::whereCode($this->value)->first(); $currency = $repository->findByCode($this->value);
} }
return $currency; return $currency;

View File

@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/** /**
* Class CurrencyId * Class CurrencyId
@ -17,10 +18,13 @@ class CurrencyId extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) { 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 { } else {
$currency = TransactionCurrency::find($this->value); $currency = $repository->find($this->value);
} }
return $currency; return $currency;

View File

@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/** /**
* Class CurrencyName * Class CurrencyName
@ -17,10 +18,14 @@ class CurrencyName extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) { 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 { } else {
$currency = TransactionCurrency::whereName($this->value)->first(); $currency = $repository->findByName($this->value);
} }
return $currency; return $currency;

View File

@ -3,6 +3,7 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
/** /**
* Class CurrencySymbol * Class CurrencySymbol
@ -17,10 +18,13 @@ class CurrencySymbol extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var CurrencyRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
if (isset($this->mapped[$this->index][$this->value])) { 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 { } else {
$currency = TransactionCurrency::whereSymbol($this->value)->first(); $currency = $repository->findBySymbol($this->value);
} }
return $currency; return $currency;

View File

@ -26,6 +26,75 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency->transactionJournals()->count(); 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 * @return Collection
*/ */

View File

@ -15,7 +15,6 @@ use Illuminate\Support\Collection;
*/ */
interface CurrencyRepositoryInterface interface CurrencyRepositoryInterface
{ {
/** /**
* @param TransactionCurrency $currency * @param TransactionCurrency $currency
* *
@ -23,6 +22,42 @@ interface CurrencyRepositoryInterface
*/ */
public function countJournals(TransactionCurrency $currency); 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 * @return Collection
*/ */