| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Scheduler; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeInterface; |
| 7 |
use DateTimeZone; |
| 8 |
use InvalidArgumentException; |
| 9 |
use ipl\Scheduler\Contract\Frequency; |
| 10 |
|
| 11 |
use function ipl\Stdlib\get_php_type; |
| 12 |
|
| 13 |
/** |
| 14 |
* Single-run scheduling frequency |
| 15 |
* |
| 16 |
* Schedules a task to run exactly once at the given point in time. |
| 17 |
* The frequency is considered expired as soon as that time has passed. |
| 18 |
*/ |
| 19 |
class OneOff implements Frequency |
| 20 |
{ |
| 21 |
/** @var DateTime Start time of this frequency */ |
| 22 |
protected DateTime $dateTime; |
| 23 |
|
| 24 |
/** |
| 25 |
* Create a one-off frequency for the given point in time |
| 26 |
* |
| 27 |
* The datetime is cloned and normalized to the default system timezone. |
| 28 |
* |
| 29 |
* @param DateTimeInterface $dateTime The exact time at which the task should run |
| 30 |
*/ |
| 31 |
public function __construct(DateTimeInterface $dateTime) |
| 32 |
{ |
| 33 |
$this->dateTime = DateTime::createFromInterface($dateTime); |
| 34 |
$this->dateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Create a {@see OneOff} instance from its stored JSON representation |
| 39 |
* |
| 40 |
* The JSON must decode to a datetime string formatted according to {@see Frequency::SERIALIZED_DATETIME_FORMAT}. |
| 41 |
* |
| 42 |
* @param string $json |
| 43 |
* |
| 44 |
* @return static |
| 45 |
* |
| 46 |
* @throws InvalidArgumentException If the JSON does not decode to a string |
| 47 |
*/ |
| 48 |
public static function fromJson(string $json): static |
| 49 |
{ |
| 50 |
$data = json_decode($json, true); |
| 51 |
if (! is_string($data)) { |
| 52 |
throw new InvalidArgumentException( |
| 53 |
sprintf( |
| 54 |
'%s expects json decoded value to be string, got %s instead', |
| 55 |
__METHOD__, |
| 56 |
get_php_type($data) |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
return new static(new DateTime($data)); |
| 62 |
} |
| 63 |
|
| 64 |
public function isDue(DateTimeInterface $dateTime): bool |
| 65 |
{ |
| 66 |
return ! $this->isExpired($dateTime) && $this->dateTime == $dateTime; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the next due date relative to the given time |
| 71 |
* |
| 72 |
* Always returns the configured run time, regardless of the given time. |
| 73 |
* |
| 74 |
* @param DateTimeInterface $dateTime |
| 75 |
* |
| 76 |
* @return DateTimeInterface |
| 77 |
*/ |
| 78 |
public function getNextDue(DateTimeInterface $dateTime): DateTimeInterface |
| 79 |
{ |
| 80 |
return $this->dateTime; |
| 81 |
} |
| 82 |
|
| 83 |
public function isExpired(DateTimeInterface $dateTime): bool |
| 84 |
{ |
| 85 |
return $this->dateTime < $dateTime; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the start time of this frequency, which equals the configured run time |
| 90 |
* |
| 91 |
* @return ?DateTimeInterface |
| 92 |
*/ |
| 93 |
public function getStart(): ?DateTimeInterface |
| 94 |
{ |
| 95 |
return $this->dateTime; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the end time of this frequency, which equals the configured run time |
| 100 |
* |
| 101 |
* @return ?DateTimeInterface |
| 102 |
*/ |
| 103 |
public function getEnd(): ?DateTimeInterface |
| 104 |
{ |
| 105 |
return $this->getStart(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Serialize this frequency to a datetime string |
| 110 |
* |
| 111 |
* Returns the configured run time formatted according to {@see Frequency::SERIALIZED_DATETIME_FORMAT}. |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public function jsonSerialize(): string |
| 116 |
{ |
| 117 |
return $this->dateTime->format(static::SERIALIZED_DATETIME_FORMAT); |
| 118 |
} |
| 119 |
} |