src/Entity/Profile/Confirmation/ConfirmationRequest.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Profile\Confirmation;
  3. use App\Entity\Profile\Profile;
  4. use Carbon\CarbonImmutable;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Doctrine\ORM\PersistentCollection;
  9. #[ORM\Table(name'profile_confirmation_requests')]
  10. #[ORM\Entity]
  11. #[ORM\InheritanceType('SINGLE_TABLE')]
  12. #[ORM\DiscriminatorColumn(name'type'type'string'length12)]
  13. #[ORM\DiscriminatorMap(['approval' => ApprovalRequest::class, 'moderation' => ModerationRequest::class])]
  14. abstract class ConfirmationRequest
  15. {
  16.     /**
  17.      * Ожидает завершения системных процессов, чтобы перейти в статус WAITING
  18.      */
  19.     public const STATUS_PROCESSING 'PROCESSING';
  20.     /**
  21.      * Ожидает действий модератора
  22.      */
  23.     public const STATUS_WAITING 'WAITING';
  24.     public const STATUS_REJECTED 'REJECTED';
  25.     public const STATUS_APPROVED 'APPROVED';
  26.     #[ORM\Id]
  27.     #[ORM\Column(name'id'type'integer')]
  28.     #[ORM\GeneratedValue(strategy'AUTO')]
  29.     protected int $id;
  30.     #[ORM\JoinColumn(name'profile_id'referencedColumnName'id')]
  31.     #[ORM\ManyToOne(targetEntityProfile::class)]
  32.     protected Profile $profile;
  33.     #[ORM\Column(type'string'columnDefinition"ENUM('PROCESSING', 'WAITING', 'REJECTED', 'APPROVED')")]
  34.     protected string $status;
  35.     #[ORM\Column(type'integer'nullabletrue)]
  36.     protected ?int $reason;
  37.     #[ORM\Column(type'string'nullabletrue)]
  38.     protected ?string $comment;
  39.     #[ORM\Column(type'json'nullabletrue)]
  40.     protected ?array $data;
  41.     /** @var Photo[] */
  42.     #[ORM\OneToMany(targetEntityPhoto::class, mappedBy'confirmationRequest'cascade: ['all'], orphanRemovaltrue)]
  43.     protected Collection $photos;
  44.     /** @var PersistentCollection|Video[] */
  45.     #[ORM\OneToMany(targetEntityVideo::class, mappedBy'confirmationRequest'cascade: ['all'], orphanRemovaltrue)]
  46.     protected Collection $videos;
  47.     #[ORM\Column(name'updated_at'type'datetimetz_immutable'nullabletrue)]
  48.     protected ?\DateTimeImmutable $updatedAt;
  49.     public function __construct(Profile $profile)
  50.     {
  51.         $this->profile $profile;
  52.         $this->updatedAt CarbonImmutable::now();
  53.         $this->status self::STATUS_WAITING;
  54.         $this->photos = new ArrayCollection();
  55.         $this->videos = new ArrayCollection();
  56.         $this->data null;
  57.     }
  58.     public function checkProcessingFiles(): void
  59.     {
  60.         if ($this->profile->hasFilesInProcess()) {
  61.             $this->status self::STATUS_PROCESSING;
  62.         }
  63.     }
  64.     public function getId(): int
  65.     {
  66.         return $this->id;
  67.     }
  68.     public function getProfile(): Profile
  69.     {
  70.         return $this->profile;
  71.     }
  72.     public function setProfile(Profile $profile): void
  73.     {
  74.         $this->profile $profile;
  75.     }
  76.     public function getStatus(): string
  77.     {
  78.         return $this->status;
  79.     }
  80.     public function setStatus(string $status): void
  81.     {
  82.         $this->status $status;
  83.     }
  84.     public function getUpdatedAt(): \DateTimeImmutable
  85.     {
  86.         return $this->updatedAt;
  87.     }
  88.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): void
  89.     {
  90.         $this->updatedAt $updatedAt;
  91.     }
  92.     public function finishProcessing(): void
  93.     {
  94.         if (self::STATUS_PROCESSING === $this->status) {
  95.             $this->status self::STATUS_WAITING;
  96.         }
  97.     }
  98.     public function isWaiting(): bool
  99.     {
  100.         return $this->status === self::STATUS_WAITING || $this->status === self::STATUS_PROCESSING;
  101.     }
  102.     public function isApproved(): bool
  103.     {
  104.         return $this->status == self::STATUS_APPROVED;
  105.     }
  106.     public function isRejected(): bool
  107.     {
  108.         return $this->status == self::STATUS_REJECTED;
  109.     }
  110. //    public function isApprovalRejected(): bool
  111. //    {
  112. //        return $this->approvalRequest && $this->approvalRequest->getStatus() == ApprovalRequest::STATUS_REJECTED;
  113. //    }
  114.     public function approve(): void
  115.     {
  116.         $this->status self::STATUS_APPROVED;
  117.         $this->updatedAt CarbonImmutable::now();
  118.     }
  119.     public function reject(?int $reason null, ?string $comment null): void
  120.     {
  121.         $this->status self::STATUS_REJECTED;
  122.         $this->updatedAt CarbonImmutable::now();
  123.         if($reason) {
  124.             $this->reason $reason;
  125.             if ($comment)
  126.                 $this->comment $comment;
  127.         }
  128.     }
  129.     public function setReason(int $reason): void
  130.     {
  131.         $this->reason $reason;
  132.     }
  133.     public function setComment(?string $comment): void
  134.     {
  135.         $this->comment $comment;
  136.     }
  137.     public function getPhoto(): ?Photo
  138.     {
  139.         return $this->photos->count() ? $this->photos->first() : null;
  140.     }
  141.     public function setPhoto(Photo $photo): void
  142.     {
  143.         $this->photos->clear();
  144.         $this->photos->add($photo);
  145.     }
  146.     public function getVideo(): ?Video
  147.     {
  148.         return $this->videos->count() ? $this->videos->first() : null;
  149.     }
  150.     public function setVideo(Video $video): void
  151.     {
  152.         $this->videos->clear();
  153.         $this->videos->add($video);
  154.     }
  155.     public function isMediaProcessed(): bool
  156.     {
  157.         if (self::STATUS_PROCESSING === $this->status) {
  158.             return false;
  159.         }
  160.         if (null === $this->getVideo()) {
  161.             return true;
  162.         }
  163.         return null !== $this->getVideo()->getPreviewPath();
  164.     }
  165.     public function isProfileMediaProcessed(): bool
  166.     {
  167.         if (self::STATUS_PROCESSING === $this->status) {
  168.             return false;
  169.         }
  170.         foreach ($this->profile->getVideos() as $video) {
  171.             if (null === $video->getPreviewPath()) {
  172.                 return false;
  173.             }
  174.         }
  175.         return true;
  176.     }
  177.     public function getData(): ?array
  178.     {
  179.         return $this->data;
  180.     }
  181.     public function setData(?array $data): void
  182.     {
  183.         $this->data $data;
  184.     }
  185. }