<?php
namespace JanusHercules\HighVolumeProcess\Domain\Entity;
use App\Utility\DatabaseIdGenerator;
use App\Utility\DateTimeUtility;
use DateTime;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use ValueError;
#[ORM\Entity]
#[ORM\Table(name: 'high_volume_process_tasks')]
#[ORM\UniqueConstraint(
name : 'unique_process_item',
columns: ['high_volume_processes_id', 'item_id']
)]
#[ORM\Index(columns: ['high_volume_processes_id'], name: 'IDX_D76C3F96CE20091')]
#[ORM\Index(columns: ['high_volume_processes_id', 'finished_at', 'error_message'], name: 'idx_task_status')]
#[ORM\Index(columns: ['finished_at'], name: 'idx_task_finished')]
class HighVolumeProcessTask
{
/**
* @throws Exception
*/
public function __construct(
HighVolumeProcess $process,
string $itemId
) {
if (mb_strlen($itemId) > 128) {
throw new ValueError('The itemId cannot exceed 128 characters.');
}
$this->process = $process;
$this->itemId = $itemId;
$this->createdAt = DateTimeUtility::createDateTimeUtc();
}
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: DatabaseIdGenerator::class)]
#[ORM\Column(
type : Types::GUID,
unique: true
)]
private ?string $id = null;
public function getId(): ?string
{
return $this->id;
}
#[ORM\ManyToOne(
targetEntity: HighVolumeProcess::class
)]
#[ORM\JoinColumn(
name : 'high_volume_processes_id',
referencedColumnName: 'id',
nullable : false,
onDelete : 'CASCADE'
)]
private HighVolumeProcess $process;
public function getProcess(): HighVolumeProcess
{
return $this->process;
}
#[ORM\Column(
type: Types::DATETIME_MUTABLE
)]
private DateTime $createdAt;
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
#[ORM\Column(
type : Types::DATETIME_MUTABLE,
nullable: true
)]
private ?DateTime $finishedAt = null;
public function getFinishedAt(): ?DateTime
{
return $this->finishedAt;
}
public function setFinishedAt(DateTime $finishedAt): void
{
$this->finishedAt = $finishedAt;
}
#[ORM\Column(
type : Types::TEXT,
nullable: true
)]
private ?string $errorMessage = null;
public function getErrorMessage(): ?string
{
return $this->errorMessage;
}
public function setErrorMessage(string $errorMessage): void
{
$this->errorMessage = mb_substr($errorMessage, 0, 1024);
}
#[ORM\Column(
type : Types::STRING,
length : 128,
nullable: false
)]
private string $itemId;
public function getItemId(): string
{
return $this->itemId;
}
#[ORM\Column(
type : Types::STRING,
length : 512,
nullable: true
)]
private ?string $resultValue = null;
public function getResultValue(): ?string
{
return $this->resultValue;
}
public function setResultValue(string $resultValue): void
{
if (mb_strlen($resultValue) > 512) {
throw new ValueError('The resultValue cannot exceed 512 characters.');
}
$this->resultValue = $resultValue;
}
}