Update rule group repository and provider.

This commit is contained in:
James Cole 2016-03-03 09:00:15 +01:00
parent 7800b0a7f5
commit a51d752a35
3 changed files with 72 additions and 9 deletions

View File

@ -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

View File

@ -0,0 +1,50 @@
<?php
namespace FireflyIII\Providers;
use Auth;
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
/**
* Class RuleGroupServiceProvider
*
* @package FireflyIII\Providers
*/
class RuleGroupServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->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);
}
);
}
}

View File

@ -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) {