Skip to content

Firefly III: a personal finances manager

Removed duplicate code.

James Coleda49afa37b7a
diff --git a/app/Handlers/Events/BudgetEventHandler.php b/app/Handlers/Events/BudgetEventHandler.php
index 86491613f3..b82aa6d749 100644
--- a/app/Handlers/Events/BudgetEventHandler.php
+++ b/app/Handlers/Events/BudgetEventHandler.php
@@ -14,8 +14,10 @@ declare(strict_types = 1);
namespace FireflyIII\Handlers\Events;
+use Carbon\Carbon;
use FireflyIII\Events\StoredBudgetLimit;
use FireflyIII\Events\UpdatedBudgetLimit;
+use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
use Illuminate\Database\QueryException;
use Log;
@@ -38,37 +40,9 @@ class BudgetEventHandler
*/
public function storeRepetition(StoredBudgetLimit $event):bool
{
- $budgetLimit = $event->budgetLimit;
- $end = $event->end;
- $set = $budgetLimit->limitrepetitions()
- ->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00'))
- ->where('enddate', $end->format('Y-m-d 00:00:00'))
- ->get();
- if ($set->count() == 0) {
- $repetition = new LimitRepetition;
- $repetition->startdate = $budgetLimit->startdate;
- $repetition->enddate = $end;
- $repetition->amount = $budgetLimit->amount;
- $repetition->budgetLimit()->associate($budgetLimit);
-
- try {
- $repetition->save();
- } catch (QueryException $e) {
- Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage());
- }
- }
-
- if ($set->count() == 1) {
- $repetition = $set->first();
- $repetition->amount = $budgetLimit->amount;
- $repetition->save();
-
- }
-
- return true;
+ return $this->processRepetitionChange($event->budgetLimit, $event->end);
}
-
/**
* Updates, if present the budget limit repetition part of a budget limit.
*
@@ -78,16 +52,25 @@ class BudgetEventHandler
*/
public function updateRepetition(UpdatedBudgetLimit $event): bool
{
- $budgetLimit = $event->budgetLimit;
- $end = $event->end;
- $set = $budgetLimit->limitrepetitions()
- ->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00'))
- ->where('enddate', $end->format('Y-m-d 00:00:00'))
- ->get();
+ return $this->processRepetitionChange($event->budgetLimit, $event->end);
+ }
+
+ /**
+ * @param BudgetLimit $budgetLimit
+ * @param Carbon $date
+ *
+ * @return bool
+ */
+ private function processRepetitionChange(BudgetLimit $budgetLimit, Carbon $date):bool
+ {
+ $set = $budgetLimit->limitrepetitions()
+ ->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00'))
+ ->where('enddate', $date->format('Y-m-d 00:00:00'))
+ ->get();
if ($set->count() == 0) {
$repetition = new LimitRepetition;
$repetition->startdate = $budgetLimit->startdate;
- $repetition->enddate = $end;
+ $repetition->enddate = $date;
$repetition->amount = $budgetLimit->amount;
$repetition->budgetLimit()->associate($budgetLimit);
diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php
index c6235d0a6d..bf10bb71ea 100644
--- a/app/Handlers/Events/UserEventHandler.php
+++ b/app/Handlers/Events/UserEventHandler.php
@@ -19,6 +19,7 @@ use FireflyIII\Events\ConfirmedUser;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\ResentConfirmation;
use FireflyIII\Repositories\User\UserRepositoryInterface;
+use FireflyIII\User;
use Illuminate\Mail\Message;
use Log;
use Mail;
@@ -82,36 +83,7 @@ class UserEventHandler
*/
public function sendConfirmationMessage(RegisteredUser $event): bool
{
- $user = $event->user;
- $ipAddress = $event->ipAddress;
- $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'], ['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;
+ return $this->sendConfirmation($event->user, $event->ipAddress);
}
/**
@@ -125,36 +97,7 @@ class UserEventHandler
*/
function sendConfirmationMessageAgain(ResentConfirmation $event): bool
{
- $user = $event->user;
- $ipAddress = $event->ipAddress;
- $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'], ['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;
+ return $this->sendConfirmation($event->user, $event->ipAddress);
}
@@ -223,4 +166,43 @@ class UserEventHandler
}
+
+ /**
+ * @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'], ['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/Import/Converter/TagSplit.php b/app/Import/Converter/TagSplit.php
new file mode 100644
index 0000000000..ab1cdf21be
--- /dev/null
+++ b/app/Import/Converter/TagSplit.php
@@ -0,0 +1,83 @@
+<?php
+/**
+ * TagSplit.php
+ * Copyright (C) 2016 [email protected]
+ *
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types = 1);
+
+namespace FireflyIII\Import\Converter;
+
+use FireflyIII\Repositories\Tag\TagRepositoryInterface;
+use FireflyIII\User;
+use Illuminate\Support\Collection;
+use Log;
+
+/**
+ * Class TagSplit
+ *
+ * @package FireflyIII\Import\Converter
+ */
+class TagSplit
+{
+
+ /**
+ * @param User $user
+ * @param array $mapping
+ * @param array $parts
+ *
+ * @return Collection
+ */
+ public static function createSetFromSplits(User $user, array $mapping, array $parts): Collection {
+ $set = new Collection;
+ Log::debug('Exploded parts.', $parts);
+
+ /** @var TagRepositoryInterface $repository */
+ $repository = app(TagRepositoryInterface::class, [$user]);
+
+
+ /** @var string $part */
+ foreach ($parts as $part) {
+ if (isset($mapping[$part])) {
+ Log::debug('Found tag in mapping. Should exist.', ['value' => $part, 'map' => $mapping[$part]]);
+ $tag = $repository->find(intval($mapping[$part]));
+ if (!is_null($tag->id)) {
+ Log::debug('Found tag by ID', ['id' => $tag->id]);
+
+ $set->push($tag);
+ continue;
+ }
+ }
+ // not mapped? Still try to find it first:
+ $tag = $repository->findByTag($part);
+ if (!is_null($tag->id)) {
+ Log::debug('Found tag by name ', ['id' => $tag->id]);
+
+ $set->push($tag);
+ }
+ if (is_null($tag->id)) {
+ // create new tag
+ $tag = $repository->store(
+ [
+ 'tag' => $part,
+ 'date' => null,
+ 'description' => $part,
+ 'latitude' => null,
+ 'longitude' => null,
+ 'zoomLevel' => null,
+ 'tagMode' => 'nothing',
+ ]
+ );
+ Log::debug('Created new tag', ['name' => $part, 'id' => $tag->id]);
+ $set->push($tag);
+ }
+ }
+ return $set;
+ }
+
+}
\ No newline at end of file
diff --git a/app/Import/Converter/TagsComma.php b/app/Import/Converter/TagsComma.php
index d4e4687e3a..e9fbbaeecd 100644
--- a/app/Import/Converter/TagsComma.php
+++ b/app/Import/Converter/TagsComma.php
@@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
-use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@@ -41,49 +40,7 @@ class TagsComma extends BasicConverter implements ConverterInterface
return new Collection;
}
$parts = array_unique(explode(',', $value));
- $set = new Collection;
- Log::debug('Exploded parts.', $parts);
-
- /** @var TagRepositoryInterface $repository */
- $repository = app(TagRepositoryInterface::class, [$this->user]);
-
-
- /** @var string $part */
- foreach ($parts as $part) {
- if (isset($this->mapping[$part])) {
- Log::debug('Found tag in mapping. Should exist.', ['value' => $part, 'map' => $this->mapping[$part]]);
- $tag = $repository->find(intval($this->mapping[$part]));
- if (!is_null($tag->id)) {
- Log::debug('Found tag by ID', ['id' => $tag->id]);
-
- $set->push($tag);
- continue;
- }
- }
- // not mapped? Still try to find it first:
- $tag = $repository->findByTag($part);
- if (!is_null($tag->id)) {
- Log::debug('Found tag by name ', ['id' => $tag->id]);
-
- $set->push($tag);
- }
- if (is_null($tag->id)) {
- // create new tag
- $tag = $repository->store(
- [
- 'tag' => $part,
- 'date' => null,
- 'description' => $part,
- 'latitude' => null,
- 'longitude' => null,
- 'zoomLevel' => null,
- 'tagMode' => 'nothing',
- ]
- );
- Log::debug('Created new tag', ['name' => $part, 'id' => $tag->id]);
- $set->push($tag);
- }
- }
+ $set = TagSplit::createSetFromSplits($this->user, $this->mapping, $parts);
$this->setCertainty(100);
return $set;
diff --git a/app/Import/Converter/TagsSpace.php b/app/Import/Converter/TagsSpace.php
index d2d86eb8eb..3c437bd94b 100644
--- a/app/Import/Converter/TagsSpace.php
+++ b/app/Import/Converter/TagsSpace.php
@@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
-use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@@ -41,49 +40,7 @@ class TagsSpace extends BasicConverter implements ConverterInterface
return new Collection;
}
$parts = array_unique(explode(' ', $value));
- $set = new Collection;
- Log::debug('Exploded parts.', $parts);
-
- /** @var TagRepositoryInterface $repository */
- $repository = app(TagRepositoryInterface::class, [$this->user]);
-
-
- /** @var string $part */
- foreach ($parts as $part) {
- if (isset($this->mapping[$part])) {
- Log::debug('Found tag in mapping. Should exist.', ['value' => $part, 'map' => $this->mapping[$part]]);
- $tag = $repository->find(intval($this->mapping[$part]));
- if (!is_null($tag->id)) {
- Log::debug('Found tag by ID', ['id' => $tag->id]);
-
- $set->push($tag);
- continue;
- }
- }
- // not mapped? Still try to find it first:
- $tag = $repository->findByTag($part);
- if (!is_null($tag->id)) {
- Log::debug('Found tag by name ', ['id' => $tag->id]);
-
- $set->push($tag);
- }
- if (is_null($tag->id)) {
- // create new tag
- $tag = $repository->store(
- [
- 'tag' => $part,
- 'date' => null,
- 'description' => $part,
- 'latitude' => null,
- 'longitude' => null,
- 'zoomLevel' => null,
- 'tagMode' => 'nothing',
- ]
- );
- Log::debug('Created new tag', ['name' => $part, 'id' => $tag->id]);
- $set->push($tag);
- }
- }
+ $set = TagSplit::createSetFromSplits($this->user, $this->mapping, $parts);
$this->setCertainty(100);
return $set;