| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |         $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()) { | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 Log::debug('Trying to mail...'); | 
					
						
							| 
									
										
										
										
											2019-08-18 11:16:33 +02:00
										 |  |  |                 Mail::to($user->email)->send(new ReportNewJournalsMail($user->email, '127.0.0.1', $event->groups)); | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |                 // @codeCoverageIgnoreStart
 | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2018-08-24 16:07:33 +02:00
										 |  |  |                 Log::debug('Send message failed! :('); | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |                 Log::error($e->getMessage()); | 
					
						
							| 
									
										
										
										
											2018-08-24 16:07:33 +02:00
										 |  |  |                 Log::error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-24 16:07:33 +02:00
										 |  |  |             // @codeCoverageIgnoreEnd
 | 
					
						
							| 
									
										
										
										
											2018-07-07 07:48:10 +02:00
										 |  |  |             Log::debug('Done!'); | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |