firefly-iii/app/Providers/EventServiceProvider.php

136 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* EventServiceProvider.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* 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\Providers;
2015-02-05 21:39:52 -06:00
use FireflyIII\Models\Account;
2015-02-25 12:32:33 -06:00
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\Transaction;
2016-06-23 05:07:31 -05:00
use FireflyIII\Models\TransactionJournal;
2016-11-25 09:55:04 -06:00
use FireflyIII\Models\TransactionJournalMeta;
2015-02-05 21:39:52 -06:00
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
2016-06-23 05:07:31 -05:00
use Log;
2015-04-07 11:26:14 -05:00
/**
* Class EventServiceProvider
*
* @package FireflyIII\Providers
*/
2015-02-11 00:35:10 -06:00
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
2015-02-11 00:35:10 -06:00
*
* @var array
*/
protected $listen
= [
2016-10-22 02:31:27 -05:00
// new event handlers:
2016-12-30 06:47:23 -06:00
'FireflyIII\Events\RegisteredUser' => // is a User related event.
2016-10-22 02:31:27 -05:00
[
'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail',
'FireflyIII\Handlers\Events\UserEventHandler@attachUserRole',
],
2016-12-30 06:47:23 -06:00
'FireflyIII\Events\RequestedNewPassword' => [ // is a User related event.
'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword',
2016-11-22 14:21:11 -06:00
],
2016-10-22 02:31:27 -05:00
'FireflyIII\Events\StoredTransactionJournal' => // is a Transaction Journal related event.
[
'FireflyIII\Handlers\Events\StoredJournalEventHandler@scanBills',
'FireflyIII\Handlers\Events\StoredJournalEventHandler@connectToPiggyBank',
'FireflyIII\Handlers\Events\StoredJournalEventHandler@processRules',
],
'FireflyIII\Events\UpdatedTransactionJournal' => // is a Transaction Journal related event.
[
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@scanBills',
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@processRules',
],
2015-02-11 00:35:10 -06:00
];
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
2016-09-16 00:05:34 -05:00
* Register any events for your application.
2015-02-11 00:35:10 -06:00
*
* @return void
*/
2016-09-16 00:05:34 -05:00
public function boot()
2015-02-11 00:35:10 -06:00
{
2016-09-16 00:05:34 -05:00
parent::boot();
2015-05-17 03:30:18 -05:00
$this->registerDeleteEvents();
$this->registerCreateEvents();
}
2016-03-19 10:24:47 -05:00
/**
*
*/
protected function registerCreateEvents()
{
// move this routine to a filter
// in case of repeated piggy banks and/or other problems.
PiggyBank::created(
function (PiggyBank $piggyBank) {
$repetition = new PiggyBankRepetition;
$repetition->piggyBank()->associate($piggyBank);
$repetition->startdate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate;
$repetition->targetdate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate;
$repetition->currentamount = 0;
$repetition->save();
}
);
}
2015-05-17 03:30:18 -05:00
/**
*
*/
protected function registerDeleteEvents()
{
Account::deleted(
2015-06-06 16:09:12 -05:00
function (Account $account) {
2016-06-23 05:07:31 -05:00
Log::debug('Now trigger account delete response #' . $account->id);
/** @var Transaction $transaction */
foreach ($account->transactions()->get() as $transaction) {
2016-06-23 05:07:31 -05:00
Log::debug('Now at transaction #' . $transaction->id);
$journal = $transaction->transactionJournal()->first();
2016-06-23 05:07:31 -05:00
if (!is_null($journal)) {
Log::debug('Call for deletion of journal #' . $journal->id);
$journal->delete();
}
}
}
);
TransactionJournal::deleted(
function (TransactionJournal $journal) {
2016-11-25 09:55:04 -06:00
Log::debug(sprintf('Now triggered journal delete response #%d', $journal->id));
2016-06-23 05:07:31 -05:00
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
2016-11-25 09:55:04 -06:00
Log::debug(sprintf('Will now delete transaction #%d', $transaction->id));
2016-06-23 05:07:31 -05:00
$transaction->delete();
}
2016-11-25 09:55:04 -06:00
// also delete journal_meta entries.
/** @var TransactionJournalMeta $meta */
foreach ($journal->transactionJournalMeta()->get() as $meta) {
Log::debug(sprintf('Will now delete meta-entry #%d', $meta->id));
$meta->delete();
}
}
);
2015-05-17 03:30:18 -05:00
}
2015-02-05 21:39:52 -06:00
}