[TASK] Use Environment API in scheduler
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / TestTask.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Example;
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\Core\Environment;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Provides testing procedures
22 */
23 class TestTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask
24 {
25 /**
26 * An email address to be used during the process
27 *
28 * @var string $email
29 */
30 public $email;
31
32 /**
33 * Function executed from the Scheduler.
34 * Sends an email
35 *
36 * @return bool
37 */
38 public function execute()
39 {
40 $success = false;
41 if (!empty($this->email)) {
42 // If an email address is defined, send a message to it
43 $this->logger->info('[TYPO3\\CMS\\Scheduler\\Example\\TestTask]: Test email sent to "' . $this->email . '"');
44 // Get execution information
45 $exec = $this->getExecution();
46 // Get call method
47 if (Environment::isCli()) {
48 $calledBy = 'CLI module dispatcher';
49 $site = '-';
50 } else {
51 $calledBy = 'TYPO3 backend';
52 $site = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
53 }
54 $start = $exec->getStart();
55 $end = $exec->getEnd();
56 $interval = $exec->getInterval();
57 $multiple = $exec->getMultiple();
58 $cronCmd = $exec->getCronCmd();
59 $mailBody = 'SCHEDULER TEST-TASK' . LF . '- - - - - - - - - - - - - - - -' . LF . 'UID: ' . $this->taskUid . LF . 'Sitename: ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . LF . 'Site: ' . $site . LF . 'Called by: ' . $calledBy . LF . 'tstamp: ' . date('Y-m-d H:i:s') . ' [' . time() . ']' . LF . 'maxLifetime: ' . $this->scheduler->extConf['maxLifetime'] . LF . 'start: ' . date('Y-m-d H:i:s', $start) . ' [' . $start . ']' . LF . 'end: ' . (empty($end) ? '-' : date('Y-m-d H:i:s', $end) . ' [' . $end . ']') . LF . 'interval: ' . $interval . LF . 'multiple: ' . ($multiple ? 'yes' : 'no') . LF . 'cronCmd: ' . ($cronCmd ? $cronCmd : 'not used');
60 // Prepare mailer and send the mail
61 try {
62 /** @var $mailer \TYPO3\CMS\Core\Mail\MailMessage */
63 $mailer = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
64 $mailer->setFrom([$this->email => 'SCHEDULER TEST-TASK']);
65 $mailer->setReplyTo([$this->email => 'SCHEDULER TEST-TASK']);
66 $mailer->setSubject('SCHEDULER TEST-TASK');
67 $mailer->setBody($mailBody);
68 $mailer->setTo($this->email);
69 $mailsSend = $mailer->send();
70 $success = $mailsSend > 0;
71 } catch (\Exception $e) {
72 throw new \TYPO3\CMS\Core\Exception($e->getMessage(), 1476048416);
73 }
74 } else {
75 // No email defined, just log the task
76 $this->logger->warning('[TYPO3\\CMS\\Scheduler\\Example\\TestTask]: No email address given');
77 }
78 return $success;
79 }
80
81 /**
82 * This method returns the destination mail address as additional information
83 *
84 * @return string Information to display
85 */
86 public function getAdditionalInformation()
87 {
88 return $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.email') . ': ' . $this->email;
89 }
90 }