Code cleanup

This commit is contained in:
James Cole
2024-12-22 08:43:12 +01:00
parent 5751f7e5a3
commit 565bd87959
574 changed files with 4600 additions and 4604 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ class APIEventHandler
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->find((int)$event->userId);
$user = $repository->find((int) $event->userId);
if (null !== $user) {
try {
+9 -9
View File
@@ -43,11 +43,16 @@ use Illuminate\Support\Facades\Notification;
*/
class AdminEventHandler
{
public function sendLoginAttemptNotification(UnknownUserAttemptedLogin $event): void
public function sendInvitationNotification(InvitationCreated $event): void
{
$sendMail = app('fireflyconfig')->get('notification_invite_created', true)->data;
if (false === $sendMail) {
return;
}
try {
$owner = new OwnerNotifiable();
Notification::send($owner, new UnknownUserLoginAttempt($event->address));
Notification::send($owner, new UserInvitation($owner, $event->invitee));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
@@ -65,16 +70,11 @@ class AdminEventHandler
}
}
public function sendInvitationNotification(InvitationCreated $event): void
public function sendLoginAttemptNotification(UnknownUserAttemptedLogin $event): void
{
$sendMail = app('fireflyconfig')->get('notification_invite_created', true)->data;
if (false === $sendMail) {
return;
}
try {
$owner = new OwnerNotifiable();
Notification::send($owner, new UserInvitation($owner, $event->invitee));
Notification::send($owner, new UnknownUserLoginAttempt($event->address));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
@@ -89,7 +89,7 @@ class BudgetLimitHandler
if (null === $viewRange || is_array($viewRange)) {
$viewRange = '1M';
}
$viewRange = (string)$viewRange;
$viewRange = (string) $viewRange;
$start = app('navigation')->startOfPeriod($budgetLimit->start_date, $viewRange);
$end = app('navigation')->startOfPeriod($budgetLimit->end_date, $viewRange);
@@ -119,7 +119,7 @@ class BudgetLimitHandler
// if not exists:
$currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
$daily = $this->getDailyAmount($budgetLimit);
$amount = bcmul($daily, (string)$currentPeriod->length(), 12);
$amount = bcmul($daily, (string) $currentPeriod->length(), 12);
// no need to calculate if period is equal.
if ($currentPeriod->equals($limitPeriod)) {
@@ -132,14 +132,14 @@ class BudgetLimitHandler
app('log')->debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
$availableBudget = new AvailableBudget(
[
'user_id' => $user->id,
'user_group_id' => $user->user_group_id,
'transaction_currency_id' => $budgetLimit->transaction_currency_id,
'start_date' => $current,
'start_date_tz' => $current->format('e'),
'end_date' => $currentEnd,
'end_date_tz' => $currentEnd->format('e'),
'amount' => $amount,
'user_id' => $user->id,
'user_group_id' => $user->user_group_id,
'transaction_currency_id' => $budgetLimit->transaction_currency_id,
'start_date' => $current,
'start_date_tz' => $current->format('e'),
'end_date' => $currentEnd,
'end_date_tz' => $currentEnd->format('e'),
'amount' => $amount,
]
);
$availableBudget->save();
@@ -202,7 +202,7 @@ class BudgetLimitHandler
$overlap = $abPeriod->overlap($limitPeriod);
if (null !== $overlap) {
$length = $overlap->length();
$daily = bcmul($this->getDailyAmount($budgetLimit), (string)$length);
$daily = bcmul($this->getDailyAmount($budgetLimit), (string) $length);
$newAmount = bcadd($newAmount, $daily);
}
}
@@ -230,7 +230,7 @@ class BudgetLimitHandler
boundaries: Boundaries::EXCLUDE_NONE()
);
$days = $limitPeriod->length();
$amount = bcdiv($budgetLimit->amount, (string)$days, 12);
$amount = bcdiv($budgetLimit->amount, (string) $days, 12);
app('log')->debug(
sprintf('Total amount for budget limit #%d is %s. Nr. of days is %d. Amount per day is %s', $budgetLimit->id, $budgetLimit->amount, $days, $amount)
);
+77 -77
View File
@@ -42,56 +42,6 @@ use Illuminate\Support\Facades\Notification;
class MFAHandler
{
public function sendMFAEnabledMail(EnabledMFA $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new EnabledMFANotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
public function sendNewMFABackupCodesMail(MFANewBackupCodes $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new NewBackupCodesNotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
public function sendBackupFewLeftMail(MFABackupFewLeft $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
@@ -118,6 +68,81 @@ class MFAHandler
}
}
public function sendBackupNoLeftMail(MFABackupNoLeft $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new MFABackupNoLeftNotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
public function sendMFADisabledMail(DisabledMFA $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new DisabledMFANotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
public function sendMFAEnabledMail(EnabledMFA $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new EnabledMFANotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
public function sendMFAFailedAttemptsMail(MFAManyFailedAttempts $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
@@ -144,14 +169,14 @@ class MFAHandler
}
}
public function sendBackupNoLeftMail(MFABackupNoLeft $event): void
public function sendNewMFABackupCodesMail(MFANewBackupCodes $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new MFABackupNoLeftNotification($user));
Notification::send($user, new NewBackupCodesNotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
@@ -193,29 +218,4 @@ class MFAHandler
app('log')->error($e->getTraceAsString());
}
}
public function sendMFADisabledMail(DisabledMFA $event): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
$user = $event->user;
try {
Notification::send($user, new DisabledMFANotification($user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
}
+76 -76
View File
@@ -300,6 +300,27 @@ class UserEventHandler
}
}
public function sendLoginAttemptNotification(UserAttemptedLogin $event): void
{
try {
Notification::send($event->user, new UserFailedLoginAttempt($event->user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
/**
* Send a new password to the user.
*/
@@ -371,6 +392,61 @@ class UserEventHandler
}
}
/**
* Sends a test message to an administrator.
*/
public function sendTestNotification(UserTestNotificationChannel $event): void
{
Log::debug(sprintf('Now in (user) sendTestNotification("%s")', $event->channel));
switch ($event->channel) {
case 'email':
$class = UserTestNotificationEmail::class;
break;
case 'slack':
$class = UserTestNotificationSlack::class;
break;
case 'ntfy':
$class = UserTestNotificationNtfy::class;
break;
case 'pushover':
$class = UserTestNotificationPushover::class;
break;
default:
app('log')->error(sprintf('Unknown channel "%s" in (user) sendTestNotification method.', $event->channel));
return;
}
Log::debug(sprintf('Will send %s as a notification.', $class));
try {
Notification::send($event->user, new $class($event->user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
Log::debug(sprintf('If you see no errors above this line, test notification was sent over channel "%s"', $event->channel));
}
/**
* @throws FireflyException
*/
@@ -430,80 +506,4 @@ class UserEventHandler
event(new DetectedNewIPAddress($user));
}
}
public function sendLoginAttemptNotification(UserAttemptedLogin $event): void
{
try {
Notification::send($event->user, new UserFailedLoginAttempt($event->user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
}
/**
* Sends a test message to an administrator.
*/
public function sendTestNotification(UserTestNotificationChannel $event): void
{
Log::debug(sprintf('Now in (user) sendTestNotification("%s")', $event->channel));
switch ($event->channel) {
case 'email':
$class = UserTestNotificationEmail::class;
break;
case 'slack':
$class = UserTestNotificationSlack::class;
break;
case 'ntfy':
$class = UserTestNotificationNtfy::class;
break;
case 'pushover':
$class = UserTestNotificationPushover::class;
break;
default:
app('log')->error(sprintf('Unknown channel "%s" in (user) sendTestNotification method.', $event->channel));
return;
}
Log::debug(sprintf('Will send %s as a notification.', $class));
try {
Notification::send($event->user, new $class($event->user));
} catch (\Exception $e) { // @phpstan-ignore-line
$message = $e->getMessage();
if (str_contains($message, 'Bcc')) {
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
if (str_contains($message, 'RFC 2822')) {
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
return;
}
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
Log::debug(sprintf('If you see no errors above this line, test notification was sent over channel "%s"', $event->channel));
}
}
@@ -49,7 +49,7 @@ class VersionCheckEventHandler
// should not check for updates:
$permission = app('fireflyconfig')->get('permission_update_check', -1);
$value = (int)$permission->data;
$value = (int) $permission->data;
if (1 !== $value) {
app('log')->debug('Update check is not enabled.');
$this->warnToCheckForUpdates($event);
@@ -111,7 +111,7 @@ class VersionCheckEventHandler
// last check time was more than a week ago.
app('log')->debug('Have warned about a new version in four weeks!');
session()->flash('info', (string)trans('firefly.disabled_but_check'));
session()->flash('info', (string) trans('firefly.disabled_but_check'));
app('fireflyconfig')->set('last_update_warning', time());
}
}
+25 -25
View File
@@ -35,6 +35,31 @@ use Illuminate\Support\Facades\Log;
*/
class AccountObserver
{
public function created(Account $account): void
{
Log::debug('Observe "created" of an account.');
$this->updateNativeAmount($account);
}
private function updateNativeAmount(Account $account): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$repository = app(AccountRepositoryInterface::class);
$currency = $repository->getAccountCurrency($account);
if (null !== $currency && $currency->id !== $userCurrency->id && '' !== (string) $account->virtual_balance && 0 !== bccomp($account->virtual_balance, '0')) {
$converter = new ExchangeRateConverter();
$converter->setIgnoreSettings(true);
$account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance);
}
if ('' === (string) $account->virtual_balance || ('' !== (string) $account->virtual_balance && 0 === bccomp($account->virtual_balance, '0'))) {
$account->virtual_balance = null;
$account->native_virtual_balance = null;
}
$account->saveQuietly();
Log::debug('Account native virtual balance is updated.');
}
/**
* Also delete related objects.
*/
@@ -57,34 +82,9 @@ class AccountObserver
$account->locations()->delete();
}
public function created(Account $account): void
{
Log::debug('Observe "created" of an account.');
$this->updateNativeAmount($account);
}
public function updated(Account $account): void
{
Log::debug('Observe "updated" of an account.');
$this->updateNativeAmount($account);
}
private function updateNativeAmount(Account $account): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$repository = app(AccountRepositoryInterface::class);
$currency = $repository->getAccountCurrency($account);
if (null !== $currency && $currency->id !== $userCurrency->id && '' !== (string) $account->virtual_balance && 0 !== bccomp($account->virtual_balance, '0')) {
$converter = new ExchangeRateConverter();
$converter->setIgnoreSettings(true);
$account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance);
}
if ('' === (string) $account->virtual_balance || ('' !== (string) $account->virtual_balance && 0 === bccomp($account->virtual_balance, '0'))) {
$account->virtual_balance = null;
$account->native_virtual_balance = null;
}
$account->saveQuietly();
Log::debug('Account native virtual balance is updated.');
}
}
+6 -6
View File
@@ -30,18 +30,18 @@ use Illuminate\Support\Facades\Log;
class AutoBudgetObserver
{
public function updated(AutoBudget $autoBudget): void
{
Log::debug('Observe "updated" of an auto budget.');
$this->updateNativeAmount($autoBudget);
}
public function created(AutoBudget $autoBudget): void
{
Log::debug('Observe "created" of an auto budget.');
$this->updateNativeAmount($autoBudget);
}
public function updated(AutoBudget $autoBudget): void
{
Log::debug('Observe "updated" of an auto budget.');
$this->updateNativeAmount($autoBudget);
}
private function updateNativeAmount(AutoBudget $autoBudget): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($autoBudget->budget->user->userGroup);
@@ -30,18 +30,18 @@ use Illuminate\Support\Facades\Log;
class AvailableBudgetObserver
{
public function updated(AvailableBudget $availableBudget): void
{
Log::debug('Observe "updated" of an available budget.');
$this->updateNativeAmount($availableBudget);
}
public function created(AvailableBudget $availableBudget): void
{
Log::debug('Observe "created" of an available budget.');
$this->updateNativeAmount($availableBudget);
}
public function updated(AvailableBudget $availableBudget): void
{
Log::debug('Observe "updated" of an available budget.');
$this->updateNativeAmount($availableBudget);
}
private function updateNativeAmount(AvailableBudget $availableBudget): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($availableBudget->user->userGroup);
+6 -6
View File
@@ -32,6 +32,12 @@ use Illuminate\Support\Facades\Log;
*/
class BillObserver
{
public function created(Bill $bill): void
{
Log::debug('Observe "created" of a bill.');
$this->updateNativeAmount($bill);
}
public function deleting(Bill $bill): void
{
app('log')->debug('Observe "deleting" of a bill.');
@@ -47,12 +53,6 @@ class BillObserver
$this->updateNativeAmount($bill);
}
public function created(Bill $bill): void
{
Log::debug('Observe "created" of a bill.');
$this->updateNativeAmount($bill);
}
private function updateNativeAmount(Bill $bill): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup);
@@ -30,18 +30,18 @@ use Illuminate\Support\Facades\Log;
class BudgetLimitObserver
{
public function updated(BudgetLimit $budgetLimit): void
{
Log::debug('Observe "updated" of a budget limit.');
$this->updateNativeAmount($budgetLimit);
}
public function created(BudgetLimit $budgetLimit): void
{
Log::debug('Observe "created" of a budget limit.');
$this->updateNativeAmount($budgetLimit);
}
public function updated(BudgetLimit $budgetLimit): void
{
Log::debug('Observe "updated" of a budget limit.');
$this->updateNativeAmount($budgetLimit);
}
private function updateNativeAmount(BudgetLimit $budgetLimit): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($budgetLimit->budget->user->userGroup);
@@ -30,18 +30,18 @@ use Illuminate\Support\Facades\Log;
class PiggyBankEventObserver
{
public function updated(PiggyBankEvent $event): void
{
Log::debug('Observe "updated" of a piggy bank event.');
$this->updateNativeAmount($event);
}
public function created(PiggyBankEvent $event): void
{
Log::debug('Observe "created" of a piggy bank event.');
$this->updateNativeAmount($event);
}
public function updated(PiggyBankEvent $event): void
{
Log::debug('Observe "updated" of a piggy bank event.');
$this->updateNativeAmount($event);
}
private function updateNativeAmount(PiggyBankEvent $event): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($event->piggyBank->accounts()->first()->user->userGroup);
+23 -23
View File
@@ -33,18 +33,35 @@ use Illuminate\Support\Facades\Log;
*/
class PiggyBankObserver
{
public function updated(PiggyBank $piggyBank): void
{
Log::debug('Observe "updated" of a piggy bank.');
$this->updateNativeAmount($piggyBank);
}
public function created(PiggyBank $piggyBank): void
{
Log::debug('Observe "created" of a piggy bank.');
$this->updateNativeAmount($piggyBank);
}
/**
* Also delete related objects.
*/
public function deleting(PiggyBank $piggyBank): void
{
app('log')->debug('Observe "deleting" of a piggy bank.');
foreach ($piggyBank->attachments()->get() as $attachment) {
$attachment->delete();
}
$piggyBank->piggyBankEvents()->delete();
$piggyBank->piggyBankRepetitions()->delete();
$piggyBank->notes()->delete();
}
public function updated(PiggyBank $piggyBank): void
{
Log::debug('Observe "updated" of a piggy bank.');
$this->updateNativeAmount($piggyBank);
}
private function updateNativeAmount(PiggyBank $piggyBank): void
{
$group = $piggyBank->accounts()->first()?->user->userGroup;
@@ -66,21 +83,4 @@ class PiggyBankObserver
$piggyBank->saveQuietly();
Log::debug('Piggy bank native target amount is updated.');
}
/**
* Also delete related objects.
*/
public function deleting(PiggyBank $piggyBank): void
{
app('log')->debug('Observe "deleting" of a piggy bank.');
foreach ($piggyBank->attachments()->get() as $attachment) {
$attachment->delete();
}
$piggyBank->piggyBankEvents()->delete();
$piggyBank->piggyBankRepetitions()->delete();
$piggyBank->notes()->delete();
}
}
+12 -12
View File
@@ -35,6 +35,18 @@ class TransactionObserver
{
public static bool $recalculate = true;
public function created(Transaction $transaction): void
{
Log::debug('Observe "created" of a transaction.');
if (config('firefly.feature_flags.running_balance_column')) {
if (1 === bccomp($transaction->amount, '0') && self::$recalculate) {
Log::debug('Trigger recalculateForJournal');
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
}
}
$this->updateNativeAmount($transaction);
}
public function deleting(?Transaction $transaction): void
{
app('log')->debug('Observe "deleting" of a transaction.');
@@ -53,18 +65,6 @@ class TransactionObserver
$this->updateNativeAmount($transaction);
}
public function created(Transaction $transaction): void
{
Log::debug('Observe "created" of a transaction.');
if (config('firefly.feature_flags.running_balance_column')) {
if (1 === bccomp($transaction->amount, '0') && self::$recalculate) {
Log::debug('Trigger recalculateForJournal');
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
}
}
$this->updateNativeAmount($transaction);
}
private function updateNativeAmount(Transaction $transaction): void
{
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($transaction->transactionJournal->user->userGroup);