‪TYPO3CMS  ‪main
Execution.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Scheduler;
17 
20 
26 {
32  protected ‪$start;
33 
39  protected ‪$end;
40 
46  protected ‪$interval;
47 
53  protected ‪$multiple = false;
54 
60  protected ‪$cronCmd;
61 
69  protected ‪$isNewSingleExecution = false;
70 
71  /**********************************
72  * Setters and getters
73  **********************************/
79  public function ‪setStart(‪$start)
80  {
81  $this->start = ‪$start;
82  }
83 
89  public function ‪getStart()
90  {
91  return ‪$this->start;
92  }
93 
99  public function ‪setEnd(‪$end)
100  {
101  $this->end = ‪$end;
102  }
103 
109  public function ‪getEnd()
110  {
111  return ‪$this->end;
112  }
113 
119  public function ‪setInterval(‪$interval)
120  {
121  $this->interval = ‪$interval;
122  }
123 
129  public function ‪getInterval()
130  {
131  return ‪$this->interval;
132  }
133 
139  public function ‪setMultiple(‪$multiple)
140  {
141  $this->multiple = ‪$multiple;
142  }
143 
149  public function ‪getMultiple()
150  {
151  return ‪$this->multiple;
152  }
153 
159  public function ‪setCronCmd($cmd)
160  {
161  $this->cronCmd = $cmd;
162  }
163 
169  public function ‪getCronCmd()
170  {
171  return ‪$this->cronCmd;
172  }
173 
187  {
188  $this->isNewSingleExecution = ‪$isNewSingleExecution;
189  }
190 
196  public function ‪getIsNewSingleExecution()
197  {
199  }
200 
201  /**********************************
202  * Execution calculations and logic
203  **********************************/
210  public function ‪getNextExecution()
211  {
212  if ($this->‪getIsNewSingleExecution()) {
213  $this->‪setIsNewSingleExecution(false);
214  return ‪$this->start;
215  }
216  if (!$this->‪isEnded()) {
217  // If the schedule has not yet run out, find out the next date
218  if (!$this->‪isStarted()) {
219  // If the schedule hasn't started yet, next date is start date
220  $date = ‪$this->start;
221  } else {
222  // If the schedule has already started, calculate next date
223  if ($this->cronCmd) {
224  // If it uses cron-like syntax, calculate next date
225  $date = $this->‪getNextCronExecution();
226  } elseif ($this->interval == 0) {
227  // If not and there's no interval either, it's a singe execution: use start date
228  $date = ‪$this->start;
229  } else {
230  // Otherwise calculate date based on interval
231  $now = time();
232  $date = $now + $this->interval - ($now - ‪$this->start) % $this->interval;
233  }
234  // If date is in the future, throw an exception
235  if (!empty($this->end) && $date > ‪$this->end) {
236  throw new \OutOfBoundsException('Next execution date is past end date.', 1250715528);
237  }
238  }
239  } else {
240  // The event has ended, throw an exception
241  throw new \OutOfBoundsException('Task is past end date.', 1250715544);
242  }
243  return $date;
244  }
245 
251  public function ‪getNextCronExecution()
252  {
253  ‪$cronCmd = GeneralUtility::makeInstance(CronCommand::class, $this->‪getCronCmd());
254  ‪$cronCmd->calculateNextValue();
255  return ‪$cronCmd->getTimestamp();
256  }
257 
263  public function ‪isStarted()
264  {
265  return $this->start < time();
266  }
267 
273  public function ‪isEnded()
274  {
275  if (empty($this->end)) {
276  // If no end is defined, the schedule never ends
277  $result = false;
278  } else {
279  // Otherwise check if end is in the past
280  $result = $this->end < time();
281  }
282  return $result;
283  }
284 }
‪TYPO3\CMS\Scheduler
Definition: AbstractAdditionalFieldProvider.php:18
‪TYPO3\CMS\Scheduler\Execution\$multiple
‪bool $multiple
Definition: Execution.php:49
‪TYPO3\CMS\Scheduler\Execution\setStart
‪setStart($start)
Definition: Execution.php:73
‪TYPO3\CMS\Scheduler\Execution\isEnded
‪bool isEnded()
Definition: Execution.php:267
‪TYPO3\CMS\Scheduler\Execution\setCronCmd
‪setCronCmd($cmd)
Definition: Execution.php:153
‪TYPO3\CMS\Scheduler\Execution\setIsNewSingleExecution
‪setIsNewSingleExecution($isNewSingleExecution)
Definition: Execution.php:180
‪TYPO3\CMS\Scheduler\Execution\getIsNewSingleExecution
‪bool getIsNewSingleExecution()
Definition: Execution.php:190
‪TYPO3\CMS\Scheduler\Execution\getInterval
‪int getInterval()
Definition: Execution.php:123
‪TYPO3\CMS\Scheduler\Execution\$cronCmd
‪string $cronCmd
Definition: Execution.php:55
‪TYPO3\CMS\Scheduler\Execution\$start
‪int $start
Definition: Execution.php:31
‪TYPO3\CMS\Scheduler\Execution\$interval
‪int $interval
Definition: Execution.php:43
‪TYPO3\CMS\Scheduler\Execution\getNextCronExecution
‪int getNextCronExecution()
Definition: Execution.php:245
‪TYPO3\CMS\Scheduler\Execution
Definition: Execution.php:26
‪TYPO3\CMS\Scheduler\Execution\getCronCmd
‪string getCronCmd()
Definition: Execution.php:163
‪TYPO3\CMS\Scheduler\Execution\getEnd
‪int getEnd()
Definition: Execution.php:103
‪TYPO3\CMS\Scheduler\Execution\setInterval
‪setInterval($interval)
Definition: Execution.php:113
‪TYPO3\CMS\Scheduler\Execution\getNextExecution
‪int getNextExecution()
Definition: Execution.php:204
‪TYPO3\CMS\Scheduler\Execution\getStart
‪int getStart()
Definition: Execution.php:83
‪TYPO3\CMS\Scheduler\Execution\setEnd
‪setEnd($end)
Definition: Execution.php:93
‪TYPO3\CMS\Scheduler\CronCommand\CronCommand
Definition: CronCommand.php:24
‪TYPO3\CMS\Scheduler\Execution\$isNewSingleExecution
‪bool $isNewSingleExecution
Definition: Execution.php:63
‪TYPO3\CMS\Scheduler\Execution\$end
‪int $end
Definition: Execution.php:37
‪TYPO3\CMS\Scheduler\Execution\getMultiple
‪bool getMultiple()
Definition: Execution.php:143
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Scheduler\Execution\isStarted
‪bool isStarted()
Definition: Execution.php:257
‪TYPO3\CMS\Scheduler\Execution\setMultiple
‪setMultiple($multiple)
Definition: Execution.php:133