TYPO3 CMS  TYPO3_8-7
Execution.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Scheduler;
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 
20 class Execution
21 {
27  protected $start;
28 
34  protected $end;
35 
41  protected $interval;
42 
48  protected $multiple = false;
49 
55  protected $cronCmd;
56 
64  protected $isNewSingleExecution = false;
65 
66  /**********************************
67  * Setters and getters
68  **********************************/
74  public function setStart($start)
75  {
76  $this->start = $start;
77  }
78 
84  public function getStart()
85  {
86  return $this->start;
87  }
88 
94  public function setEnd($end)
95  {
96  $this->end = $end;
97  }
98 
104  public function getEnd()
105  {
106  return $this->end;
107  }
108 
114  public function setInterval($interval)
115  {
116  $this->interval = $interval;
117  }
118 
124  public function getInterval()
125  {
126  return $this->interval;
127  }
128 
134  public function setMultiple($multiple)
135  {
136  $this->multiple = $multiple;
137  }
138 
144  public function getMultiple()
145  {
146  return $this->multiple;
147  }
148 
154  public function setCronCmd($cmd)
155  {
156  $this->cronCmd = $cmd;
157  }
158 
164  public function getCronCmd()
165  {
166  return $this->cronCmd;
167  }
168 
182  {
183  $this->isNewSingleExecution = $isNewSingleExecution;
184  }
185 
191  public function getIsNewSingleExecution()
192  {
194  }
195 
196  /**********************************
197  * Execution calculations and logic
198  **********************************/
205  public function getNextExecution()
206  {
207  if ($this->getIsNewSingleExecution()) {
208  $this->setIsNewSingleExecution(false);
209  return $this->start;
210  }
211  if (!$this->isEnded()) {
212  // If the schedule has not yet run out, find out the next date
213  if (!$this->isStarted()) {
214  // If the schedule hasn't started yet, next date is start date
215  $date = $this->start;
216  } else {
217  // If the schedule has already started, calculate next date
218  if ($this->cronCmd) {
219  // If it uses cron-like syntax, calculate next date
220  $date = $this->getNextCronExecution();
221  } elseif ($this->interval == 0) {
222  // If not and there's no interval either, it's a singe execution: use start date
223  $date = $this->start;
224  } else {
225  // Otherwise calculate date based on interval
226  $now = time();
227  $date = $now + $this->interval - ($now - $this->start) % $this->interval;
228  }
229  // If date is in the future, throw an exception
230  if (!empty($this->end) && $date > $this->end) {
231  throw new \OutOfBoundsException('Next execution date is past end date.', 1250715528);
232  }
233  }
234  } else {
235  // The event has ended, throw an exception
236  throw new \OutOfBoundsException('Task is past end date.', 1250715544);
237  }
238  return $date;
239  }
240 
246  public function getNextCronExecution()
247  {
249  $cronCmd = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\CronCommand\CronCommand::class, $this->getCronCmd());
250  $cronCmd->calculateNextValue();
251  return $cronCmd->getTimestamp();
252  }
253 
259  public function isStarted()
260  {
261  return $this->start < time();
262  }
263 
269  public function isEnded()
270  {
271  if (empty($this->end)) {
272  // If no end is defined, the schedule never ends
273  $result = false;
274  } else {
275  // Otherwise check if end is in the past
276  $result = $this->end < time();
277  }
278  return $result;
279  }
280 }
setIsNewSingleExecution($isNewSingleExecution)
Definition: Execution.php:181
static makeInstance($className,... $constructorArguments)