From 0f32761ae86e0ce15f7c50ccceae5567f2ba362d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 8 Nov 2020 14:13:21 +0100 Subject: [PATCH] Refactor code. --- .../CurrencyExchangeRateController.php | 112 -------------- .../V1/Requests/TransactionUpdateRequest.php | 16 -- app/Factory/RecurrenceFactory.php | 9 +- app/Factory/TransactionFactory.php | 21 +-- app/Factory/TransactionJournalFactory.php | 4 - app/Factory/TransactionJournalMetaFactory.php | 12 -- app/Factory/TransactionTypeFactory.php | 12 -- .../Chart/Basic/ChartJsGenerator.php | 12 -- app/Helpers/Attachments/AttachmentHelper.php | 4 - app/Helpers/Fiscal/FiscalHelper.php | 4 - app/Helpers/Help/Help.php | 3 - app/Helpers/Report/NetWorth.php | 10 -- app/Helpers/Report/PopupReport.php | 10 -- app/Helpers/Report/ReportHelper.php | 6 - .../Controllers/Json/ExchangeController.php | 83 ----------- app/Repositories/Account/AccountTasker.php | 10 -- .../Account/OperationsRepository.php | 11 -- .../Attachment/AttachmentRepository.php | 10 -- .../Budget/NoBudgetRepository.php | 11 -- .../Category/NoCategoryRepository.php | 11 -- .../Category/OperationsRepository.php | 11 -- .../Journal/JournalCLIRepository.php | 10 -- .../Journal/JournalRepository.php | 10 -- .../LinkType/LinkTypeRepository.php | 10 -- .../ObjectGroup/ObjectGroupRepository.php | 10 -- app/Repositories/Rule/RuleRepository.php | 10 -- .../RuleGroup/RuleGroupRepository.php | 10 -- app/Repositories/Tag/OperationsRepository.php | 11 -- .../TransactionGroupRepository.php | 14 +- .../IsDuplicateTransaction.php} | 48 +++--- app/Services/Currency/FixerIOv2.php | 137 ------------------ app/Services/Currency/RatesApiIOv1.php | 126 ---------------- app/Support/Http/Controllers/AugumentData.php | 15 +- config/mail.php | 2 +- 34 files changed, 45 insertions(+), 750 deletions(-) delete mode 100644 app/Api/V1/Controllers/CurrencyExchangeRateController.php delete mode 100644 app/Http/Controllers/Json/ExchangeController.php rename app/{Services/Currency/ExchangeRateInterface.php => Rules/IsDuplicateTransaction.php} (51%) delete mode 100644 app/Services/Currency/FixerIOv2.php delete mode 100644 app/Services/Currency/RatesApiIOv1.php diff --git a/app/Api/V1/Controllers/CurrencyExchangeRateController.php b/app/Api/V1/Controllers/CurrencyExchangeRateController.php deleted file mode 100644 index 2f4f1c4a80..0000000000 --- a/app/Api/V1/Controllers/CurrencyExchangeRateController.php +++ /dev/null @@ -1,112 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace FireflyIII\Api\V1\Controllers; - -use Carbon\Carbon; -use FireflyIII\Exceptions\FireflyException; -use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; -use FireflyIII\Services\Currency\ExchangeRateInterface; -use FireflyIII\Transformers\CurrencyExchangeRateTransformer; -use FireflyIII\User; -use Illuminate\Http\JsonResponse; -use Illuminate\Http\Request; -use League\Fractal\Resource\Item; - -/** - * Class CurrencyExchangeRateController - * - * @codeCoverageIgnore - */ -class CurrencyExchangeRateController extends Controller -{ - /** @var CurrencyRepositoryInterface The currency repository */ - private $repository; - - - /** - * CurrencyExchangeRateController constructor. - */ - public function __construct() - { - parent::__construct(); - $this->middleware( - function ($request, $next) { - /** @var User $admin */ - $admin = auth()->user(); - - $this->repository = app(CurrencyRepositoryInterface::class); - $this->repository->setUser($admin); - - return $next($request); - } - ); - - } - - /** - * Show an exchange rate. - * - * @param Request $request - * - * @return JsonResponse - * @throws FireflyException - */ - public function index(Request $request): JsonResponse - { - $manager = $this->getManager(); - $fromCurrency = $this->repository->findByCodeNull($request->get('from') ?? 'EUR'); - $toCurrency = $this->repository->findByCodeNull($request->get('to') ?? 'USD'); - - if (null === $fromCurrency) { - throw new FireflyException('200007: Unknown source currency'); - } - if (null === $toCurrency) { - throw new FireflyException('200007: Unknown destination currency'); - } - - /** @var Carbon $dateObj */ - $dateObj = Carbon::createFromFormat('Y-m-d', $request->get('date') ?? date('Y-m-d')); - $this->parameters->set('from', $fromCurrency->code); - $this->parameters->set('to', $toCurrency->code); - $this->parameters->set('date', $dateObj->format('Y-m-d')); - $this->parameters->set('amount', $request->get('amount')); - - $rate = $this->repository->getExchangeRate($fromCurrency, $toCurrency, $dateObj); - if (null === $rate) { - /** @var User $admin */ - $admin = auth()->user(); - // create service: - /** @var ExchangeRateInterface $service */ - $service = app(ExchangeRateInterface::class); - $service->setUser($admin); - $rate = $service->getRate($fromCurrency, $toCurrency, $dateObj); - } - /** @var CurrencyExchangeRateTransformer $transformer */ - $transformer = app(CurrencyExchangeRateTransformer::class); - $transformer->setParameters($this->parameters); - $resource = new Item($rate, $transformer, 'currency_exchange_rates'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - } -} diff --git a/app/Api/V1/Requests/TransactionUpdateRequest.php b/app/Api/V1/Requests/TransactionUpdateRequest.php index 977eeb2215..449bd452ab 100644 --- a/app/Api/V1/Requests/TransactionUpdateRequest.php +++ b/app/Api/V1/Requests/TransactionUpdateRequest.php @@ -374,26 +374,10 @@ class TransactionUpdateRequest extends FormRequest // validate source/destination is equal, depending on the transaction journal type. $this->validateEqualAccountsForUpdate($validator, $transactionGroup); - // If type is set, source + destination info is mandatory. - // Not going to do this. Not sure where the demand came from. - // validate that the currency fits the source and/or destination account. // validate all account info $this->validateAccountInformationUpdate($validator); - // The currency info must match the accounts involved. - // Instead will ignore currency info as much as possible. - - // TODO if the transaction_journal_id is empty, some fields are mandatory, like the amount! - - // all journals must have a description - - // // validate foreign currency info - // - - // - // // make sure all splits have valid source + dest info - // the group must have a description if > 1 journal. } ); } diff --git a/app/Factory/RecurrenceFactory.php b/app/Factory/RecurrenceFactory.php index b3582200f3..6549a23d6a 100644 --- a/app/Factory/RecurrenceFactory.php +++ b/app/Factory/RecurrenceFactory.php @@ -41,10 +41,8 @@ class RecurrenceFactory { use TransactionTypeTrait, RecurringTransactionTrait; - /** @var MessageBag */ - private $errors; - /** @var User */ - private $user; + private MessageBag $errors; + private User $user; /** @@ -54,9 +52,6 @@ class RecurrenceFactory */ public function __construct() { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } $this->errors = new MessageBag; } diff --git a/app/Factory/TransactionFactory.php b/app/Factory/TransactionFactory.php index 9521838dc5..0b5f3f2aa9 100644 --- a/app/Factory/TransactionFactory.php +++ b/app/Factory/TransactionFactory.php @@ -38,18 +38,12 @@ use Log; */ class TransactionFactory { - /** @var Account */ - private $account; - /** @var TransactionCurrency */ - private $currency; - /** @var TransactionCurrency */ - private $foreignCurrency; - /** @var TransactionJournal */ - private $journal; - /** @var bool */ - private $reconciled; - /** @var User */ - private $user; + private Account $account; + private TransactionCurrency $currency; + private TransactionCurrency $foreignCurrency; + private TransactionJournal $journal; + private bool $reconciled; + private User $user; /** @@ -59,9 +53,6 @@ class TransactionFactory */ public function __construct() { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } $this->reconciled = false; } diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index e78b2ee938..2b4f61af63 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -95,10 +95,6 @@ class TransactionJournalFactory ]; - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - $this->currencyRepository = app(CurrencyRepositoryInterface::class); $this->typeRepository = app(TransactionTypeRepositoryInterface::class); $this->billRepository = app(BillRepositoryInterface::class); diff --git a/app/Factory/TransactionJournalMetaFactory.php b/app/Factory/TransactionJournalMetaFactory.php index 88b0059749..872e0ca601 100644 --- a/app/Factory/TransactionJournalMetaFactory.php +++ b/app/Factory/TransactionJournalMetaFactory.php @@ -34,18 +34,6 @@ use Log; */ class TransactionJournalMetaFactory { - /** - * Constructor. - * - * @codeCoverageIgnore - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @param array $data * diff --git a/app/Factory/TransactionTypeFactory.php b/app/Factory/TransactionTypeFactory.php index 45b0414c94..2ec9b54a29 100644 --- a/app/Factory/TransactionTypeFactory.php +++ b/app/Factory/TransactionTypeFactory.php @@ -33,18 +33,6 @@ use Log; */ class TransactionTypeFactory { - /** - * Constructor. - * - * @codeCoverageIgnore - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @param string $type * diff --git a/app/Generator/Chart/Basic/ChartJsGenerator.php b/app/Generator/Chart/Basic/ChartJsGenerator.php index a9a4134e2c..a9158ca78b 100644 --- a/app/Generator/Chart/Basic/ChartJsGenerator.php +++ b/app/Generator/Chart/Basic/ChartJsGenerator.php @@ -30,18 +30,6 @@ use Log; */ class ChartJsGenerator implements GeneratorInterface { - /** - * Constructor. - * - * @codeCoverageIgnore - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * Expects data as:. * diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php index 71004cd113..c20a6413e6 100644 --- a/app/Helpers/Attachments/AttachmentHelper.php +++ b/app/Helpers/Attachments/AttachmentHelper.php @@ -70,10 +70,6 @@ class AttachmentHelper implements AttachmentHelperInterface $this->messages = new MessageBag; $this->attachments = new Collection; $this->uploadDisk = Storage::disk('upload'); - - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } } diff --git a/app/Helpers/Fiscal/FiscalHelper.php b/app/Helpers/Fiscal/FiscalHelper.php index 10959f29c3..b9dbd6be39 100644 --- a/app/Helpers/Fiscal/FiscalHelper.php +++ b/app/Helpers/Fiscal/FiscalHelper.php @@ -39,10 +39,6 @@ class FiscalHelper implements FiscalHelperInterface public function __construct() { $this->useCustomFiscalYear = app('preferences')->get('customFiscalYear', false)->data; - - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } } /** diff --git a/app/Helpers/Help/Help.php b/app/Helpers/Help/Help.php index 359b37294d..f7fc1415b1 100644 --- a/app/Helpers/Help/Help.php +++ b/app/Helpers/Help/Help.php @@ -44,9 +44,6 @@ class Help implements HelpInterface */ public function __construct() { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } $this->userAgent = sprintf($this->userAgent, config('firefly.version')); } diff --git a/app/Helpers/Report/NetWorth.php b/app/Helpers/Report/NetWorth.php index d8fbf511bd..63aa980dae 100644 --- a/app/Helpers/Report/NetWorth.php +++ b/app/Helpers/Report/NetWorth.php @@ -47,16 +47,6 @@ class NetWorth implements NetWorthInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * Returns the user's net worth in an array with the following layout: * diff --git a/app/Helpers/Report/PopupReport.php b/app/Helpers/Report/PopupReport.php index 7b586b8866..01cefdc1bf 100644 --- a/app/Helpers/Report/PopupReport.php +++ b/app/Helpers/Report/PopupReport.php @@ -39,16 +39,6 @@ use Log; */ class PopupReport implements PopupReportInterface { - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * Collect the transactions for one account and one budget. * diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index 3d532a132a..70745d1a6b 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -49,12 +49,6 @@ class ReportHelper implements ReportHelperInterface public function __construct(BudgetRepositoryInterface $budgetRepository) { $this->budgetRepository = $budgetRepository; - - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - - } /** diff --git a/app/Http/Controllers/Json/ExchangeController.php b/app/Http/Controllers/Json/ExchangeController.php deleted file mode 100644 index 41e85d2391..0000000000 --- a/app/Http/Controllers/Json/ExchangeController.php +++ /dev/null @@ -1,83 +0,0 @@ -. - */ -declare(strict_types=1); - -namespace FireflyIII\Http\Controllers\Json; - -use Carbon\Carbon; -use FireflyIII\Http\Controllers\Controller; -use FireflyIII\Models\TransactionCurrency; -use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; -use FireflyIII\Services\Currency\ExchangeRateInterface; -use FireflyIII\User; -use Illuminate\Http\JsonResponse; -use Illuminate\Http\Request; -use Log; - -/** - * Class ExchangeController. - */ -class ExchangeController extends Controller -{ - - /** - * Returns an exchange rate. - * - * @param Request $request - * @param TransactionCurrency $fromCurrency - * @param TransactionCurrency $toCurrency - * @param Carbon $date - * - * @return JsonResponse - */ - public function getRate(Request $request, TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): JsonResponse - { - /** @var CurrencyRepositoryInterface $repository */ - $repository = app(CurrencyRepositoryInterface::class); - $rate = $repository->getExchangeRate($fromCurrency, $toCurrency, $date); - - - if (null === $rate) { - Log::debug(sprintf('No cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d'))); - - // create service: - /** @var User $user */ - $user = auth()->user(); - /** @var ExchangeRateInterface $service */ - $service = app(ExchangeRateInterface::class); - $service->setUser($user); - - // get rate: - $rate = $service->getRate($fromCurrency, $toCurrency, $date); - } - - $return = $rate->toArray(); - $return['amount'] = null; - if (null !== $request->get('amount')) { - // assume amount is in "from" currency: - $return['amount'] = bcmul($request->get('amount'), (string) $rate->rate, 12); - // round to toCurrency decimal places: - $return['amount'] = round($return['amount'], $toCurrency->decimal_places); - } - - return response()->json($return); - } -} diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php index 061362e106..bde3ae8ad6 100644 --- a/app/Repositories/Account/AccountTasker.php +++ b/app/Repositories/Account/AccountTasker.php @@ -39,16 +39,6 @@ class AccountTasker implements AccountTaskerInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @param Collection $accounts * @param Carbon $start diff --git a/app/Repositories/Account/OperationsRepository.php b/app/Repositories/Account/OperationsRepository.php index b7458808fa..1c39fe31d5 100644 --- a/app/Repositories/Account/OperationsRepository.php +++ b/app/Repositories/Account/OperationsRepository.php @@ -40,17 +40,6 @@ class OperationsRepository implements OperationsRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - die(__METHOD__); - } - } - /** * This method returns a list of all the withdrawal transaction journals (as arrays) set in that period * which have the specified accounts. It's grouped per currency, with as few details in the array diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php index 47841c640b..1518bdaa6c 100644 --- a/app/Repositories/Attachment/AttachmentRepository.php +++ b/app/Repositories/Attachment/AttachmentRepository.php @@ -45,16 +45,6 @@ class AttachmentRepository implements AttachmentRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @param Attachment $attachment * diff --git a/app/Repositories/Budget/NoBudgetRepository.php b/app/Repositories/Budget/NoBudgetRepository.php index ade5f3fb90..7229a49420 100644 --- a/app/Repositories/Budget/NoBudgetRepository.php +++ b/app/Repositories/Budget/NoBudgetRepository.php @@ -41,17 +41,6 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - die(get_class($this)); - } - } - /** * @param Collection $accounts * @param Carbon $start diff --git a/app/Repositories/Category/NoCategoryRepository.php b/app/Repositories/Category/NoCategoryRepository.php index b8581a2df8..e86df31d12 100644 --- a/app/Repositories/Category/NoCategoryRepository.php +++ b/app/Repositories/Category/NoCategoryRepository.php @@ -40,17 +40,6 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - die(__METHOD__); - } - } - /** * This method returns a list of all the withdrawal transaction journals (as arrays) set in that period * which have no category set to them. It's grouped per currency, with as few details in the array diff --git a/app/Repositories/Category/OperationsRepository.php b/app/Repositories/Category/OperationsRepository.php index 0b45f367e7..f20573c630 100644 --- a/app/Repositories/Category/OperationsRepository.php +++ b/app/Repositories/Category/OperationsRepository.php @@ -40,17 +40,6 @@ class OperationsRepository implements OperationsRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - die(__METHOD__); - } - } - /** * This method returns a list of all the withdrawal transaction journals (as arrays) set in that period * which have the specified category set to them. It's grouped per currency, with as few details in the array diff --git a/app/Repositories/Journal/JournalCLIRepository.php b/app/Repositories/Journal/JournalCLIRepository.php index 4e7e853078..449cf7bdff 100644 --- a/app/Repositories/Journal/JournalCLIRepository.php +++ b/app/Repositories/Journal/JournalCLIRepository.php @@ -42,16 +42,6 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * Get all transaction journals with a specific type, regardless of user. * diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 34b8d8fbd0..6fb1b7c16b 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -49,16 +49,6 @@ class JournalRepository implements JournalRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * Search in journal descriptions. * diff --git a/app/Repositories/LinkType/LinkTypeRepository.php b/app/Repositories/LinkType/LinkTypeRepository.php index 440719d427..cbb404f007 100644 --- a/app/Repositories/LinkType/LinkTypeRepository.php +++ b/app/Repositories/LinkType/LinkTypeRepository.php @@ -40,16 +40,6 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @param LinkType $linkType * diff --git a/app/Repositories/ObjectGroup/ObjectGroupRepository.php b/app/Repositories/ObjectGroup/ObjectGroupRepository.php index 763bfc3ba9..4522dce4a8 100644 --- a/app/Repositories/ObjectGroup/ObjectGroupRepository.php +++ b/app/Repositories/ObjectGroup/ObjectGroupRepository.php @@ -39,16 +39,6 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @inheritDoc */ diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index c8cd0ece86..ac49dd5c41 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -41,16 +41,6 @@ class RuleRepository implements RuleRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @return int */ diff --git a/app/Repositories/RuleGroup/RuleGroupRepository.php b/app/Repositories/RuleGroup/RuleGroupRepository.php index 063e714e79..88e8171cfc 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepository.php +++ b/app/Repositories/RuleGroup/RuleGroupRepository.php @@ -37,16 +37,6 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @return int */ diff --git a/app/Repositories/Tag/OperationsRepository.php b/app/Repositories/Tag/OperationsRepository.php index f0ac3bab99..45c3af02ea 100644 --- a/app/Repositories/Tag/OperationsRepository.php +++ b/app/Repositories/Tag/OperationsRepository.php @@ -40,17 +40,6 @@ class OperationsRepository implements OperationsRepositoryInterface /** @var User */ private $user; - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - die(__METHOD__); - } - } - /** * This method returns a list of all the withdrawal transaction journals (as arrays) set in that period * which have the specified tag(s) set to them. It's grouped per currency, with as few details in the array diff --git a/app/Repositories/TransactionGroup/TransactionGroupRepository.php b/app/Repositories/TransactionGroup/TransactionGroupRepository.php index 669ece4102..4404b1966d 100644 --- a/app/Repositories/TransactionGroup/TransactionGroupRepository.php +++ b/app/Repositories/TransactionGroup/TransactionGroupRepository.php @@ -53,25 +53,13 @@ use Log; */ class TransactionGroupRepository implements TransactionGroupRepositoryInterface { - /** @var User */ - private $user; - - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - app('log')->warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } + private User $user; /** * @param TransactionGroup $group */ public function destroy(TransactionGroup $group): void { - /** @var TransactionGroupDestroyService $service */ $service = new TransactionGroupDestroyService; $service->destroy($group); } diff --git a/app/Services/Currency/ExchangeRateInterface.php b/app/Rules/IsDuplicateTransaction.php similarity index 51% rename from app/Services/Currency/ExchangeRateInterface.php rename to app/Rules/IsDuplicateTransaction.php index e70971c0fc..d14128bd88 100644 --- a/app/Services/Currency/ExchangeRateInterface.php +++ b/app/Rules/IsDuplicateTransaction.php @@ -1,7 +1,7 @@ . */ -declare(strict_types=1); -namespace FireflyIII\Services\Currency; +namespace FireflyIII\Rules; -use Carbon\Carbon; -use FireflyIII\Models\CurrencyExchangeRate; -use FireflyIII\Models\TransactionCurrency; -use FireflyIII\User; + +use Illuminate\Contracts\Validation\Rule; /** - * Interface ExchangeRateInterface + * Class IsDuplicateTransaction */ -interface ExchangeRateInterface +class IsDuplicateTransaction implements Rule { - /** - * @param TransactionCurrency $fromCurrency - * @param TransactionCurrency $toCurrency - * @param Carbon $date - * - * @return CurrencyExchangeRate - */ - public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate; + private string $value; /** - * @param User $user - * - * @return mixed + * @inheritDoc */ - public function setUser(User $user); -} + public function passes($attribute, $value) + { + $this->value = $value; + return false; + } + + /** + * @inheritDoc + */ + public function message() + { + return $this->value; + } +} \ No newline at end of file diff --git a/app/Services/Currency/FixerIOv2.php b/app/Services/Currency/FixerIOv2.php deleted file mode 100644 index 05efb698e9..0000000000 --- a/app/Services/Currency/FixerIOv2.php +++ /dev/null @@ -1,137 +0,0 @@ -. - */ -declare(strict_types=1); - -namespace FireflyIII\Services\Currency; - -use Carbon\Carbon; -use Exception; -use FireflyIII\Models\CurrencyExchangeRate; -use FireflyIII\Models\TransactionCurrency; -use FireflyIII\User; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\GuzzleException; -use Log; - -/** - * Class FixerIOv2. - */ -class FixerIOv2 implements ExchangeRateInterface -{ - /** @var User */ - protected $user; - - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - - /** - * @param TransactionCurrency $fromCurrency - * @param TransactionCurrency $toCurrency - * @param Carbon $date - * - * @return CurrencyExchangeRate - * @throws Exception - */ - public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate - { - // create new exchange rate with default values. - $rate = 0; - $exchangeRate = new CurrencyExchangeRate; - $exchangeRate->user()->associate($this->user); - $exchangeRate->fromCurrency()->associate($fromCurrency); - $exchangeRate->toCurrency()->associate($toCurrency); - $exchangeRate->date = $date; - $exchangeRate->rate = $rate; - $exchangeRate->updated_at = today(config('app.timezone')); - $exchangeRate->created_at = today(config('app.timezone')); - - // get API key - $apiKey = config('firefly.fixer_api_key'); - - // if no API key, return unsaved exchange rate. - if ('' === $apiKey) { - Log::warning('No fixer.IO API key, will not do conversion.'); - - return $exchangeRate; - } - - // build URI - $uri = sprintf( - 'http://data.fixer.io/api/%s?access_key=%s&base=%s&symbols=%s', - $date->format('Y-m-d'), $apiKey, $fromCurrency->code, $toCurrency->code - ); - Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri))); - $client = new Client; - try { - - $res = $client->request('GET', $uri); - $statusCode = $res->getStatusCode(); - $body = $res->getBody()->getContents(); - } catch (GuzzleException|Exception $e) { - // don't care about error - $body = sprintf('Guzzle exception: %s', $e->getMessage()); - $statusCode = 500; - } - Log::debug(sprintf('Result status code is %d', $statusCode)); - Log::debug(sprintf('Result body is: %s', $body)); - - $content = null; - if (200 !== $statusCode) { - Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body)); - } - $success = false; - // get rate from body: - if (200 === $statusCode) { - $content = json_decode($body, true); - $success = $content['success'] ?? false; - } - if (null !== $content && true === $success) { - $code = $toCurrency->code; - $rate = (float)($content['rates'][$code] ?? 0); - Log::debug('Got the following rates from Fixer: ', $content['rates'] ?? []); - } - - $exchangeRate->rate = $rate; - if (0.0 !== $rate) { - Log::debug('Rate is not zero, save it!'); - $exchangeRate->save(); - } - - return $exchangeRate; - } - - /** - * @param User $user - * - * @return void - */ - public function setUser(User $user) - { - $this->user = $user; - } -} diff --git a/app/Services/Currency/RatesApiIOv1.php b/app/Services/Currency/RatesApiIOv1.php deleted file mode 100644 index 903e31ec0b..0000000000 --- a/app/Services/Currency/RatesApiIOv1.php +++ /dev/null @@ -1,126 +0,0 @@ -. - */ -declare(strict_types=1); - -namespace FireflyIII\Services\Currency; - -use Carbon\Carbon; -use Exception; -use FireflyIII\Models\CurrencyExchangeRate; -use FireflyIII\Models\TransactionCurrency; -use FireflyIII\User; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\GuzzleException; -use Log; - -/** - * Class RatesApiIOv1. - * @codeCoverageIgnore - */ -class RatesApiIOv1 implements ExchangeRateInterface -{ - /** @var User */ - protected $user; - - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - - /** - * @param TransactionCurrency $fromCurrency - * @param TransactionCurrency $toCurrency - * @param Carbon $date - * - * @return CurrencyExchangeRate - */ - public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate - { - // create new exchange rate with default values. - $rate = 0; - $exchangeRate = new CurrencyExchangeRate; - $exchangeRate->user()->associate($this->user); - $exchangeRate->fromCurrency()->associate($fromCurrency); - $exchangeRate->toCurrency()->associate($toCurrency); - $exchangeRate->date = $date; - $exchangeRate->rate = $rate; - $exchangeRate->updated_at = today(config('app.timezone')); - $exchangeRate->created_at = today(config('app.timezone')); - - // build URI - $uri = sprintf( - 'https://api.ratesapi.io/api/%s?base=%s&symbols=%s', - $date->format('Y-m-d'), $fromCurrency->code, $toCurrency->code - ); - Log::debug(sprintf('Going to request exchange rate using URI %s', $uri)); - $client = new Client; - try { - $res = $client->request('GET', $uri); - $statusCode = $res->getStatusCode(); - $body = $res->getBody()->getContents(); - } catch (GuzzleException|Exception $e) { - // don't care about error - $body = sprintf('Guzzle exception: %s', $e->getMessage()); - $statusCode = 500; - } - Log::debug(sprintf('Result status code is %d', $statusCode)); - Log::debug(sprintf('Result body is: %s', $body)); - - $content = null; - if (200 !== $statusCode) { - Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from RatesApiIO.', $statusCode, $body)); - } - $success = false; - // get rate from body: - if (200 === $statusCode) { - $content = json_decode($body, true); - $success = true; - } - if (null !== $content && true === $success) { - $code = $toCurrency->code; - $rate = (float)($content['rates'][$code] ?? 0); - Log::debug('Got the following rates from RatesApi: ', $content['rates'] ?? []); - } - - $exchangeRate->rate = $rate; - if (0.0 !== $rate) { - Log::debug('Rate is not zero, save it!'); - $exchangeRate->save(); - } - - return $exchangeRate; - } - - /** - * @param User $user - * - * @return void - */ - public function setUser(User $user) - { - $this->user = $user; - } -} diff --git a/app/Support/Http/Controllers/AugumentData.php b/app/Support/Http/Controllers/AugumentData.php index 74d0fc7930..eb9002da70 100644 --- a/app/Support/Http/Controllers/AugumentData.php +++ b/app/Support/Http/Controllers/AugumentData.php @@ -210,12 +210,19 @@ trait AugumentData return $cache->get(); // @codeCoverageIgnore } - $set = $blRepository->getBudgetLimits($budget, $start, $end); - $limits = new Collection(); - + $set = $blRepository->getBudgetLimits($budget, $start, $end); + $limits = new Collection(); + $budgetCollection = new Collection([$budget]); /** @var BudgetLimit $entry */ foreach ($set as $entry) { - $entry->spent = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date); + $currency = $entry->transactionCurrency; + // clone because these objects change each other. + $currentStart = clone $start; + $currentEnd = clone $end; + $expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $currency); + $spent = $expenses[(int)$currency->id]['sum'] ?? '0'; + $entry->spent = $spent; + $limits->push($entry); } $cache->store($limits); diff --git a/config/mail.php b/config/mail.php index 57d7998ba6..0582881016 100644 --- a/config/mail.php +++ b/config/mail.php @@ -65,7 +65,7 @@ return [ 'log' => [ 'transport' => 'log', 'channel' => env('MAIL_LOG_CHANNEL', 'stack'), - 'level' => 'debug', + 'level' => 'notice', ], 'array' => [