diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php index 87e10738ce..9c4cd303a9 100644 --- a/app/Handlers/Events/UserEventHandler.php +++ b/app/Handlers/Events/UserEventHandler.php @@ -13,17 +13,14 @@ declare(strict_types = 1); namespace FireflyIII\Handlers\Events; -use Exception; -use FireflyConfig; use FireflyIII\Events\RegisteredUser; use FireflyIII\Events\RequestedNewPassword; use FireflyIII\Events\ResentConfirmation; use FireflyIII\Repositories\User\UserRepositoryInterface; -use FireflyIII\User; +use FireflyIII\Support\Events\SendUserMail; use Illuminate\Mail\Message; use Log; use Mail; -use Preferences; use Session; use Swift_TransportException; @@ -82,7 +79,9 @@ class UserEventHandler */ public function sendConfirmationMessage(RegisteredUser $event): bool { - return $this->sendConfirmation($event->user, $event->ipAddress); + $sender = new SendUserMail; + + return $sender->sendConfirmation($event->user, $event->ipAddress); } /** @@ -96,7 +95,9 @@ class UserEventHandler */ function sendConfirmationMessageAgain(ResentConfirmation $event): bool { - return $this->sendConfirmation($event->user, $event->ipAddress); + $sender = new SendUserMail; + + return $sender->sendConfirmation($event->user, $event->ipAddress); } @@ -159,43 +160,4 @@ class UserEventHandler return true; } - - /** - * @param User $user - * @param string $ipAddress - * - * @return bool - */ - private function sendConfirmation(User $user, string $ipAddress): bool - { - $mustConfirmAccount = FireflyConfig::get('must_confirm_account', config('firefly.configuration.must_confirm_account'))->data; - if ($mustConfirmAccount === false) { - Preferences::setForUser($user, 'user_confirmed', true); - Preferences::setForUser($user, 'user_confirmed_last_mail', 0); - Preferences::mark(); - - return true; - } - $email = $user->email; - $code = str_random(16); - $route = route('do_confirm_account', [$code]); - Preferences::setForUser($user, 'user_confirmed', false); - Preferences::setForUser($user, 'user_confirmed_last_mail', time()); - Preferences::setForUser($user, 'user_confirmed_code', $code); - try { - Mail::send( - ['emails.confirm-account-html', 'emails.confirm-account-text'], ['route' => $route, 'ip' => $ipAddress], - function (Message $message) use ($email) { - $message->to($email, $email)->subject('Please confirm your Firefly III account'); - } - ); - } catch (Swift_TransportException $e) { - Log::error($e->getMessage()); - } catch (Exception $e) { - Log::error($e->getMessage()); - } - - return true; - } - } diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php index 18cdd71acc..55896229f9 100644 --- a/app/Helpers/Report/BalanceReportHelper.php +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -31,6 +31,7 @@ use Log; /** * Class BalanceReportHelper * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) // I can't really help it. * @package FireflyIII\Helpers\Report */ class BalanceReportHelper implements BalanceReportHelperInterface diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php index d79b9617b3..18f9e4016f 100644 --- a/app/Helpers/Report/BudgetReportHelper.php +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -43,6 +43,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface } /** + * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5. * @param Carbon $start * @param Carbon $end * @param Collection $accounts @@ -57,12 +58,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface /** @var Budget $budget */ foreach ($set as $budget) { $repetitions = $budget->limitrepetitions()->before($end)->after($start)->get(); - - // no repetition(s) for this budget: - if ($repetitions->count() == 0) { - // spent for budget in time range: - $spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end); - + if ($repetitions->count() == 0) { // no repetition(s) for this budget + $spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);// spent for budget in time range if ($spent > 0) { $budgetLine = new BudgetLine; $budgetLine->setBudget($budget)->setOverspent($spent); @@ -70,11 +67,9 @@ class BudgetReportHelper implements BudgetReportHelperInterface } continue; } - // one or more repetitions for budget: /** @var LimitRepetition $repetition */ - foreach ($repetitions as $repetition) { - $data = $this->calculateExpenses($budget, $repetition, $accounts); - + foreach ($repetitions as $repetition) { // one or more repetitions for budget + $data = $this->calculateExpenses($budget, $repetition, $accounts); $budgetLine = new BudgetLine; $budgetLine->setBudget($budget)->setRepetition($repetition) ->setLeft($data['left'])->setSpent($data['expenses'])->setOverspent($data['overspent']) @@ -84,12 +79,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface ->addLeft($data['left'])->addOverspent($data['overspent'])->addBudgetLine($budgetLine); } - } - - // stuff outside of budgets: - - $noBudget = $this->repository->spentInPeriodWithoutBudget($accounts, $start, $end); + $noBudget = $this->repository->spentInPeriodWithoutBudget($accounts, $start, $end); // stuff outside of budgets $budgetLine = new BudgetLine; $budgetLine->setOverspent($noBudget)->setSpent($noBudget); $object->addOverspent($noBudget)->addBudgetLine($budgetLine); diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index ae0e4fa800..71d80119a4 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -53,6 +53,8 @@ class ReportHelper implements ReportHelperInterface * This method generates a full report for the given period on all * the users bills and their payments. * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5. + * * Excludes bills which have not had a payment on the mentioned accounts. * * @param Carbon $start @@ -80,8 +82,6 @@ class ReportHelper implements ReportHelperInterface $billLine->setMin(strval($bill->amount_min)); $billLine->setMax(strval($bill->amount_max)); $billLine->setHit(false); - // is hit in period? - $entry = $journals->filter( function (Transaction $transaction) use ($bill) { return $transaction->bill_id === $bill->id; @@ -94,14 +94,10 @@ class ReportHelper implements ReportHelperInterface $billLine->setLastHitDate($first->date); $billLine->setHit(true); } - - // bill is active, or bill is hit: if ($billLine->isActive() || $billLine->isHit()) { $collection->addBill($billLine); } } - - // do some extra filtering. $collection->filterBills(); return $collection; diff --git a/app/Http/Controllers/Admin/ConfigurationController.php b/app/Http/Controllers/Admin/ConfigurationController.php index 0f9e62b65a..13238872cc 100644 --- a/app/Http/Controllers/Admin/ConfigurationController.php +++ b/app/Http/Controllers/Admin/ConfigurationController.php @@ -65,7 +65,7 @@ class ConfigurationController extends Controller return view( 'admin.configuration.index', - compact('subTitle', 'subTitleIcon', 'singleUserMode', 'mustConfirmAccount', 'isDemoSite', 'sendErrorMessage', 'siteOwner') + compact('subTitle', 'subTitleIcon', 'singleUserMode', 'mustConfirmAccount', 'isDemoSite', 'siteOwner') ); } diff --git a/app/Support/Events/SendUserMail.php b/app/Support/Events/SendUserMail.php new file mode 100644 index 0000000000..59a1ebc66c --- /dev/null +++ b/app/Support/Events/SendUserMail.php @@ -0,0 +1,62 @@ +data; + if ($mustConfirmAccount === false) { + Preferences::setForUser($user, 'user_confirmed', true); + Preferences::setForUser($user, 'user_confirmed_last_mail', 0); + Preferences::mark(); + + return true; + } + $email = $user->email; + $code = str_random(16); + $route = route('do_confirm_account', [$code]); + Preferences::setForUser($user, 'user_confirmed', false); + Preferences::setForUser($user, 'user_confirmed_last_mail', time()); + Preferences::setForUser($user, 'user_confirmed_code', $code); + try { + Mail::send( + ['emails.confirm-account-html', 'emails.confirm-account-text'], ['route' => $route, 'ip' => $ipAddress], + function (Message $message) use ($email) { + $message->to($email, $email)->subject('Please confirm your Firefly III account'); + } + ); + } catch (Swift_TransportException $e) { + Log::error($e->getMessage()); + } catch (Exception $e) { + Log::error($e->getMessage()); + } + + return true; + } +} \ No newline at end of file diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index 9555f3e00c..b5bdf896a2 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -318,30 +318,6 @@ class FireflyValidator extends Validator } - /** - * @param $attribute - * @param $value - * @param $parameters - * - * - * @return bool - */ - public function validateUniqueForUser($attribute, $value, $parameters): bool - { - $query = DB::table($parameters[0])->where($parameters[1], $value); - $query->where('user_id', auth()->user()->id); - if (isset($parameters[2])) { - $query->where('id', '!=', $parameters[2]); - } - $count = $query->count(); - if ($count == 0) { - return true; - } - - return false; - - } - /** * Validate an object and its unicity. Checks for encryption / encrypted values as well. * diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index c889b7df44..8a65c4ee73 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -26,7 +26,6 @@ $factory->define( static $password; return [ - 'name' => $faker->name, 'email' => $faker->safeEmail, 'password' => $password ?: $password = bcrypt('secret'), 'remember_token' => str_random(10), diff --git a/tests/unit/Models/TransactionTypeTest.php b/tests/unit/Models/TransactionTypeTest.php index e0c3f2aa86..c47a5f2d6b 100644 --- a/tests/unit/Models/TransactionTypeTest.php +++ b/tests/unit/Models/TransactionTypeTest.php @@ -19,6 +19,7 @@ class TransactionTypeTest extends TestCase { public function testIsDeposit() { + $transactionType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); $this->assertTrue($transactionType->isDeposit()); }