summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/scheduler/src/Cron.php
blob: 639957bf7597ed5d7e15048d2e2de30b82ed17a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

namespace ipl\Scheduler;

use Cron\CronExpression;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use ipl\Scheduler\Contract\Frequency;

use function ipl\Stdlib\get_php_type;

class Cron implements Frequency
{
    public const PART_MINUTE = 0;
    public const PART_HOUR = 1;
    public const PART_DAY = 2;
    public const PART_MONTH = 3;
    public const PART_WEEKDAY = 4;

    /** @var CronExpression */
    protected $cron;

    /** @var ?DateTimeInterface Start time of this frequency */
    protected $start;

    /** @var ?DateTimeInterface End time of this frequency */
    protected $end;

    /** @var string String representation of the cron expression */
    protected $expression;

    /**
     * Create frequency from the specified cron expression
     *
     * @param string $expression
     *
     * @throws InvalidArgumentException If expression is not a valid cron expression
     */
    public function __construct(string $expression)
    {
        $this->cron = new CronExpression($expression);
        $this->expression = $expression;
    }

    public function isDue(DateTimeInterface $dateTime): bool
    {
        if ($this->isExpired($dateTime) || $dateTime < $this->start) {
            return false;
        }

        return $this->cron->isDue($dateTime);
    }

    public function getNextDue(DateTimeInterface $dateTime): DateTimeInterface
    {
        if ($this->isExpired($dateTime)) {
            return $this->end;
        }

        if ($dateTime < $this->start) {
            return $this->start;
        }

        return $this->cron->getNextRunDate($dateTime);
    }

    public function isExpired(DateTimeInterface $dateTime): bool
    {
        return $this->end !== null && $this->end < $dateTime;
    }

    public function getStart(): ?DateTimeInterface
    {
        return $this->start;
    }

    public function getEnd(): ?DateTimeInterface
    {
        return $this->end;
    }

    /**
     * Get the configured cron expression
     *
     * @return string
     */
    public function getExpression(): string
    {
        return $this->expression;
    }

    /**
     * Set the start time of this frequency
     *
     * @param DateTimeInterface $start
     *
     * @return $this
     */
    public function startAt(DateTimeInterface $start): self
    {
        $this->start = clone $start;
        $this->start->setTimezone(new DateTimeZone(date_default_timezone_get()));

        return $this;
    }

    /**
     * Set the end time of this frequency
     *
     * @param DateTimeInterface $end
     *
     * @return $this
     */
    public function endAt(DateTimeInterface $end): Frequency
    {
        $this->end = clone $end;
        $this->end->setTimezone(new DateTimeZone(date_default_timezone_get()));

        return $this;
    }

    /**
     * Get the given part of the underlying cron expression
     *
     * @param int $part One of the classes `PART_*` constants
     *
     * @return string
     *
     * @throws InvalidArgumentException If the given part is invalid
     */
    public function getPart(int $part): string
    {
        $value = $this->cron->getExpression($part);
        if ($value === null) {
            throw new InvalidArgumentException(sprintf('Invalid expression part specified: %d', $part));
        }

        return $value;
    }

    /**
     * Get the parts of the underlying cron expression as an array
     *
     * @return string[]
     */
    public function getParts(): array
    {
        return $this->cron->getParts();
    }

    /**
     * Get whether the given cron expression is valid
     *
     * @param string $expression
     *
     * @return bool
     */
    public static function isValid(string $expression): bool
    {
        return CronExpression::isValidExpression($expression);
    }

    public static function fromJson(string $json): Frequency
    {
        $data = json_decode($json, true);
        if (! is_array($data)) {
            throw new InvalidArgumentException(
                sprintf(
                    '%s expects json decoded value to be an array, got %s instead',
                    __METHOD__,
                    get_php_type($data)
                )
            );
        }

        $self = new static($data['expression']);
        if (isset($data['start'])) {
            $self->startAt(new DateTime($data['start']));
        }

        if (isset($data['end'])) {
            $self->endAt(new DateTime($data['end']));
        }

        return $self;
    }

    public function jsonSerialize(): array
    {
        $data = ['expression' => $this->getExpression()];
        if ($this->start) {
            $data['start'] = $this->start->format(static::SERIALIZED_DATETIME_FORMAT);
        }

        if ($this->end) {
            $data['end'] = $this->end->format(static::SERIALIZED_DATETIME_FORMAT);
        }

        return $data;
    }
}