Use facades.

This commit is contained in:
James Cole 2014-11-21 11:12:22 +01:00
parent ec776bb6eb
commit 3dce194930
22 changed files with 177 additions and 90 deletions

View File

@ -499,9 +499,6 @@ class GoogleChartController extends BaseController
public function recurringOverview(RecurringTransaction $recurring)
{
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = App::make('FireflyIII\Shared\Toolkit\Date');
/** @var \Grumpydictator\Gchart\GChart $chart */
$chart = App::make('gchart');
$chart->addColumn('Date', 'date');
@ -526,7 +523,7 @@ class GoogleChartController extends BaseController
}
unset($result);
$chart->addRow(clone $start, $recurring->amount_max, $recurring->amount_min, $amount);
$start = $dateKit->addPeriod($start, $recurring->repeat_freq, 0);
$start = DateKit::addPeriod($start, $recurring->repeat_freq, 0);
}
$chart->generate();
@ -557,9 +554,6 @@ class GoogleChartController extends BaseController
/** @var \FireflyIII\Database\Recurring $rcr */
$rcr = App::make('FireflyIII\Database\Recurring');
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = App::make('FireflyIII\Shared\Toolkit\Date');
$recurring = $rcr->get();
/** @var \RecurringTransaction $entry */
@ -580,7 +574,7 @@ class GoogleChartController extends BaseController
* Get end of period for $current:
*/
$currentEnd = clone $current;
$dateKit->endOfPeriod($currentEnd, $entry->repeat_freq);
DateKit::endOfPeriod($currentEnd, $entry->repeat_freq);
/*
* In the current session range?
@ -605,7 +599,7 @@ class GoogleChartController extends BaseController
/*
* Add some time for the next loop!
*/
$dateKit->addPeriod($current, $entry->repeat_freq, intval($entry->skip));
DateKit::addPeriod($current, $entry->repeat_freq, intval($entry->skip));
}

View File

@ -84,12 +84,9 @@ class HomeController extends BaseController
*/
public function sessionNext()
{
/** @var \FireflyIII\Shared\Toolkit\Navigation $navigation */
$navigation = App::make('FireflyIII\Shared\Toolkit\Navigation');
$navigation->next();
Navigation::next();
return Redirect::back();
//return Redirect::route('index');
}
/**
@ -97,11 +94,8 @@ class HomeController extends BaseController
*/
public function sessionPrev()
{
/** @var \FireflyIII\Shared\Toolkit\Navigation $navigation */
$navigation = App::make('FireflyIII\Shared\Toolkit\Navigation');
$navigation->prev();
Navigation::prev();
return Redirect::back();
//return Redirect::route('index');
}
}

View File

