src/Entity/OnlineShop/SupplierDocument.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Entity\OnlineShop;
  3. use App\Entity\AbstractBase;
  4. use App\Entity\MiniAbstractBase;
  5. use App\Entity\Traits\HasDocumentTrait;
  6. use App\Entity\Traits\NameTrait;
  7. use App\Entity\User;
  8. use App\Enum\DocumentTypeEnum;
  9. use App\Repository\OnlineShop\SupplierDocumentRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Gedmo\Mapping\Annotation as Gedmo;
  14. use Symfony\Component\HttpFoundation\File\File;
  15. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  16. /**
  17.  * @ORM\Table(name="vulco_supplier_document")
  18.  *
  19.  * @ORM\Entity(repositoryClass=SupplierDocumentRepository::class)
  20.  *
  21.  * @ORM\HasLifecycleCallbacks()
  22.  *
  23.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  24.  *
  25.  * @Vich\Uploadable
  26.  */
  27. class SupplierDocument extends AbstractBase
  28. {
  29.     use HasDocumentTrait;
  30.     use NameTrait;
  31.     /**
  32.      * @ORM\Column(type="string", length=255)
  33.      */
  34.     private string $name;
  35.     /**
  36.      * @Vich\UploadableField(mapping="supplier_document", fileNameProperty="documentName")
  37.      */
  38.     private ?File $document null;
  39.     /**
  40.      * @ORM\Column(type="string", length=255, nullable=true)
  41.      */
  42.     private ?string $documentName null;
  43.     /**
  44.      * @ORM\Column(type="integer")
  45.      */
  46.     private int $documentType DocumentTypeEnum::PROMOTION;
  47.     /**
  48.      * @ORM\Column(type="integer", nullable=false, options={"default": 0})
  49.      */
  50.     private int $numDownloads 0;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\SupplierDocumentDownload", mappedBy="document", cascade={"persist", "remove"}, orphanRemoval=true)
  53.      * @ORM\OrderBy({"createdAt": "DESC"})
  54.      */
  55.     private ?Collection $downloads;
  56.     /**
  57.      * @ORM\Column(type="date", nullable=true)
  58.      */
  59.     private ?\DateTimeInterface $startDate null;
  60.     /**
  61.      * @ORM\Column(type="date", nullable=true)
  62.      */
  63.     private ?\DateTimeInterface $endDate null;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\SupplierCategory")
  66.      *
  67.      * @ORM\JoinColumn(name="supplier_category_id", referencedColumnName="id")
  68.      */
  69.     private ?SupplierCategory $category null;
  70.     /**
  71.      * @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Supplier", inversedBy="documents")
  72.      *
  73.      * @ORM\JoinColumn(name="supplier_id", referencedColumnName="id", onDelete="CASCADE")
  74.      */
  75.     private Supplier $supplier;
  76.     public function __construct()
  77.     {
  78.         $this->downloads = new ArrayCollection();
  79.     }
  80.     public function getDocumentType(): int
  81.     {
  82.         return $this->documentType;
  83.     }
  84.     public function getDocumentTypeName(): string
  85.     {
  86.         return array_key_exists($this->documentTypeDocumentTypeEnum::getTranslations()) ? DocumentTypeEnum::getTranslations()[$this->documentType] : MiniAbstractBase::DEFAULT_EMPTY_STRING;
  87.     }
  88.     public function setDocumentType(int $documentType): self
  89.     {
  90.         $this->documentType $documentType;
  91.         return $this;
  92.     }
  93.     public function getNumDownloads(): int
  94.     {
  95.         return $this->numDownloads;
  96.     }
  97.     public function setNumDownloads(int $numDownloads): self
  98.     {
  99.         $this->numDownloads $numDownloads;
  100.         return $this;
  101.     }
  102.     public function getDownloads(): ?Collection
  103.     {
  104.         return $this->downloads;
  105.     }
  106.     public function setDownloads(?Collection $downloads): self
  107.     {
  108.         $this->downloads $downloads;
  109.         return $this;
  110.     }
  111.     public function existsDownload(User $user): bool
  112.     {
  113.         $result false;
  114.         $download = new SupplierDocumentDownload();
  115.         $download->setDocument($this);
  116.         $download->setUser($user);
  117.         $predicate = function ($keySupplierDocumentDownload $element) use ($download) {
  118.             return ($element->getDocument()->getId() === $download->getDocument()->getId()) &&
  119.                 ($element->getUser()->getId() === $download->getUser()->getId());
  120.         };
  121.         if ($this->downloads->exists($predicate)) {
  122.             $result true;
  123.         }
  124.         return $result;
  125.     }
  126.     public function addDownload(SupplierDocumentDownload $download): self
  127.     {
  128.         $predicate = function ($keySupplierDocumentDownload $element) use ($download) {
  129.             return ($element->getDocument()->getId() === $download->getDocument()->getId()) &&
  130.                 ($element->getUser()->getId() === $download->getUser()->getId());
  131.         };
  132.         if (!$this->downloads->exists($predicate)) {
  133.             $this->downloads->add($download);
  134.             $this->setNumDownloads($this->getNumDownloads() + 1);
  135.         }
  136.         return $this;
  137.     }
  138.     public function removeDownload(SupplierDocumentDownload $download): self
  139.     {
  140.         if ($this->downloads->contains($download)) {
  141.             $this->downloads->removeElement($download);
  142.             $this->setNumDownloads($this->getNumDownloads() - 1);
  143.         }
  144.         return $this;
  145.     }
  146.     public function countDownloads(): int
  147.     {
  148.         if (null !== $this->getDownloads()) {
  149.             return count($this->getDownloads());
  150.         }
  151.         return 0;
  152.     }
  153.     public function getStartDate(): ?\DateTimeInterface
  154.     {
  155.         return $this->startDate;
  156.     }
  157.     public function getStartDateAsString(): string
  158.     {
  159.         return $this->getDateAsString($this->getStartDate());
  160.     }
  161.     public function setStartDate(?\DateTimeInterface $startDate): self
  162.     {
  163.         $this->startDate $startDate;
  164.         return $this;
  165.     }
  166.     public function getEndDate(): ?\DateTimeInterface
  167.     {
  168.         return $this->endDate;
  169.     }
  170.     public function getEndDateAsString(): string
  171.     {
  172.         return $this->getDateAsString($this->getEndDate());
  173.     }
  174.     public function setEndDate(?\DateTimeInterface $endDate): self
  175.     {
  176.         $this->endDate $endDate;
  177.         return $this;
  178.     }
  179.     public function getCategory(): ?SupplierCategory
  180.     {
  181.         return $this->category;
  182.     }
  183.     public function setCategory(SupplierCategory $category): self
  184.     {
  185.         $this->category $category;
  186.         return $this;
  187.     }
  188.     public function getSupplier(): Supplier
  189.     {
  190.         return $this->supplier;
  191.     }
  192.     public function setSupplier(Supplier $supplier): self
  193.     {
  194.         $this->supplier $supplier;
  195.         return $this;
  196.     }
  197.     public function isEnabled(): bool
  198.     {
  199.         $now = new \DateTime();
  200.         $now->setTime(00);
  201.         $startDate $this->getStartDate();
  202.         $startDate?->setTime(00);
  203.         $endDate $this->getEndDate();
  204.         $endDate?->setTime(00);
  205.         if ($startDate && $endDate) {
  206.             return $startDate <= $now && $now <= $endDate;
  207.         }
  208.         if ($startDate && !$endDate) {
  209.             return $startDate <= $now;
  210.         }
  211.         if (!$startDate && $endDate) {
  212.             return $now <= $endDate;
  213.         }
  214.         return true;
  215.     }
  216.     public function __toString(): string
  217.     {
  218.         return $this->id $this->getName() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
  219.     }
  220. }