firefly-iii/app/Providers/EventServiceProvider.php

164 lines
6.0 KiB
PHP
Raw Normal View History

<?php
/**
* EventServiceProvider.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Providers;
2015-02-05 21:39:52 -06:00
2018-05-26 06:55:11 -05:00
use Exception;
2018-04-02 08:10:40 -05:00
use FireflyIII\Events\AdminRequestedTestMessage;
2017-12-05 13:50:04 -06:00
use FireflyIII\Events\RegisteredUser;
2018-04-02 08:10:40 -05:00
use FireflyIII\Events\RequestedNewPassword;
2018-06-25 09:01:45 -05:00
use FireflyIII\Events\RequestedReportOnJournals;
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus;
2018-04-02 08:10:40 -05:00
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Events\UserChangedEmail;
2018-05-26 06:55:11 -05:00
use FireflyIII\Mail\OAuthTokenCreatedMail;
2015-02-25 12:32:33 -06:00
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
2018-05-26 06:55:11 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-12-05 13:50:04 -06:00
use Illuminate\Auth\Events\Login;
2015-02-05 21:39:52 -06:00
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
2018-05-26 06:55:11 -05:00
use Laravel\Passport\Client;
use Laravel\Passport\Events\AccessTokenCreated;
use Log;
use Mail;
use Request;
use Session;
2015-04-07 11:26:14 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class EventServiceProvider.
*/
2015-02-11 00:35:10 -06:00
class EventServiceProvider extends ServiceProvider
{
/**
* @codeCoverageIgnore
* The event listener mappings for the application.
2015-02-11 00:35:10 -06:00
*
* @var array
*/
protected $listen
= [
// is a User related event.
2018-04-02 08:10:40 -05:00
RegisteredUser::class => [
2017-11-15 05:25:49 -06:00
'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail',
'FireflyIII\Handlers\Events\UserEventHandler@attachUserRole',
],
2017-12-05 13:50:04 -06:00
// is a User related event.
2018-04-02 08:10:40 -05:00
Login::class => [
2017-12-05 13:50:04 -06:00
'FireflyIII\Handlers\Events\UserEventHandler@checkSingleUserIsAdmin',
'FireflyIII\Handlers\Events\UserEventHandler@demoUserBackToEnglish',
2017-12-28 12:03:15 -06:00
],
2018-04-02 08:10:40 -05:00
RequestedVersionCheckStatus::class => [
2017-12-28 04:38:40 -06:00
'FireflyIII\Handlers\Events\VersionCheckEventHandler@checkForUpdates',
2017-12-05 13:50:04 -06:00
],
2018-06-25 09:01:45 -05:00
RequestedReportOnJournals::class => [
'FireflyIII\Handlers\Events\AutomationHandler@reportJournals',
],
2017-12-05 13:50:04 -06:00
// is a User related event.
2018-04-02 08:10:40 -05:00
RequestedNewPassword::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword',
2016-11-22 14:21:11 -06:00
],
// is a User related event.
2018-04-02 08:10:40 -05:00
UserChangedEmail::class => [
'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeConfirmMail',
'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeUndoMail',
],
2017-09-27 08:45:55 -05:00
// admin related
2018-04-02 08:10:40 -05:00
AdminRequestedTestMessage::class => [
2017-09-27 08:45:55 -05:00
'FireflyIII\Handlers\Events\AdminEventHandler@sendTestMessage',
],
// is a Transaction Journal related event.
2018-04-02 08:10:40 -05:00
StoredTransactionJournal::class => [
2017-11-15 05:25:49 -06:00
'FireflyIII\Handlers\Events\StoredJournalEventHandler@processRules',
],
// is a Transaction Journal related event.
2018-04-02 08:10:40 -05:00
UpdatedTransactionJournal::class => [
2017-11-15 05:25:49 -06:00
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@processRules',
],
2018-05-26 06:55:11 -05:00
// API related events:
AccessTokenCreated::class => [
'FireflyIII\Handlers\Events\APIEventHandler@accessTokenCreated',
],
2015-02-11 00:35:10 -06:00
];
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* @codeCoverageIgnore
2016-09-16 00:05:34 -05:00
* Register any events for your application.
2015-02-11 00:35:10 -06:00
*/
2018-07-25 23:10:17 -05:00
public function boot(): void
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->registerCreateEvents();
}
2016-03-19 10:24:47 -05:00
/**
2018-07-22 11:50:27 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2016-03-19 10:24:47 -05:00
*/
2018-05-26 06:55:11 -05:00
protected function registerCreateEvents(): void
2016-03-19 10:24:47 -05:00
{
// in case of repeated piggy banks and/or other problems.
PiggyBank::created(
function (PiggyBank $piggyBank) {
$repetition = new PiggyBankRepetition;
$repetition->piggyBank()->associate($piggyBank);
2018-03-25 06:30:55 -05:00
$repetition->startdate = $piggyBank->startdate;
$repetition->targetdate = $piggyBank->targetdate;
2016-03-19 10:24:47 -05:00
$repetition->currentamount = 0;
$repetition->save();
}
);
2018-05-26 06:55:11 -05:00
Client::created(
function (Client $oauthClient) {
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull((int)$oauthClient->user_id);
if (null === $user) {
2018-07-09 13:42:16 -05:00
Log::info('OAuth client generated but no user associated.');
2016-03-19 10:24:47 -05:00
2018-05-26 06:55:11 -05:00
return;
}
2018-02-21 11:42:15 -06:00
2018-05-26 06:55:11 -05:00
$email = $user->email;
$ipAddress = Request::ip();
Log::debug(sprintf('Now in EventServiceProvider::registerCreateEvents. Email is %s, IP is %s', $email, $ipAddress));
try {
Log::debug('Trying to send message...');
Mail::to($email)->send(new OAuthTokenCreatedMail($email, $ipAddress, $oauthClient));
// @codeCoverageIgnoreStart
} catch (Exception $e) {
Log::debug('Send message failed! :(');
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
Session::flash('error', 'Possible email error: ' . $e->getMessage());
}
Log::debug('If no error above this line, message was sent.');
}
);
2015-05-17 03:30:18 -05:00
}
2018-05-26 06:55:11 -05:00
2015-02-05 21:39:52 -06:00
}