@ -48,13 +48,10 @@ class PiggybankController extends BaseController
/** @var \FireflyIII\Database\Account $acct */
$acct = App::make('FireflyIII\Database\Account');
/** @var \FireflyIII\Shared\Toolkit\Form $toolkit */
$toolkit = App::make('FireflyIII\Shared\Toolkit\Form');
$periods = Config::get('firefly.piggybank_periods');
$accounts = $toolkit->makeSelectList($acct->getAssetAccounts());
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
return View::make('piggybanks.create', compact('accounts', 'periods'))->with('title', 'Piggy banks')->with('mainTitleIcon', 'fa-sort-amount-asc')->with(
'subTitle', 'Create new piggy bank'
@ -99,12 +96,9 @@ class PiggybankController extends BaseController
/** @var \FireflyIII\Database\Account $acct */
$acct = App::make('FireflyIII\Database\Account');
/** @var \FireflyIII\Shared\Toolkit\Form $toolkit */
$toolkit = App::make('FireflyIII\Shared\Toolkit\Form');
$periods = Config::get('firefly.piggybank_periods');
$accounts = $toolkit->makeSelectList($acct->getAssetAccounts());
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
/*
* Flash some data to fill the form.

View File

@ -21,20 +21,15 @@ class ReminderController extends BaseController
{
$amount = null;
$actionURL = '#';
if (get_class($reminder->remindersable) == 'Piggybank') {
/** @var \FireflyIII\Shared\Toolkit\Reminders $toolkit */
$reminderKit = App::make('FireflyIII\Shared\Toolkit\Reminders');
$amount = $reminderKit->amountForReminder($reminder);
$amount = Reminders::amountForReminder($reminder);
}
return View::make('reminders.show', compact('reminder', 'amount'));
}
public function act(Reminder $reminder) {
/** @var \FireflyIII\Shared\Toolkit\Reminders $toolkit */
$reminderKit = App::make('FireflyIII\Shared\Toolkit\Reminders');
switch(get_class($reminder->remindersable)) {
default:
@ -42,7 +37,7 @@ class ReminderController extends BaseController
break;
break;
case 'Piggybank':
$amount = $reminderKit->amountForReminder($reminder);
$amount = Reminders::amountForReminder($reminder);
$prefilled = [
'amount' => round($amount,2),
'description' => 'Money for ' . $reminder->remindersable->name,

View File

@ -34,8 +34,6 @@ class TransactionController extends BaseController
/*
* The repositories we need:
*/
/** @var \FireflyIII\Shared\Toolkit\Form $form */
$form = App::make('FireflyIII\Shared\Toolkit\Form');
/** @var \FireflyIII\Database\Account $accountRepository */
$accountRepository = App::make('FireflyIII\Database\Account');
@ -47,14 +45,14 @@ class TransactionController extends BaseController
$piggyRepository = App::make('FireflyIII\Database\Piggybank');
// get asset accounts with names and id's .
$assetAccounts = $form->makeSelectList($accountRepository->getAssetAccounts());
$assetAccounts = FFForm::makeSelectList($accountRepository->getAssetAccounts());
// get budgets as a select list.
$budgets = $form->makeSelectList($budgetRepository->get());
$budgets = FFForm::makeSelectList($budgetRepository->get());
$budgets[0] = '(no budget)';
// get the piggy banks.
$piggies = $form->makeSelectList($piggyRepository->get());
$piggies = FFForm::makeSelectList($piggyRepository->get());
$piggies[0] = '(no piggy bank)';
/*
@ -135,8 +133,6 @@ class TransactionController extends BaseController
/*
* All the repositories we need:
*/
/** @var \FireflyIII\Shared\Toolkit\Form $form */
$form = App::make('FireflyIII\Shared\Toolkit\Form');
/** @var \FireflyIII\Database\Account $accountRepository */
$accountRepository = App::make('FireflyIII\Database\Account');
@ -152,10 +148,10 @@ class TransactionController extends BaseController
$what = strtolower($journal->transactiontype->type);
// get asset accounts with names and id's.
$accounts = $form->makeSelectList($accountRepository->getAssetAccounts());
$accounts = FFForm::makeSelectList($accountRepository->getAssetAccounts());
// get budgets as a select list.
$budgets = $form->makeSelectList($budgetRepository->get());
$budgets = FFForm::makeSelectList($budgetRepository->get());
$budgets[0] = '(no budget)';
/*
@ -163,7 +159,7 @@ class TransactionController extends BaseController
* of the transactions in the journal has this field, it should all fill in nicely.
*/
// get the piggy banks.
$piggies = $form->makeSelectList($piggyRepository->get());
$piggies = FFForm::makeSelectList($piggyRepository->get());
$piggies[0] = '(no piggy bank)';
$piggyBankId = 0;
foreach ($journal->transactions as $t) {

View File

@ -7,16 +7,9 @@ App::before(
$reminders = [];
if (Auth::check()) {
/** @var \FireflyIII\Shared\Toolkit\Filter $toolkit */
$filter = App::make('FireflyIII\Shared\Toolkit\Filter');
$filter->setSessionDateRange();
/** @var \FireflyIII\Shared\Toolkit\Reminders $toolkit */
$reminderKit = App::make('FireflyIII\Shared\Toolkit\Reminders');
$reminderKit->updateReminders();
$reminders = $reminderKit->getReminders();
Filter::setSessionDateRange();
Reminders::updateReminders();
$reminders = Reminders::getReminders();
}
View::share('reminders', $reminders);

View File

@ -238,7 +238,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
{
// Add expenses that have no budget:
return \Auth::user()->transactionjournals()->whereNotIn(
return $this->getUser()->transactionjournals()->whereNotIn(
'transaction_journals.id', function ($query) use ($start, $end) {
$query->select('transaction_journals.id')->from('transaction_journals')->leftJoin(
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'

View File

@ -67,9 +67,7 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
/*
* Jump to the start of the period.
*/
/** @var \FireflyIII\Shared\Toolkit\Date $toolkit */
$toolkit = \App::make('FireflyIII\Shared\Toolkit\Date');
$date = $toolkit->startOfPeriod($date, $data['repeat_freq']);
$date = DateKit::startOfPeriod($date, $data['repeat_freq']);
$recurring->date = $date;
$recurring->skip = intval($data['skip']);

View File

@ -407,7 +407,7 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
// }
$validator = \Validator::make([$model], \Transaction::$rules);
$validator = \Validator::make([$model], \TransactionJournal::$rules);
if ($validator->invalid()) {
$errors->merge($errors);
}

View File

@ -13,11 +13,9 @@ class Budget
*/
public function storeOrUpdateLimit(\Limit $limit)
{
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = \App::make('FireflyIII\Shared\Toolkit\Date');
$end = $dateKit->addPeriod(clone $limit->startdate, $limit->repeat_freq, 0);
$end = DateKit::addPeriod(clone $limit->startdate, $limit->repeat_freq, 0);
$end->subDay();
$set = $limit->limitrepetitions()->where('startdate', $limit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->get();

View File

@ -2,6 +2,7 @@
namespace FireflyIII;
use FireflyIII\Shared\Validation\FireflyValidator;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
/**
@ -21,6 +22,16 @@ class FF3ServiceProvider extends ServiceProvider
);
}
/**
* Return the services bla bla.
*
* @return array
*/
public function provides()
{
return ['reminders', 'filters', 'datekit', 'navigation'];
}
/**
* Triggered automatically by Laravel
*/
@ -29,12 +40,50 @@ class FF3ServiceProvider extends ServiceProvider
// FORMAT:
#$this->app->bind('Interface', 'Class');
$this->app->bind(
'reminders', function () {
return new \FireflyIII\Shared\Toolkit\Reminders;
}
);
$this->app->bind(
'filter', function () {
return new \FireflyIII\Shared\Toolkit\Filter;
}
);
$this->app->bind(
'datekit', function () {
return new \FireflyIII\Shared\Toolkit\Date;
}
);
$this->app->bind(
'navigation', function () {
return new \FireflyIII\Shared\Toolkit\Navigation;
}
);
$this->app->bind(
'ffform', function () {
return new \FireflyIII\Shared\Toolkit\Form;
}
);
// preferences:
$this->app->bind('FireflyIII\Shared\Preferences\PreferencesInterface', 'FireflyIII\Shared\Preferences\Preferences');
// registration and user mail:
$this->app->bind('FireflyIII\Shared\Mail\RegistrationInterface', 'FireflyIII\Shared\Mail\Registration');
// Shortcut so developers don't need to add an Alias in app/config/app.php
$this->app->booting(
function () {
$loader = AliasLoader::getInstance();
$loader->alias('Reminders', 'FireflyIII\Shared\Facade\Reminders');
$loader->alias('Filter', 'FireflyIII\Shared\Facade\Filter');
$loader->alias('DateKit', 'FireflyIII\Shared\Facade\DateKit');
$loader->alias('Navigation', 'FireflyIII\Shared\Facade\Navigation');
$loader->alias('FFForm', 'FireflyIII\Shared\Facade\FFForm');
}
);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace FireflyIII\Shared\Facade;
use Illuminate\Support\Facades\Facade;
class DateKit extends Facade
{
protected static function getFacadeAccessor()
{
return 'datekit';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace FireflyIII\Shared\Facade;
use Illuminate\Support\Facades\Facade;
class FFForm extends Facade
{
protected static function getFacadeAccessor()
{
return 'ffform';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace FireflyIII\Shared\Facade;
use Illuminate\Support\Facades\Facade;
class Filter extends Facade
{
protected static function getFacadeAccessor()
{
return 'filter';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace FireflyIII\Shared\Facade;
use Illuminate\Support\Facades\Facade;
class Navigation extends Facade
{
protected static function getFacadeAccessor()
{
return 'navigation';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace FireflyIII\Shared\Facade;
use Illuminate\Support\Facades\Facade;
class Reminders extends Facade
{
protected static function getFacadeAccessor()
{
return 'reminders';
}
}

View File

@ -22,6 +22,7 @@ class Date
*/
public function addPeriod(Carbon $date, $repeatFreq, $skip)
{
// TODO clone the dates so referred date won't be altered.
$add = ($skip + 1);
switch ($repeatFreq) {
default:

View File

@ -23,9 +23,6 @@ class Reminders
public function amountForReminder(\Reminder $reminder)
{
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = \App::make('FireflyIII\Shared\Toolkit\Date');
switch (get_class($reminder->remindersable)) {
case 'Piggybank':
@ -34,7 +31,7 @@ class Reminders
$reminders = 0;
while ($start <= $end) {
$reminders++;
$start = $dateKit->addPeriod($start, $reminder->remindersable->reminder, $reminder->remindersable->reminder_skip);
$start = DateKit::addPeriod($start, $reminder->remindersable->reminder, $reminder->remindersable->reminder_skip);
}
/*
* Now find amount yet to save.
@ -49,9 +46,6 @@ class Reminders
throw new FireflyException('Cannot handle class ' . get_class($reminder->remindersable) . ' in amountForReminder.');
break;
}
return 50;
}
/**
@ -77,10 +71,6 @@ class Reminders
/** @var \FireflyIII\Database\Piggybank $repository */
$repository = \App::make('FireflyIII\Database\Piggybank');
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = \App::make('FireflyIII\Shared\Toolkit\Date');
/** @var Collection $piggybanks */
$piggybanks = $repository->get();
$set = $piggybanks->filter(
@ -100,12 +90,12 @@ class Reminders
*/
/** @var \PiggybankRepetition $repetition */
$repetition = $piggybank->currentRelevantRep();
$start = $dateKit->startOfPeriod(Carbon::now(), $piggybank->reminder);
$start = DateKit::startOfPeriod(Carbon::now(), $piggybank->reminder);
if ($repetition->targetdate && $repetition->targetdate <= Carbon::now()) {
// break when no longer relevant:
continue;
}
$end = $dateKit->endOfPeriod(clone $start, $piggybank->reminder);
$end = DateKit::endOfPeriod(clone $start, $piggybank->reminder);
// should have a reminder for this period:
/** @var \Collection $reminders */
$reminders = $piggybank->reminders()->dateIs($start, $end)->get();

View File

@ -83,6 +83,7 @@ class RecurringTransaction extends Ardent
return $this->hasMany('TransactionJournal');
}
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
*
@ -92,9 +93,6 @@ class RecurringTransaction extends Ardent
public function nextExpectedMatch()
{
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = App::make('FireflyIII\Shared\Toolkit\Date');
/*
* The date Firefly tries to find. If this stays null, it's "unknown".
*/
@ -104,14 +102,14 @@ class RecurringTransaction extends Ardent
* $today is the start of the next period, to make sure FF3 won't miss anything
* when the current period has a transaction journal.
*/
$today = $dateKit->addPeriod(new Carbon, $this->repeat_freq, 0);
$today = DateKit::addPeriod(new Carbon, $this->repeat_freq, 0);
/*
* FF3 loops from the $start of the recurring transaction, and to make sure
* $skip works, it adds one (for modulo).
*/
$skip = $this->skip + 1;
$start = $dateKit->startOfPeriod(new Carbon, $this->repeat_freq);
$start = DateKit::startOfPeriod(new Carbon, $this->repeat_freq);
/*
* go back exactly one month/week/etc because FF3 does not care about 'next'
* recurring transactions if they're too far into the past.
@ -124,7 +122,7 @@ class RecurringTransaction extends Ardent
while ($start <= $today) {
if (($counter % $skip) == 0) {
// do something.
$end = $dateKit->endOfPeriod(clone $start, $this->repeat_freq);
$end = DateKit::endOfPeriod(clone $start, $this->repeat_freq);
$journalCount = $this->transactionjournals()->before($end)->after($start)->count();
if ($journalCount == 0) {
$finalDate = clone $start;
@ -133,7 +131,7 @@ class RecurringTransaction extends Ardent
}
// add period for next round!
$start = $dateKit->addPeriod($start, $this->repeat_freq, 0);
$start = DateKit::addPeriod($start, $this->repeat_freq, 0);
$counter++;
}

View File

@ -15,7 +15,7 @@ use LaravelBook\Ardent\Ardent;
* @property boolean $active
* @property integer $remembersable_id
* @property string $remembersable_type
* @property-read \ $remindersable
* @property-read \Piggybank $remindersable
* @property-read \User $user
* @property mixed $data
* @method static \Illuminate\Database\Query\Builder|\Reminder whereId($value)
@ -62,6 +62,8 @@ class Reminder extends Eloquent
{
return $this->belongsTo('User');
}
public function scopeDateIs($query, Carbon $start, Carbon $end)
{
return $query->where('startdate', $start->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'));

View File

@ -15,6 +15,7 @@
Somewhere between {{$reminder->startdate->format('j F Y')}} and {{$reminder->enddate->format('j F Y')}} you
should deposit {{mf($amount)}} in piggy bank <a href="{{route('piggybanks.show',$reminder->remindersable_id)}}">{{{$reminder->remindersable->name}}}</a>
in order to make your goal of saving {{mf($reminder->remindersable->targetamount)}} on {{$reminder->remindersable->targetdate->format('j F Y')}}
@endif
</p>
<p>

View File

@ -102,13 +102,17 @@ Event::subscribe('FireflyIII\Event\TransactionJournal');
// event for when the non-repeating piggy bank is updated because the single repetition must also be changed.
// (also make piggy bank events "invalid" when they start falling outside of the date-scope of the piggy bank,
// although this not changes the amount in the piggy bank).
// TODO check if recurring transactions are being updated when journals are updated (aka no longer fitting, thus removed).
// TODO think about reminders.
// TODO an event that triggers and creates a limit + limit repetition when a budget is created, or something?
// TODO has many through needs to be added wherever relevant. Account > journals, etc.
// TODO check all models for "external" methods once more.
// TODO Auth::user() should be used very sparsely.
// TODO direct calls to models are BAD
// TODO cleanup everything related to reminders because it still feels a bit sloppy.
// TODO use a Database\Reminder thing instead of self-made ORM.
// check if recurring transactions are being updated when journals are updated (aka no longer fitting, thus removed).
// think about reminders.
// an event that triggers and creates a limit + limit repetition when a budget is created, or something?
// has many through needs to be added wherever relevant. Account > journals, etc.
// check all models for "external" methods once more.
// Auth::user() should be used very sparsely.
// direct calls to models are BAD
// cleanup everything related to reminders because it still feels a bit sloppy.
// use a Database\Reminder thing instead of self-made ORM.
// TODO create static calls instead of all the App::make() things.
// TODO see if the various has-many-throughs actually get used.
// TODO set very tight rules on all models
// TODO create custom uniquely rules.
return $app;