src/Entity/Profile/FileProcessingTask.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Profile;
  3. use App\Entity\ContainsDomainEvents;
  4. use App\Entity\DomainEventsRecorderTrait;
  5. use App\Event\Profile\FileProcessingTaskWasCompleted;
  6. use App\Message\ProcessUploadedVideoMessage;
  7. use DateTimeImmutable;
  8. use DateTimeZone;
  9. use Doctrine\ORM\Mapping AS ORM;
  10. #[ORM\Entity]
  11. #[ORM\Table(name'file_processing_tasks')]
  12. class FileProcessingTask implements ContainsDomainEvents
  13. {
  14.     use DomainEventsRecorderTrait;
  15.     public const STATE_PENDING 'pending';
  16.     public const STATE_PROCESSING 'processing';
  17.     public const STATE_COMPLETED 'completed';
  18.     public const STATE_FAILED 'failed';
  19.     #[ORM\IdORM\GeneratedValue(strategy'AUTO'), ORM\Column(name'id'type'integer')]
  20.     private int $id;
  21.     #[ORM\JoinColumn(name'profile_id'referencedColumnName'id')]
  22.     #[ORM\ManyToOne(targetEntityProfile::class, inversedBy'processingFiles')]
  23.     protected Profile $profile;
  24.     #[ORM\Column(name'path'type'string'length128)]
  25.     protected string $path;
  26.     #[ORM\Column(name'state'type'string'length32)]
  27.     protected string $state;
  28.     #[ORM\Column(name'added_at'type'datetimetz_immutable')]
  29.     protected DateTimeImmutable $addedAt;
  30.     #[ORM\Column(name'started_at'type'datetimetz_immutable'nullabletrue)]
  31.     protected ?DateTimeImmutable $startedAt null;
  32.     #[ORM\Column(name'last_error'type'text'nullabletrue)]
  33.     protected ?string $lastError null;
  34.     public function __construct(Profile $profilestring $path)
  35.     {
  36.         $this->profile $profile;
  37.         $this->path $path;
  38.         $this->state self::STATE_PENDING;
  39.         $this->addedAt = new DateTimeImmutable('now', new DateTimeZone('UTC'));
  40.     }
  41.     public function start(): void
  42.     {
  43.         $this->state self::STATE_PROCESSING;
  44.         $this->startedAt = new DateTimeImmutable('now', new DateTimeZone('UTC'));
  45.     }
  46.     public function isStarted(): bool
  47.     {
  48.         return $this->state === self::STATE_PROCESSING;
  49.     }
  50.     public function completeVideoTranscoding(string $videoPathstring $posterPath): void
  51.     {
  52.         $this->state self::STATE_COMPLETED;
  53.         $this->profile->addVideo($videoPath$posterPath);
  54.         $this->recordEvent(new FileProcessingTaskWasCompleted($this->profile$this));
  55.     }
  56.     public function isCompleted(): bool
  57.     {
  58.         return $this->state === self::STATE_COMPLETED;
  59.     }
  60.     public function fail(string $errorMessage): void
  61.     {
  62.         $this->state self::STATE_FAILED;
  63.         $this->lastError $errorMessage;
  64.     }
  65.     public function createCommandToProcessUploadedVideo(): ProcessUploadedVideoMessage
  66.     {
  67.         return new ProcessUploadedVideoMessage(
  68.             $this->id,
  69.             $this->profile->getId(),
  70.             $this->path,
  71.         );
  72.     }
  73. }