chrismichaels84 /
mistletoe
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Mistletoe; |
||
| 3 | use Mistletoe\Contracts\TaskRunnerInterface; |
||
| 4 | use Mistletoe\Runners\GenericTaskRunner; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Class TaskPlanner |
||
| 8 | * @package FBS\Planner |
||
| 9 | */ |
||
| 10 | class TaskPlanner |
||
| 11 | { |
||
| 12 | /** @var array Tasks currently scheduled */ |
||
| 13 | protected $tasks = []; |
||
| 14 | |||
| 15 | /** @var TaskBag */ |
||
| 16 | protected $currentTask; |
||
| 17 | |||
| 18 | /** @var string Namespace prefix */ |
||
| 19 | protected $nsPrefix = ''; |
||
| 20 | |||
| 21 | protected $currentEnvironment = self::PRODUCTION_ENVIRONMENT; |
||
| 22 | |||
| 23 | /* Environment Constants */ |
||
| 24 | const PRODUCTION_ENVIRONMENT = 'PRODUCTION'; |
||
| 25 | const DEVELOPMENT_ENVIRONMENT = 'DEVELOPMENT'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Mistletoe\Contracts\TaskRunnerInterface |
||
| 29 | */ |
||
| 30 | protected $taskRunner; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var integer |
||
| 34 | */ |
||
| 35 | protected $closureIncrement = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $currentTime; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $testing = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Begin a new Task Chain |
||
| 49 | * @param string|\Closure $task |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | 26 | public function add($task) |
|
| 53 | { |
||
| 54 | 26 | $body = null; |
|
| 55 | 26 | if ($task instanceof \Closure || $task instanceof Command) { |
|
| 56 | 1 | $body = $task; |
|
| 57 | 1 | $task = $this->getNextClosureIncrement(); |
|
| 58 | 1 | } |
|
| 59 | |||
| 60 | 26 | $this->createNewTask($task, $body); |
|
| 61 | 26 | return $this; |
|
| 62 | } |
||
| 63 | |||
| 64 | /* Schedule a full expression */ |
||
| 65 | /** |
||
| 66 | * Add a full expression |
||
| 67 | * @param string $expression |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | 8 | public function schedule($expression) |
|
| 71 | { |
||
| 72 | 8 | $this->getCurrentTask()->setCronExpression($expression); |
|
| 73 | 8 | return $this; |
|
| 74 | } |
||
| 75 | |||
| 76 | 5 | public function always() |
|
| 77 | { |
||
| 78 | 5 | return $this->schedule('* * * * *'); |
|
| 79 | } |
||
| 80 | |||
| 81 | /* Intervals */ |
||
| 82 | /** |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | 3 | public function yearly() |
|
| 86 | { |
||
| 87 | 3 | $this->getCurrentTask()->setInterval('@yearly'); |
|
| 88 | 3 | return $this; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return TaskPlanner |
||
| 93 | */ |
||
| 94 | 2 | public function annually() |
|
| 95 | { |
||
| 96 | 2 | return $this->yearly(); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | 5 | public function monthly() |
|
| 103 | { |
||
| 104 | 5 | $this->getCurrentTask()->setInterval('@monthly'); |
|
| 105 | 5 | return $this; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return $this |
||
| 110 | */ |
||
| 111 | 2 | public function weekly() |
|
| 112 | { |
||
| 113 | 2 | $this->getCurrentTask()->setInterval('@weekly'); |
|
| 114 | 2 | return $this; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | 10 | public function daily() |
|
| 121 | { |
||
| 122 | 10 | $this->getCurrentTask()->setInterval('@daily'); |
|
| 123 | 10 | return $this; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | 3 | public function hourly() |
|
| 130 | { |
||
| 131 | 3 | $this->getCurrentTask()->setInterval('@hourly'); |
|
| 132 | 3 | return $this; |
|
| 133 | } |
||
| 134 | |||
| 135 | /* Time intervals */ |
||
| 136 | |||
| 137 | 1 | public function everyXHours($hrs) |
|
| 138 | { |
||
| 139 | 1 | $this->getCurrentTask()->setHour("*/{$hrs}"); |
|
| 140 | 1 | return $this; |
|
| 141 | } |
||
| 142 | |||
| 143 | 1 | public function everyXMinutes($mins) |
|
| 144 | { |
||
| 145 | 1 | $this->getCurrentTask()->setMinute("*/{$mins}"); |
|
| 146 | 1 | return $this; |
|
| 147 | } |
||
| 148 | |||
| 149 | /* @todo: this could be cleaned up and expanded for minutes, hours, days, and weeks |
||
| 150 | * @param $name |
||
| 151 | * @param $arguments |
||
| 152 | * @return |
||
| 153 | */ |
||
| 154 | 1 | public function __call($name, $arguments) |
|
| 155 | { |
||
| 156 | 1 | if (strpos($name, 'every') === false) { |
|
| 157 | throw new \BadMethodCallException("{$name} is not a valid method. Did you mean everyXHours? or everyXMinutes()?"); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Get Increment |
||
| 161 | 1 | $increment = $name; |
|
| 162 | 1 | $increment = str_replace('every', '', $increment); |
|
| 163 | 1 | $increment = str_replace('Minutes', '', $increment); |
|
| 164 | 1 | $increment = str_replace('Hours', '', $increment); |
|
| 165 | 1 | $increment = (int) $increment; |
|
| 166 | |||
| 167 | // Get Hours or Minutes |
||
| 168 | 1 | $method = $name; |
|
| 169 | 1 | if (strpos($method, 'Minutes')){ |
|
| 170 | 1 | $method = 'Minutes'; |
|
| 171 | 1 | } |
|
| 172 | |||
| 173 | 1 | if (strpos($method, 'Hours')) { |
|
| 174 | 1 | $method = 'Hours'; |
|
| 175 | 1 | } |
|
| 176 | |||
| 177 | // Build Method |
||
| 178 | 1 | $method = "everyX{$method}"; |
|
| 179 | |||
| 180 | // Execute |
||
| 181 | 1 | return $this->$method($increment); |
|
| 182 | } |
||
| 183 | |||
| 184 | /* Times */ |
||
| 185 | /** |
||
| 186 | * @param string $time |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | 12 | public function at($time) |
|
| 190 | { |
||
| 191 | 12 | $this->getCurrentTask()->addTime($time); |
|
| 192 | 12 | return $this; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return TaskPlanner |
||
| 197 | */ |
||
| 198 | 3 | public function atMidnight() |
|
| 199 | { |
||
| 200 | 3 | return $this->at('24:00'); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return TaskPlanner |
||
| 205 | */ |
||
| 206 | 1 | public function atNoon() |
|
| 207 | { |
||
| 208 | 1 | return $this->at('12:00'); |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string|array|integer $minute |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | 2 | public function atMinute($minute) |
|
| 216 | { |
||
| 217 | 2 | $this->getCurrentTask()->addMinute($minute); |
|
| 218 | 2 | return $this; |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string|array|integer $minute |
||
| 223 | * @return TaskPlanner |
||
| 224 | */ |
||
| 225 | 2 | public function andAtMinute($minute) |
|
| 226 | { |
||
| 227 | 2 | return $this->atMinute($minute); |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string|array|integer $hour |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | 2 | public function atHour($hour) |
|
| 235 | { |
||
| 236 | 2 | $this->getCurrentTask()->addHour($hour); |
|
| 237 | 2 | return $this; |
|
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string|array|integer $hour |
||
| 242 | * @return TaskPlanner |
||
| 243 | */ |
||
| 244 | 1 | public function andAtHour($hour) |
|
| 245 | { |
||
| 246 | 1 | return $this->atHour($hour); |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string|array|integer $date |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 4 | public function on($date) |
|
| 254 | { |
||
| 255 | 4 | $this->getCurrentTask()->addDate($date); |
|
| 256 | 4 | return $this; |
|
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string|array|integer $date |
||
| 261 | * @return TaskPlanner |
||
| 262 | */ |
||
| 263 | 2 | public function andOn($date) |
|
| 264 | { |
||
| 265 | 2 | return $this->on($date); |
|
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string|array|integer $day |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | 5 | public function onDay($day) |
|
| 273 | { |
||
| 274 | 5 | $this->getCurrentTask()->addDay($day); |
|
| 275 | 5 | return $this; |
|
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string|array|integer $day |
||
| 280 | * @return TaskPlanner |
||
| 281 | */ |
||
| 282 | 4 | public function andOnDay($day) |
|
| 283 | { |
||
| 284 | 4 | return $this->onDay($day); |
|
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string|array|integer $month |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | 4 | public function onMonth($month) |
|
| 292 | { |
||
| 293 | 4 | $this->getCurrentTask()->addMonth($month); |
|
| 294 | 4 | return $this; |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string|array|integer $month |
||
| 299 | * @return TaskPlanner |
||
| 300 | */ |
||
| 301 | 1 | public function andOnMonth($month) |
|
| 302 | { |
||
| 303 | 1 | return $this->onMonth($month); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string|array|integer $weekday |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | 3 | public function onWeekday($weekday) |
|
| 311 | { |
||
| 312 | 3 | $this->getCurrentTask()->addWeekday($weekday); |
|
| 313 | 3 | return $this; |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string|array|integer $weekday |
||
| 318 | * @return TaskPlanner |
||
| 319 | */ |
||
| 320 | public function andOnWeekday($weekday) |
||
| 321 | { |
||
| 322 | return $this->onWeekday($weekday); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | 2 | public function onSunday() |
|
| 329 | { |
||
| 330 | 2 | return $this->onWeekday(0); |
|
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return TaskPlanner |
||
| 335 | */ |
||
| 336 | public function onMonday() |
||
| 337 | { |
||
| 338 | return $this->onWeekday(1); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return TaskPlanner |
||
| 343 | */ |
||
| 344 | public function onTuesday() |
||
| 345 | { |
||
| 346 | return $this->onWeekday(2); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return TaskPlanner |
||
| 351 | */ |
||
| 352 | public function onWednesday() |
||
| 353 | { |
||
| 354 | return $this->onWeekday(3); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return TaskPlanner |
||
| 359 | */ |
||
| 360 | 1 | public function onThursday() |
|
| 361 | { |
||
| 362 | return $this->onWeekday(4); |
||
| 363 | 1 | } |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @return TaskPlanner |
||
| 367 | */ |
||
| 368 | public function onFriday() |
||
| 369 | { |
||
| 370 | return $this->onWeekday(5); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return TaskPlanner |
||
| 375 | */ |
||
| 376 | 3 | public function onSaturday() |
|
| 377 | { |
||
| 378 | 3 | return $this->onWeekday(6); |
|
| 379 | } |
||
| 380 | |||
| 381 | /* Environments */ |
||
| 382 | /** |
||
| 383 | * Restrict task to a specific environment |
||
| 384 | * @param string $environment |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | 2 | public function onEnvironment($environment) |
|
| 388 | { |
||
| 389 | 2 | $this->getCurrentTask()->addEnvironment(strtoupper($environment)); |
|
| 390 | 2 | return $this; |
|
| 391 | } |
||
| 392 | |||
| 393 | public function setEnvironments(array $environment) |
||
| 394 | { |
||
| 395 | 3 | array_map(function($value) { |
|
| 396 | 3 | return strtoupper($value); |
|
| 397 | 3 | }, $environment); |
|
| 398 | |||
| 399 | 3 | $this->getCurrentTask()->setEnvironments($environment); |
|
| 400 | 3 | return $this; |
|
| 401 | } |
||
| 402 | |||
| 403 | /* Alias for onEnvironment() */ |
||
| 404 | /** |
||
| 405 | * @return TaskPlanner |
||
| 406 | */ |
||
| 407 | 2 | public function onProductionOnly() |
|
| 408 | { |
||
| 409 | 2 | return $this->setEnvironments([static::PRODUCTION_ENVIRONMENT]); |
|
| 410 | } |
||
| 411 | |||
| 412 | /* Alias for onEnvironment() */ |
||
| 413 | /** |
||
| 414 | * @return TaskPlanner |
||
| 415 | */ |
||
| 416 | 2 | public function onDevelopmentOnly() |
|
| 417 | { |
||
| 418 | 2 | return $this->setEnvironments([static::DEVELOPMENT_ENVIRONMENT]); |
|
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $task |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | 7 | public function followedBy($task) |
|
| 426 | { |
||
| 427 | 7 | $this->getCurrentTask()->addFollowedBy($task); |
|
| 428 | 7 | return $this; |
|
| 429 | } |
||
| 430 | |||
| 431 | /* Running Tasks */ |
||
| 432 | /** |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | 1 | public function runDueTasks() |
|
| 436 | { |
||
| 437 | 1 | return $this->getTaskRunner()->runDueTasks(); |
|
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function runAllTasks() |
||
| 444 | { |
||
| 445 | return $this->getTaskRunner()->runAllTasks(); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param $task |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function runTask($task) |
||
| 453 | { |
||
| 454 | return $this->getTaskRunner()->runTask($task); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param array $tasks |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function runTasks(array $tasks) |
||
| 462 | { |
||
| 463 | return $this->getTaskRunner()->runTasks($tasks); |
||
| 464 | } |
||
| 465 | |||
| 466 | /* Getters and Setters */ |
||
| 467 | /** |
||
| 468 | * @param \Mistletoe\Contracts\TaskRunnerInterface $runner |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function setTaskRunner(TaskRunnerInterface $runner) |
||
| 472 | { |
||
| 473 | $this->taskRunner = $runner; |
||
| 474 | return $this; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return TaskRunnerInterface |
||
| 479 | */ |
||
| 480 | 1 | public function getTaskRunner() |
|
| 481 | { |
||
| 482 | 1 | $runner = $this->taskRunner ?: (new GenericTaskRunner($this->tasks)); |
|
| 483 | $runner |
||
| 484 | 1 | ->setTaskBags($this->tasks) |
|
| 485 | 1 | ->setCurrentEnvironment(strtoupper($this->currentEnvironment)) |
|
| 486 | 1 | ->setCurrentTime($this->currentTime) |
|
| 487 | 1 | ->flagForTesting($this->testing); |
|
| 488 | |||
| 489 | 1 | return $runner; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | 7 | public function getTasks() |
|
| 496 | { |
||
| 497 | 7 | return $this->tasks; |
|
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $name |
||
| 502 | * @return TaskBag |
||
| 503 | */ |
||
| 504 | 15 | public function getTask($name) |
|
| 505 | { |
||
| 506 | 15 | return $this->tasks[$name]; |
|
| 507 | } |
||
| 508 | |||
| 509 | public function getDueTasks() |
||
| 510 | { |
||
| 511 | return $this->getTaskRunner()->getDueTasks(); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function getNsPrefix() |
||
| 518 | { |
||
| 519 | return $this->nsPrefix; |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param string $nsPrefix |
||
| 524 | * @return $this |
||
| 525 | */ |
||
| 526 | public function setNsPrefix($nsPrefix) |
||
| 527 | { |
||
| 528 | $this->nsPrefix = $nsPrefix; |
||
| 529 | return $this; |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | public function getCurrentEnvironment() |
||
| 536 | { |
||
| 537 | return $this->currentEnvironment; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param string $currentEnvironment |
||
| 542 | * @return $this |
||
| 543 | */ |
||
| 544 | public function setCurrentEnvironment($currentEnvironment) |
||
| 545 | { |
||
| 546 | $this->currentEnvironment = strtoupper($currentEnvironment); |
||
| 547 | return $this; |
||
| 548 | } |
||
| 549 | |||
| 550 | /* Internal Methods */ |
||
| 551 | /** |
||
| 552 | * @param string $task |
||
| 553 | * @param \Closure|null $body |
||
| 554 | */ |
||
| 555 | 26 | protected function createNewTask($task, $body = null) |
|
| 556 | { |
||
| 557 | 26 | $this->tasks[$task] = new TaskBag($task); |
|
| 558 | 26 | $this->setCurrentTask($task); |
|
| 559 | |||
| 560 | 26 | $this->getCurrentTask()->setTask( |
|
| 561 | 26 | ($body) ? $body : $task |
|
|
0 ignored issues
–
show
|
|||
| 562 | 26 | ); |
|
| 563 | 26 | } |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $task |
||
| 567 | */ |
||
| 568 | 26 | protected function setCurrentTask($task) |
|
| 569 | { |
||
| 570 | 26 | $this->currentTask = $this->tasks[$task]; |
|
| 571 | 26 | } |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @return TaskBag |
||
| 575 | */ |
||
| 576 | 26 | protected function getCurrentTask() |
|
| 577 | { |
||
| 578 | 26 | return $this->currentTask; |
|
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @return null|string |
||
| 583 | */ |
||
| 584 | 1 | private function getNextClosureIncrement() |
|
| 585 | { |
||
| 586 | 1 | $this->closureIncrement++; |
|
| 587 | 1 | return "_task{$this->closureIncrement}"; |
|
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function getCurrentTime() |
||
| 594 | { |
||
| 595 | return $this->currentTime; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param mixed $currentTime |
||
| 600 | * @return $this |
||
| 601 | */ |
||
| 602 | 1 | public function setCurrentTime($currentTime) |
|
| 603 | { |
||
| 604 | 1 | $this->currentTime = $currentTime; |
|
| 605 | 1 | return $this; |
|
| 606 | } |
||
| 607 | |||
| 608 | public function flagForTesting() |
||
| 609 | { |
||
| 610 | $this->testing = true; |
||
| 611 | } |
||
| 612 | } |
||
| 613 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.