2022-04-27 14:24:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Entity;
|
|
|
|
|
|
|
|
use App\Repository\JobRepository;
|
2022-05-18 13:21:09 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2022-05-16 18:00:44 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2022-04-27 14:24:48 +02:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
|
|
|
|
#[ORM\Entity(repositoryClass: JobRepository::class)]
|
|
|
|
class Job
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int|null
|
|
|
|
*/
|
|
|
|
#[ORM\Id()]
|
|
|
|
#[ORM\GeneratedValue(strategy: "AUTO")]
|
|
|
|
#[ORM\Column(type: "integer")]
|
|
|
|
private ?int $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
#[ORM\Column(type: "string", length: 100)]
|
|
|
|
private string $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-05-18 17:14:25 +02:00
|
|
|
#[ORM\Column(name: "`data`", type: "text")]
|
2022-04-27 14:24:48 +02:00
|
|
|
private string $data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2022-05-18 17:14:25 +02:00
|
|
|
#[ORM\Column(name: "`interval`", type: "integer")]
|
2022-04-27 14:24:48 +02:00
|
|
|
private int $interval;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
#[ORM\Column(type: "integer")]
|
|
|
|
private int $nextrun;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
#[ORM\Column(type: "integer", nullable: true)]
|
2022-05-13 15:24:05 +02:00
|
|
|
private ?int $lastrun;
|
2022-04-27 14:24:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
#[ORM\Column(type: "integer")]
|
|
|
|
private int $running;
|
|
|
|
|
2022-05-16 18:00:44 +02:00
|
|
|
/**
|
|
|
|
* @var Collection
|
|
|
|
*/
|
2022-05-18 11:12:35 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: "App\Entity\Run", mappedBy: "job", cascade: ["remove"])]
|
2022-05-16 18:00:44 +02:00
|
|
|
private Collection $runs;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->runs = new ArrayCollection();
|
|
|
|
}
|
|
|
|
|
2022-04-27 14:24:48 +02:00
|
|
|
/**
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function getId(): ?int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $id
|
|
|
|
* @return Job
|
|
|
|
*/
|
|
|
|
public function setId(?int $id): Job
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return Job
|
|
|
|
*/
|
|
|
|
public function setName(string $name): Job
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:06:46 +02:00
|
|
|
public function getData(?string $name = ''): mixed
|
2022-04-27 14:24:48 +02:00
|
|
|
{
|
2022-05-17 16:06:46 +02:00
|
|
|
$data = json_decode($this->data, true);
|
|
|
|
if(!empty($name)) {
|
|
|
|
$names = explode('.', $name);
|
|
|
|
foreach($names as $item) {
|
|
|
|
if(!isset($data[$item])) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
$data = $data[$item];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
2022-04-27 14:24:48 +02:00
|
|
|
}
|
|
|
|
|
2022-05-18 13:21:09 +02:00
|
|
|
public function setData(string $name, mixed $value): Job
|
2022-05-13 15:24:05 +02:00
|
|
|
{
|
2022-05-18 17:14:25 +02:00
|
|
|
$data = json_decode($this->data ?? '{}', true);
|
2022-05-17 16:06:46 +02:00
|
|
|
if (!empty($name)) {
|
|
|
|
$this->addDataItem($data, $name, $value);
|
|
|
|
}
|
2022-05-13 15:24:05 +02:00
|
|
|
$this->data = json_encode($data);
|
2022-05-17 16:06:46 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addDataItem(array &$data, array|string $name, mixed $value): bool
|
|
|
|
{
|
|
|
|
$names = is_array($name) ? $name : explode('.', $name);
|
|
|
|
$current = $names[0];
|
2022-05-18 17:14:25 +02:00
|
|
|
if(count($names) > 1) {
|
2022-05-17 16:06:46 +02:00
|
|
|
unset($names[0]);
|
2022-05-18 17:14:25 +02:00
|
|
|
if(!isset($data[$current])) $data[$current] = [];
|
2022-05-17 16:06:46 +02:00
|
|
|
$this->addDataItem($data[$current], array_values($names), $value);
|
|
|
|
} else {
|
|
|
|
$data[$names[0]] = $value;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-13 15:24:05 +02:00
|
|
|
|
2022-05-17 16:06:46 +02:00
|
|
|
public function removeData(?string $name = ''): mixed
|
|
|
|
{
|
|
|
|
$data = json_decode($this->data, true);
|
|
|
|
if (!empty($name)) {
|
|
|
|
$this->removeDataItem($data, $name);
|
|
|
|
}
|
2022-05-18 13:21:09 +02:00
|
|
|
$this->data = json_encode($data);
|
2022-05-13 15:24:05 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:06:46 +02:00
|
|
|
private function removeDataItem(array &$data, array|string $name): bool
|
|
|
|
{
|
|
|
|
$names = is_array($name) ? $name : explode('.', $name);
|
|
|
|
$current = $names[0];
|
2022-05-19 20:25:05 +02:00
|
|
|
if(!isset($data[$current])) {
|
|
|
|
return false;
|
|
|
|
} elseif(is_array($data[$current]) && isset($names[1])) {
|
2022-05-17 16:06:46 +02:00
|
|
|
unset($names[0]);
|
|
|
|
$this->removeDataItem($data[$current], array_values($names));
|
|
|
|
} else {
|
2022-05-19 20:25:05 +02:00
|
|
|
if(isset($data[$names[0]])) unset($data[$names[0]]);
|
2022-05-17 16:06:46 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasData($name): bool
|
|
|
|
{
|
|
|
|
return !empty($this->getData($name));
|
|
|
|
}
|
2022-04-27 14:24:48 +02:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getInterval(): int
|
|
|
|
{
|
|
|
|
return $this->interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $interval
|
|
|
|
* @return Job
|
|
|
|
*/
|
|
|
|
public function setInterval(int $interval): Job
|
|
|
|
{
|
|
|
|
$this->interval = $interval;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNextrun(): int
|
|
|
|
{
|
|
|
|
return $this->nextrun;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $nextrun
|
|
|
|
* @return Job
|
|
|
|
*/
|
|
|
|
public function setNextrun(int $nextrun): Job
|
|
|
|
{
|
|
|
|
$this->nextrun = $nextrun;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2022-05-13 15:24:05 +02:00
|
|
|
public function getLastrun(): ?int
|
2022-04-27 14:24:48 +02:00
|
|
|
{
|
|
|
|
return $this->lastrun;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $lastrun
|
|
|
|
* @return Job
|
|
|
|
*/
|
2022-05-18 17:14:25 +02:00
|
|
|
public function setLastrun(?int $lastrun): Job
|
2022-04-27 14:24:48 +02:00
|
|
|
{
|
|
|
|
$this->lastrun = $lastrun;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getRunning(): int
|
|
|
|
{
|
|
|
|
return $this->running;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $running
|
|
|
|
* @return Job
|
|
|
|
*/
|
|
|
|
public function setRunning(int $running): Job
|
|
|
|
{
|
|
|
|
$this->running = $running;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:05:55 +02:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2022-05-18 13:21:09 +02:00
|
|
|
public function getRuns(): Collection
|
2022-05-18 13:05:55 +02:00
|
|
|
{
|
|
|
|
return $this->runs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $runs
|
|
|
|
* @return Job
|
|
|
|
*/
|
2022-05-18 13:21:09 +02:00
|
|
|
public function setRuns(Collection $runs): Job
|
2022-05-18 13:05:55 +02:00
|
|
|
{
|
|
|
|
$this->runs = $runs;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-04-27 14:24:48 +02:00
|
|
|
}
|