Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.47% |
85 / 95 |
|
92.59% |
25 / 27 |
CRAP | |
0.00% |
0 / 1 |
| Interval | |
89.47% |
85 / 95 |
|
92.59% |
25 / 27 |
32.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getStart | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setStart | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEnd | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setEnd | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMaxDuration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMaxDuration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMinute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMinute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| addMinute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getHour | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setHour | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| addHour | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getDayOfMonth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDayOfMonth | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| addDayOfMonth | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getDayOfWeek | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDayOfWeek | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| addDayOfWeek | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getMonth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMonth | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| addMonth | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getYear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setYear | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| addYear | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| serialize | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| unserialize | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\TaskSchedule |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\TaskSchedule; |
| 16 | |
| 17 | use phpOMS\Contract\SerializableInterface; |
| 18 | |
| 19 | /** |
| 20 | * Interval class for tasks. |
| 21 | * |
| 22 | * @package phpOMS\Utils\TaskSchedule |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class Interval implements SerializableInterface |
| 28 | { |
| 29 | /** |
| 30 | * Start of the task. |
| 31 | * |
| 32 | * @var \DateTime |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | private \DateTime $start; |
| 36 | |
| 37 | /** |
| 38 | * End of the task. |
| 39 | * |
| 40 | * @var null|\DateTime |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | private ?\DateTime $end = null; |
| 44 | |
| 45 | /** |
| 46 | * Max runtime duration |
| 47 | * |
| 48 | * After this duration in seconds the task/job is stopped. |
| 49 | * |
| 50 | * 0 = infinite |
| 51 | * |
| 52 | * @var int |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | private int $maxDuration = 0; |
| 56 | |
| 57 | /** |
| 58 | * Minute. |
| 59 | * |
| 60 | * @var array |
| 61 | * @since 1.0.0 |
| 62 | */ |
| 63 | private $minute = []; |
| 64 | |
| 65 | /** |
| 66 | * Hour. |
| 67 | * |
| 68 | * @var array |
| 69 | * @since 1.0.0 |
| 70 | */ |
| 71 | private $hour = []; |
| 72 | |
| 73 | /** |
| 74 | * Day of month. |
| 75 | * |
| 76 | * @var array |
| 77 | * @since 1.0.0 |
| 78 | */ |
| 79 | private $dayOfMonth = []; |
| 80 | |
| 81 | /** |
| 82 | * Month. |
| 83 | * |
| 84 | * @var array |
| 85 | * @since 1.0.0 |
| 86 | */ |
| 87 | private $month = []; |
| 88 | |
| 89 | /** |
| 90 | * Day of week. |
| 91 | * |
| 92 | * @var array |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | private $dayOfWeek = []; |
| 96 | |
| 97 | /** |
| 98 | * Year. |
| 99 | * |
| 100 | * @var array |
| 101 | * @since 1.0.0 |
| 102 | */ |
| 103 | private $year = []; |
| 104 | |
| 105 | /** |
| 106 | * Constructor. |
| 107 | * |
| 108 | * @param null|\DateTime $start Start of the job/task |
| 109 | * @param null|string $interval Interval to unserialize (internal serialization not a cronjob string etc.) |
| 110 | * |
| 111 | * @since 1.0.0 |
| 112 | */ |
| 113 | public function __construct(\DateTime $start = null, string $interval = null) |
| 114 | { |
| 115 | $this->start = $start ?? new \DateTime('now'); |
| 116 | |
| 117 | if ($interval !== null) { |
| 118 | $this->unserialize($interval); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get start. |
| 124 | * |
| 125 | * @return \DateTime |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | public function getStart() : \DateTime |
| 130 | { |
| 131 | return $this->start; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Set start. |
| 136 | * |
| 137 | * @param \DateTime $start Start date |
| 138 | * |
| 139 | * @return void |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | */ |
| 143 | public function setStart(\DateTime $start) : void |
| 144 | { |
| 145 | $this->start = $start; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get end. |
| 150 | * |
| 151 | * @return \DateTime |
| 152 | * |
| 153 | * @since 1.0.0 |
| 154 | */ |
| 155 | public function getEnd() : ?\DateTime |
| 156 | { |
| 157 | return $this->end; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set end. |
| 162 | * |
| 163 | * @param \DateTime $end End date |
| 164 | * |
| 165 | * @return void |
| 166 | * |
| 167 | * @since 1.0.0 |
| 168 | */ |
| 169 | public function setEnd(\DateTime $end) : void |
| 170 | { |
| 171 | $this->end = $end; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get max runtime duration |
| 176 | * |
| 177 | * After this duration a task/job is cancelled |
| 178 | * |
| 179 | * @return int |
| 180 | * |
| 181 | * @since 1.0.0 |
| 182 | */ |
| 183 | public function getMaxDuration() : int |
| 184 | { |
| 185 | return $this->maxDuration; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Set max runtime duration |
| 190 | * |
| 191 | * After this duration a task/job is cancelled |
| 192 | * |
| 193 | * @param int $duration Max duration in seconds |
| 194 | * |
| 195 | * @return void |
| 196 | * |
| 197 | * @since 1.0.0 |
| 198 | */ |
| 199 | public function setMaxDuration(int $duration) : void |
| 200 | { |
| 201 | $this->maxDuration = $duration; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get minute. |
| 206 | * |
| 207 | * @return array |
| 208 | * |
| 209 | * @since 1.0.0 |
| 210 | */ |
| 211 | public function getMinute() : array |
| 212 | { |
| 213 | return $this->minute; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Set minute. |
| 218 | * |
| 219 | * @param int $index Index of the value to change |
| 220 | * @param int $start Start |
| 221 | * @param int $end End |
| 222 | * @param int $step Step |
| 223 | * |
| 224 | * @return void |
| 225 | * |
| 226 | * @since 1.0.0 |
| 227 | */ |
| 228 | public function setMinute(int $index, int $start = null, int $end = null, int $step = null) : void |
| 229 | { |
| 230 | $this->minute[$index] = [ |
| 231 | 'start' => $start, |
| 232 | 'end' => $end, |
| 233 | 'step' => $step, |
| 234 | ]; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Add minute. |
| 239 | * |
| 240 | * @param int $start Start |
| 241 | * @param int $end End |
| 242 | * @param int $step Step |
| 243 | * |
| 244 | * @return void |
| 245 | * |
| 246 | * @since 1.0.0 |
| 247 | */ |
| 248 | public function addMinute(int $start = null, int $end = null, int $step = null) : void |
| 249 | { |
| 250 | $this->minute[] = [ |
| 251 | 'start' => $start, |
| 252 | 'end' => $end, |
| 253 | 'step' => $step, |
| 254 | ]; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get hour. |
| 259 | * |
| 260 | * @return array |
| 261 | * |
| 262 | * @since 1.0.0 |
| 263 | */ |
| 264 | public function getHour() : array |
| 265 | { |
| 266 | return $this->hour; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Set hour. |
| 271 | * |
| 272 | * @param int $index Index of the value to change |
| 273 | * @param int $start Start |
| 274 | * @param int $end End |
| 275 | * @param int $step Step |
| 276 | * |
| 277 | * @return void |
| 278 | * |
| 279 | * @since 1.0.0 |
| 280 | */ |
| 281 | public function setHour(int $index, int $start = null, int $end = null, int $step = null) : void |
| 282 | { |
| 283 | $this->hour[$index] = [ |
| 284 | 'start' => $start, |
| 285 | 'end' => $end, |
| 286 | 'step' => $step, |
| 287 | ]; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Add hour. |
| 292 | * |
| 293 | * @param int $start Start |
| 294 | * @param int $end End |
| 295 | * @param int $step Step |
| 296 | * |
| 297 | * @return void |
| 298 | * |
| 299 | * @since 1.0.0 |
| 300 | */ |
| 301 | public function addHour(int $start = null, int $end = null, int $step = null) : void |
| 302 | { |
| 303 | $this->hour[] = [ |
| 304 | 'start' => $start, |
| 305 | 'end' => $end, |
| 306 | 'step' => $step, |
| 307 | ]; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get day of month. |
| 312 | * |
| 313 | * @return array |
| 314 | * |
| 315 | * @since 1.0.0 |
| 316 | */ |
| 317 | public function getDayOfMonth() : array |
| 318 | { |
| 319 | return $this->dayOfMonth; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Set day of the month. |
| 324 | * |
| 325 | * @param int $index Index of the value to change |
| 326 | * @param int $start Start |
| 327 | * @param int $end End |
| 328 | * @param int $step Step |
| 329 | * |
| 330 | * @return void |
| 331 | * |
| 332 | * @since 1.0.0 |
| 333 | */ |
| 334 | public function setDayOfMonth(int $index, int $start = null, int $end = null, int $step = null) : void |
| 335 | { |
| 336 | $this->dayOfMonth[$index] = [ |
| 337 | 'start' => $start, |
| 338 | 'end' => $end, |
| 339 | 'step' => $step, |
| 340 | ]; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Add day of the month. |
| 345 | * |
| 346 | * @param int $start Start |
| 347 | * @param int $end End |
| 348 | * @param int $step Step |
| 349 | * |
| 350 | * @return void |
| 351 | * |
| 352 | * @since 1.0.0 |
| 353 | */ |
| 354 | public function addDayOfMonth(int $start = null, int $end = null, int $step = null) : void |
| 355 | { |
| 356 | $this->dayOfMonth[] = [ |
| 357 | 'start' => $start, |
| 358 | 'end' => $end, |
| 359 | 'step' => $step, |
| 360 | ]; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get day of week. |
| 365 | * |
| 366 | * @return array |
| 367 | * |
| 368 | * @since 1.0.0 |
| 369 | */ |
| 370 | public function getDayOfWeek() : array |
| 371 | { |
| 372 | return $this->dayOfWeek; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Set day of the week. |
| 377 | * |
| 378 | * @param int $index Index of the value to change |
| 379 | * @param int $start Start |
| 380 | * @param int $end End |
| 381 | * @param int $step Step |
| 382 | * |
| 383 | * @return void |
| 384 | * |
| 385 | * @since 1.0.0 |
| 386 | */ |
| 387 | public function setDayOfWeek(int $index, int $start = null, int $end = null, int $step = null) : void |
| 388 | { |
| 389 | $this->dayOfWeek[$index] = [ |
| 390 | 'start' => $start, |
| 391 | 'end' => $end, |
| 392 | 'step' => $step, |
| 393 | ]; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Add day of the week. |
| 398 | * |
| 399 | * @param int $start Start |
| 400 | * @param int $end End |
| 401 | * @param int $step Step |
| 402 | * |
| 403 | * @return void |
| 404 | * |
| 405 | * @since 1.0.0 |
| 406 | */ |
| 407 | public function addDayOfWeek(int $start = null, int $end = null, int $step = null) : void |
| 408 | { |
| 409 | $this->dayOfWeek[] = [ |
| 410 | 'start' => $start, |
| 411 | 'end' => $end, |
| 412 | 'step' => $step, |
| 413 | ]; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Get month. |
| 418 | * |
| 419 | * @return array |
| 420 | * |
| 421 | * @since 1.0.0 |
| 422 | */ |
| 423 | public function getMonth() : array |
| 424 | { |
| 425 | return $this->month; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Set month. |
| 430 | * |
| 431 | * @param int $index Index of the value to change |
| 432 | * @param int $start Start |
| 433 | * @param int $end End |
| 434 | * @param int $step Step |
| 435 | * |
| 436 | * @return void |
| 437 | * |
| 438 | * @since 1.0.0 |
| 439 | */ |
| 440 | public function setMonth(int $index, int $start = null, int $end = null, int $step = null) : void |
| 441 | { |
| 442 | $this->month[$index] = [ |
| 443 | 'start' => $start, |
| 444 | 'end' => $end, |
| 445 | 'step' => $step, |
| 446 | ]; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Add month. |
| 451 | * |
| 452 | * @param int $start Start |
| 453 | * @param int $end End |
| 454 | * @param int $step Step |
| 455 | * |
| 456 | * @return void |
| 457 | * |
| 458 | * @since 1.0.0 |
| 459 | */ |
| 460 | public function addMonth(int $start = null, int $end = null, int $step = null) : void |
| 461 | { |
| 462 | $this->month[] = [ |
| 463 | 'start' => $start, |
| 464 | 'end' => $end, |
| 465 | 'step' => $step, |
| 466 | ]; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Get year. |
| 471 | * |
| 472 | * @return array |
| 473 | * |
| 474 | * @since 1.0.0 |
| 475 | */ |
| 476 | public function getYear() : array |
| 477 | { |
| 478 | return $this->year; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Set year. |
| 483 | * |
| 484 | * @param int $index Index of the value to change |
| 485 | * @param int $start Start |
| 486 | * @param int $end End |
| 487 | * @param int $step Step |
| 488 | * |
| 489 | * @return void |
| 490 | * |
| 491 | * @since 1.0.0 |
| 492 | */ |
| 493 | public function setYear(int $index, int $start = null, int $end = null, int $step = null) : void |
| 494 | { |
| 495 | $this->year[$index] = [ |
| 496 | 'start' => $start, |
| 497 | 'end' => $end, |
| 498 | 'step' => $step, |
| 499 | ]; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Add year. |
| 504 | * |
| 505 | * @param int $start Start |
| 506 | * @param int $end End |
| 507 | * @param int $step Step |
| 508 | * |
| 509 | * @return void |
| 510 | * |
| 511 | * @since 1.0.0 |
| 512 | */ |
| 513 | public function addYear(int $start = null, int $end = null, int $step = null) : void |
| 514 | { |
| 515 | $this->year[] = [ |
| 516 | 'start' => $start, |
| 517 | 'end' => $end, |
| 518 | 'step' => $step, |
| 519 | ]; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Create string representation. |
| 524 | * |
| 525 | * @return string |
| 526 | * |
| 527 | * @since 1.0.0 |
| 528 | */ |
| 529 | public function serialize() : string |
| 530 | { |
| 531 | $serialized = \json_encode([ |
| 532 | 'start' => $this->start->format('Y-m-d H:i:s'), |
| 533 | 'end' => $this->end === null ? null : $this->end->format('Y-m-d H:i:s'), |
| 534 | 'maxDuration' => $this->maxDuration, |
| 535 | 'minute' => $this->minute, |
| 536 | 'hour' => $this->hour, |
| 537 | 'dayOfMonth' => $this->dayOfMonth, |
| 538 | 'dayOfWeek' => $this->dayOfWeek, |
| 539 | 'year' => $this->year, |
| 540 | ]); |
| 541 | |
| 542 | return $serialized === false ? '{}' : $serialized; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Unserialize. |
| 547 | * |
| 548 | * @param string $serialized String to unserialize |
| 549 | * |
| 550 | * @return void |
| 551 | * |
| 552 | * @since 1.0.0 |
| 553 | */ |
| 554 | public function unserialize(mixed $serialized) : void |
| 555 | { |
| 556 | /** @var array $data */ |
| 557 | $data = \json_decode($serialized, true); |
| 558 | |
| 559 | $this->start = new \DateTime($data['start']); |
| 560 | $this->end = $data['end'] === null ? null : new \DateTime($data['end']); |
| 561 | $this->maxDuration = $data['maxDuration']; |
| 562 | $this->minute = $data['minute']; |
| 563 | $this->hour = $data['hour']; |
| 564 | $this->dayOfMonth = $data['dayOfMonth']; |
| 565 | $this->dayOfWeek = $data['dayOfWeek']; |
| 566 | $this->year = $data['year']; |
| 567 | } |
| 568 | } |