From a51d752a35fb0c0eef23104d0939d6dc09656fb2 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 3 Mar 2016 09:00:15 +0100 Subject: [PATCH] Update rule group repository and provider. --- app/Providers/FireflyServiceProvider.php | 1 - app/Providers/RuleGroupServiceProvider.php | 50 +++++++++++++++++++ .../RuleGroup/RuleGroupRepository.php | 30 ++++++++--- 3 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 app/Providers/RuleGroupServiceProvider.php diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 8b7eba73ff..7bdd87aca8 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -87,7 +87,6 @@ class FireflyServiceProvider extends ServiceProvider $this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository'); $this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository'); $this->app->bind('FireflyIII\Repositories\Rule\RuleRepositoryInterface', 'FireflyIII\Repositories\Rule\RuleRepository'); - $this->app->bind('FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface', 'FireflyIII\Repositories\RuleGroup\RuleGroupRepository'); $this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); // CSV import diff --git a/app/Providers/RuleGroupServiceProvider.php b/app/Providers/RuleGroupServiceProvider.php new file mode 100644 index 0000000000..82a9566303 --- /dev/null +++ b/app/Providers/RuleGroupServiceProvider.php @@ -0,0 +1,50 @@ +app->bind( + 'FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface', + function (Application $app, array $arguments) { + if (!isset($arguments[0]) && Auth::check()) { + return app('FireflyIII\Repositories\RuleGroup\RuleGroupRepository', [Auth::user()]); + } else { + if (!isset($arguments[0]) && !Auth::check()) { + throw new FireflyException('There is no user present.'); + } + } + + return app('FireflyIII\Repositories\RuleGroup\RuleGroupRepository', $arguments); + } + ); + } +} diff --git a/app/Repositories/RuleGroup/RuleGroupRepository.php b/app/Repositories/RuleGroup/RuleGroupRepository.php index 61e745b21f..a0f809f9a4 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepository.php +++ b/app/Repositories/RuleGroup/RuleGroupRepository.php @@ -10,7 +10,7 @@ use FireflyIII\Models\RuleGroup; use FireflyIII\User; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Collection; - +use Log; /** * Class RuleGroupRepository * @@ -18,12 +18,26 @@ use Illuminate\Support\Collection; */ class RuleGroupRepository implements RuleGroupRepositoryInterface { + /** @var User */ + private $user; + + /** + * BillRepository constructor. + * + * @param User $user + */ + public function __construct(User $user) + { + Log::debug('Constructed piggy bank repository for user #' . $user->id . ' (' . $user->email . ')'); + $this->user = $user; + } + /** * @return int */ public function count() { - return Auth::user()->ruleGroups()->count(); + return $this->user->ruleGroups()->count(); } /** @@ -62,7 +76,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface */ public function get() { - return Auth::user()->ruleGroups()->orderBy('order', 'ASC')->get(); + return $this->user->ruleGroups()->orderBy('order', 'ASC')->get(); } /** @@ -70,7 +84,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface */ public function getHighestOrderRuleGroup() { - $entry = Auth::user()->ruleGroups()->max('order'); + $entry = $this->user->ruleGroups()->max('order'); return intval($entry); } @@ -112,7 +126,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface $order = $ruleGroup->order; // find the rule with order+1 and give it order-1 - $other = Auth::user()->ruleGroups()->where('order', ($order + 1))->first(); + $other = $this->user->ruleGroups()->where('order', ($order + 1))->first(); if ($other) { $other->order = ($other->order - 1); $other->save(); @@ -133,7 +147,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface $order = $ruleGroup->order; // find the rule with order-1 and give it order+1 - $other = Auth::user()->ruleGroups()->where('order', ($order - 1))->first(); + $other = $this->user->ruleGroups()->where('order', ($order - 1))->first(); if ($other) { $other->order = ($other->order + 1); $other->save(); @@ -149,9 +163,9 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface */ public function resetRuleGroupOrder() { - Auth::user()->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]); + $this->user->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]); - $set = Auth::user()->ruleGroups()->where('active', 1)->orderBy('order', 'ASC')->get(); + $set = $this->user->ruleGroups()->where('active', 1)->orderBy('order', 'ASC')->get(); $count = 1; /** @var RuleGroup $entry */ foreach ($set as $entry) {