Files
firefly-iii/app/Handlers/Events/AutomationHandler.php

89 lines
2.7 KiB
PHP
Raw Normal View History

2018-06-25 16:01:45 +02:00
<?php
/**
* AutomationHandler.php
2020-01-28 08:45:38 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-25 16:01:45 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-25 16:01:45 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-06-25 16:01:45 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-25 16:01:45 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-06-25 16:01:45 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-25 16:01:45 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
use Exception;
use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Mail\ReportNewJournalsMail;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Log;
use Mail;
/**
* Class AutomationHandler
*/
class AutomationHandler
{
/**
2018-07-07 07:48:10 +02:00
* Respond to the creation of X journals.
*
2018-06-25 16:01:45 +02:00
* @param RequestedReportOnJournals $event
*
* @return bool
*/
public function reportJournals(RequestedReportOnJournals $event): bool
{
$sendReport = config('firefly.send_report_journals');
2018-10-13 13:19:41 +02:00
if (false === $sendReport) {
2018-12-12 20:30:25 +01:00
return true; // @codeCoverageIgnore
2018-10-13 13:19:41 +02:00
}
2018-06-25 16:01:45 +02:00
Log::debug('In reportJournals.');
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull($event->userId);
2019-08-18 11:16:33 +02:00
if (null !== $user && 0 !== $event->groups->count()) {
$email = $user->email;
// see if user has alternative email address:
$pref = app('preferences')->getForUser($user, 'remote_guard_alt_email', null);
if (null !== $pref) {
$email = $pref->data;
}
// if user is demo user, send to owner:
if($user->hasRole('demo')) {
$email = config('firefly.site_owner');
}
2018-07-07 07:48:10 +02:00
try {
Log::debug('Trying to mail...');
Mail::to($user->email)->send(new ReportNewJournalsMail($email, '127.0.0.1', $event->groups));
2018-07-07 07:48:10 +02:00
// @codeCoverageIgnoreStart
} catch (Exception $e) {
Log::debug('Send message failed! :(');
2018-07-07 07:48:10 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2018-07-07 07:48:10 +02:00
}
// @codeCoverageIgnoreEnd
2018-07-07 07:48:10 +02:00
Log::debug('Done!');
2018-06-25 16:01:45 +02:00
}
return true;
}
}