vendor/shapecode/cron-bundle/src/Entity/CronJob.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shapecode\Bundle\CronBundle\Entity;
  4. use Cron\CronExpression;
  5. use DateTimeInterface;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Shapecode\Bundle\CronBundle\Repository\CronJobRepository;
  11. #[ORM\Entity(repositoryClassCronJobRepository::class)]
  12. class CronJob extends AbstractEntity
  13. {
  14.     #[ORM\Column(typeTypes::STRING)]
  15.     private string $command;
  16.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  17.     private string|null $arguments null;
  18.     #[ORM\Column(typeTypes::STRINGnullabletrue)]
  19.     private string|null $description null;
  20.     #[ORM\Column(typeTypes::INTEGERoptions: ['unsigned' => true'default' => 0])]
  21.     private int $runningInstances 0;
  22.     #[ORM\Column(typeTypes::INTEGERoptions: ['unsigned' => true'default' => 1])]
  23.     private int $maxInstances 1;
  24.     #[ORM\Column(typeTypes::INTEGERoptions: ['unsigned' => true'default' => 1])]
  25.     private int $number 1;
  26.     #[ORM\Column(typeTypes::STRING)]
  27.     private string $period;
  28.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  29.     private DateTimeInterface|null $lastUse null;
  30.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  31.     private DateTimeInterface $nextRun;
  32.     /** @var Collection<int, CronJobResult>*/
  33.     #[ORM\OneToMany(mappedBy'cronJob'targetEntityCronJobResult::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  34.     private Collection $results;
  35.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => true])]
  36.     private bool $enable true;
  37.     public function __construct(
  38.         string $command,
  39.         string $period,
  40.     ) {
  41.         $this->command $command;
  42.         $this->period  $period;
  43.         $this->results = new ArrayCollection();
  44.         $this->calculateNextRun();
  45.     }
  46.     public static function create(string $commandstring $period): self
  47.     {
  48.         return new self($command$period);
  49.     }
  50.     public function getCommand(): string
  51.     {
  52.         return $this->command;
  53.     }
  54.     public function getFullCommand(): string
  55.     {
  56.         $arguments '';
  57.         if ($this->getArguments() !== null) {
  58.             $arguments ' ' $this->getArguments();
  59.         }
  60.         return $this->getCommand() . $arguments;
  61.     }
  62.     public function getArguments(): string|null
  63.     {
  64.         return $this->arguments;
  65.     }
  66.     public function setArguments(string|null $arguments): self
  67.     {
  68.         $this->arguments $arguments;
  69.         return $this;
  70.     }
  71.     public function getDescription(): string|null
  72.     {
  73.         return $this->description;
  74.     }
  75.     public function setDescription(string|null $description): self
  76.     {
  77.         $this->description $description;
  78.         return $this;
  79.     }
  80.     public function getRunningInstances(): int
  81.     {
  82.         return $this->runningInstances;
  83.     }
  84.     public function increaseRunningInstances(): self
  85.     {
  86.         ++$this->runningInstances;
  87.         return $this;
  88.     }
  89.     public function decreaseRunningInstances(): self
  90.     {
  91.         --$this->runningInstances;
  92.         return $this;
  93.     }
  94.     public function getMaxInstances(): int
  95.     {
  96.         return $this->maxInstances;
  97.     }
  98.     public function setMaxInstances(int $maxInstances): self
  99.     {
  100.         $this->maxInstances $maxInstances;
  101.         return $this;
  102.     }
  103.     public function getNumber(): int
  104.     {
  105.         return $this->number;
  106.     }
  107.     public function setNumber(int $number): self
  108.     {
  109.         $this->number $number;
  110.         return $this;
  111.     }
  112.     public function getPeriod(): string
  113.     {
  114.         return $this->period;
  115.     }
  116.     public function setPeriod(string $period): self
  117.     {
  118.         $this->period $period;
  119.         return $this;
  120.     }
  121.     public function getLastUse(): DateTimeInterface|null
  122.     {
  123.         return $this->lastUse;
  124.     }
  125.     public function setLastUse(DateTimeInterface $lastUse): self
  126.     {
  127.         $this->lastUse $lastUse;
  128.         return $this;
  129.     }
  130.     public function setNextRun(DateTimeInterface $nextRun): self
  131.     {
  132.         $this->nextRun $nextRun;
  133.         return $this;
  134.     }
  135.     public function getNextRun(): DateTimeInterface
  136.     {
  137.         return $this->nextRun;
  138.     }
  139.     /** @return Collection<int, CronJobResult> */
  140.     public function getResults(): Collection
  141.     {
  142.         return $this->results;
  143.     }
  144.     public function setEnable(bool $enable): self
  145.     {
  146.         $this->enable $enable;
  147.         return $this;
  148.     }
  149.     public function isEnable(): bool
  150.     {
  151.         return $this->enable;
  152.     }
  153.     public function enable(): self
  154.     {
  155.         return $this->setEnable(true);
  156.     }
  157.     public function disable(): self
  158.     {
  159.         return $this->setEnable(false);
  160.     }
  161.     public function calculateNextRun(): self
  162.     {
  163.         $cron = new CronExpression($this->getPeriod());
  164.         $this->setNextRun($cron->getNextRunDate());
  165.         return $this;
  166.     }
  167.     public function __toString(): string
  168.     {
  169.         return $this->getCommand();
  170.     }
  171. }