TYPO3 CMS  TYPO3_7-6
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  **********************************/
75  public function setStart($start)
76  {
77  $this->start = $start;
78  }
79 
85  public function getStart()
86  {
87  return $this->start;
88  }
89 
96  public function setEnd($end)
97  {
98  $this->end = $end;
99  }
100 
106  public function getEnd()
107  {
108  return $this->end;
109  }
110 
117  public function setInterval($interval)
118  {
119  $this->interval = $interval;
120  }
121 
127  public function getInterval()
128  {
129  return $this->interval;
130  }
131 
138  public function setMultiple($multiple)
139  {
140  $this->multiple = $multiple;
141  }
142 
148  public function getMultiple()
149  {
150  return $this->multiple;
151  }
152 
159  public function setCronCmd($cmd)
160  {
161  $this->cronCmd = $cmd;
162  }
163 
169  public function getCronCmd()
170  {
171  return $this->cronCmd;
172  }
173 
188  {
189  $this->isNewSingleExecution = $isNewSingleExecution;
190  }
191 
197  public function getIsNewSingleExecution()
198  {
200  }
201 
202  /**********************************
203  * Execution calculations and logic
204  **********************************/
211  public function getNextExecution()
212  {
213  if ($this->getIsNewSingleExecution()) {
214  $this->setIsNewSingleExecution(false);
215  return $this->start;
216  }
217  if (!$this->isEnded()) {
218  // If the schedule has not yet run out, find out the next date
219  if (!$this->isStarted()) {
220  // If the schedule hasn't started yet, next date is start date
221  $date = $this->start;
222  } else {
223  // If the schedule has already started, calculate next date
224  if ($this->cronCmd) {
225  // If it uses cron-like syntax, calculate next date
226  $date = $this->getNextCronExecution();
227  } elseif ($this->interval == 0) {
228  // If not and there's no interval either, it's a singe execution: use start date
229  $date = $this->start;
230  } else {
231  // Otherwise calculate date based on interval
232  $now = time();
233  $date = $now + $this->interval - ($now - $this->start) % $this->interval;
234  }
235  // If date is in the future, throw an exception
236  if (!empty($this->end) && $date > $this->end) {
237  throw new \OutOfBoundsException('Next execution date is past end date.', 1250715528);
238  }
239  }
240  } else {
241  // The event has ended, throw an exception
242  throw new \OutOfBoundsException('Task is past end date.', 1250715544);
243  }
244  return $date;
245  }
246 
252  public function getNextCronExecution()
253  {
255  $cronCmd = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\CronCommand\CronCommand::class, $this->getCronCmd());
256  $cronCmd->calculateNextValue();
257  return $cronCmd->getTimestamp();
258  }
259 
265  public function isStarted()
266  {
267  return $this->start < time();
268  }
269 
275  public function isEnded()
276  {
277  if (empty($this->end)) {
278  // If no end is defined, the schedule never ends
279  $result = false;
280  } else {
281  // Otherwise check if end is in the past
282  $result = $this->end < time();
283  }
284  return $result;
285  }
286 }
setIsNewSingleExecution($isNewSingleExecution)
Definition: Execution.php:187