[CLEANUP] Code cleanup in ext:reports
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Task / SystemStatusUpdateTask.php
1 <?php
2 namespace TYPO3\CMS\Reports\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Mail\MailMessage;
18 use TYPO3\CMS\Core\Registry;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MailUtility;
21 use TYPO3\CMS\Lang\LanguageService;
22 use TYPO3\CMS\Reports\Status;
23 use TYPO3\CMS\Scheduler\Task\AbstractTask;
24
25 /**
26 * A task that should be run regularly to determine the system's status.
27 */
28 class SystemStatusUpdateTask extends AbstractTask
29 {
30 /**
31 * Email addresses to send email notification to in case we find problems with
32 * the system.
33 *
34 * @var string
35 */
36 protected $notificationEmail = null;
37
38 /**
39 * Executes the System Status Update task, determining the highest severity of
40 * status reports and saving that to the registry to be displayed at login
41 * if necessary.
42 *
43 * @see \TYPO3\CMS\Scheduler\Task\AbstractTask::execute()
44 */
45 public function execute()
46 {
47 /** @var Registry $registry */
48 $registry = GeneralUtility::makeInstance(Registry::class);
49 /** @var \TYPO3\CMS\Reports\Report\Status\Status $statusReport */
50 $statusReport = GeneralUtility::makeInstance(\TYPO3\CMS\Reports\Report\Status\Status::class);
51 $systemStatus = $statusReport->getDetailedSystemStatus();
52 $highestSeverity = $statusReport->getHighestSeverity($systemStatus);
53 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
54 if ($highestSeverity > Status::OK) {
55 $this->sendNotificationEmail($systemStatus);
56 }
57 return true;
58 }
59
60 /**
61 * Gets the notification email addresses.
62 *
63 * @return string Notification email addresses.
64 */
65 public function getNotificationEmail()
66 {
67 return $this->notificationEmail;
68 }
69
70 /**
71 * Sets the notification email address.
72 *
73 * @param string $notificationEmail Notification email address.
74 * @return void
75 */
76 public function setNotificationEmail($notificationEmail)
77 {
78 $this->notificationEmail = $notificationEmail;
79 }
80
81 /**
82 * Sends a notification email, reporting system issues.
83 *
84 * @param Status[] $systemStatus Array of statuses
85 * @return void
86 */
87 protected function sendNotificationEmail(array $systemStatus)
88 {
89 $systemIssues = array();
90 foreach ($systemStatus as $statusProvider) {
91 /** @var Status $status */
92 foreach ($statusProvider as $status) {
93 if ($status->getSeverity() > Status::OK) {
94 $systemIssues[] = (string)$status . CRLF . $status->getMessage() . CRLF . CRLF;
95 }
96 }
97 }
98 $notificationEmails = GeneralUtility::trimExplode(LF, $this->notificationEmail, true);
99 $sendEmailsTo = array();
100 foreach ($notificationEmails as $notificationEmail) {
101 $sendEmailsTo[] = $notificationEmail;
102 }
103 $subject = sprintf($this->getLanguageService()->getLL('status_updateTask_email_subject'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
104 $message = sprintf($this->getLanguageService()->getLL('status_problemNotification'), '', '');
105 $message .= CRLF . CRLF;
106 $message .= $this->getLanguageService()->getLL('status_updateTask_email_site') . ': ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
107 $message .= CRLF . CRLF;
108 $message .= $this->getLanguageService()->getLL('status_updateTask_email_issues') . ': ' . CRLF;
109 $message .= implode(CRLF, $systemIssues);
110 $message .= CRLF . CRLF;
111 $from = MailUtility::getSystemFrom();
112 /** @var MailMessage $mail */
113 $mail = GeneralUtility::makeInstance(MailMessage::class);
114 $mail->setFrom($from);
115 $mail->setTo($sendEmailsTo);
116 $mail->setSubject($subject);
117 $mail->setBody($message);
118 $mail->send();
119 }
120
121 /**
122 * @return LanguageService
123 */
124 protected function getLanguageService()
125 {
126 return $GLOBALS['LANG'];
127 }
128 }