mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-04 04:24:26 +00:00
Refactor methods that request the old currency preference.
This commit is contained in:
@@ -108,35 +108,10 @@ class Amount
|
||||
*/
|
||||
public function getCurrencies(): Collection
|
||||
{
|
||||
throw new FireflyException(sprintf('Method "%s" needs a refactor', __METHOD__));
|
||||
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getCurrencyCode(): string
|
||||
{
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty('getCurrencyCode');
|
||||
if ($cache->has()) {
|
||||
return $cache->get();
|
||||
}
|
||||
$currencyPreference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
||||
|
||||
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
|
||||
if ($currency) {
|
||||
$cache->store($currency->code);
|
||||
|
||||
return $currency->code;
|
||||
}
|
||||
$cache->store(config('firefly.default_currency', 'EUR'));
|
||||
|
||||
return (string)config('firefly.default_currency', 'EUR');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
@@ -163,22 +138,14 @@ class Amount
|
||||
if ($cache->has()) {
|
||||
return $cache->get();
|
||||
}
|
||||
$currencyPreference = app('preferences')->getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR'));
|
||||
$currencyPrefStr = $currencyPreference ? $currencyPreference->data : 'EUR';
|
||||
|
||||
// at this point the currency preference could be encrypted, if coming from an old version.
|
||||
$currencyCode = $this->tryDecrypt((string)$currencyPrefStr);
|
||||
|
||||
// could still be json encoded:
|
||||
/** @var TransactionCurrency|null $currency */
|
||||
$currency = TransactionCurrency::where('code', $currencyCode)->first();
|
||||
if (null === $currency) {
|
||||
// get EUR
|
||||
$currency = TransactionCurrency::where('code', 'EUR')->first();
|
||||
$default = $user->currencies()->where('user_default', true)->first();
|
||||
if(null === $default) {
|
||||
$default = $this->getSystemCurrency();
|
||||
$user->currencies()->sync([$default->id => ['user_default' => true]]);
|
||||
}
|
||||
$cache->store($currency);
|
||||
$cache->store($default);
|
||||
|
||||
return $currency;
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,6 +60,9 @@ class Preferences
|
||||
*/
|
||||
public function get(string $name, $default = null): ?Preference
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
if (null === $user) {
|
||||
@@ -82,6 +85,9 @@ class Preferences
|
||||
*/
|
||||
public function getForUser(User $user, string $name, $default = null): ?Preference
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']);
|
||||
if (null !== $preference && null === $preference->data) {
|
||||
$preference->delete();
|
||||
@@ -108,6 +114,9 @@ class Preferences
|
||||
*/
|
||||
public function delete(string $name): bool
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
|
||||
if (Cache::has($fullName)) {
|
||||
Cache::forget($fullName);
|
||||
@@ -123,6 +132,9 @@ class Preferences
|
||||
*/
|
||||
public function forget(User $user, string $name): void
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
$key = sprintf('preference%s%s', $user->id, $name);
|
||||
Cache::forget($key);
|
||||
Cache::put($key, '', 5);
|
||||
@@ -138,6 +150,9 @@ class Preferences
|
||||
*/
|
||||
public function setForUser(User $user, string $name, $value): Preference
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
$fullName = sprintf('preference%s%s', $user->id, $name);
|
||||
Cache::forget($fullName);
|
||||
/** @var Preference|null $pref */
|
||||
@@ -185,6 +200,9 @@ class Preferences
|
||||
*/
|
||||
public function findByName(string $name): Collection
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
return Preference::where('name', $name)->get();
|
||||
}
|
||||
|
||||
@@ -220,6 +238,9 @@ class Preferences
|
||||
*/
|
||||
public function getFresh(string $name, $default = null): ?Preference
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
if (null === $user) {
|
||||
@@ -243,6 +264,9 @@ class Preferences
|
||||
*/
|
||||
public function getFreshForUser(User $user, string $name, $default = null): ?Preference
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
return $this->getForUser($user, $name, $default);
|
||||
}
|
||||
|
||||
@@ -283,6 +307,9 @@ class Preferences
|
||||
*/
|
||||
public function set(string $name, $value): Preference
|
||||
{
|
||||
if('currencyPreference' === $name) {
|
||||
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
|
||||
}
|
||||
$user = auth()->user();
|
||||
if (null === $user) {
|
||||
// make new preference, return it:
|
||||
|
||||
Reference in New Issue
Block a user