src/Entity/Channel.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\Finder\Finder;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Symfony\Component\Uid\UuidV6;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. /**
  13.  * @ORM\Entity(repositoryClass="App\Repository\ChannelRepository")
  14.  * @ORM\Table(uniqueConstraints={
  15.  *     @ORM\UniqueConstraint(name="slug_idx", columns={"slug"}),
  16.  *     @ORM\UniqueConstraint(name="key_idx", columns={"key"})
  17.  * })
  18.  * @ORM\HasLifecycleCallbacks()
  19.  * @UniqueEntity("slug", errorPath="name", message="This name is already in use")
  20.  * @UniqueEntity("key", errorPath="name", message="This key is already in use")
  21.  */
  22. class Channel
  23. {
  24.     /**
  25.      * @ORM\Id()
  26.      * @ORM\Column(type="uuid", unique=true)
  27.      * @ORM\GeneratedValue(strategy="CUSTOM")
  28.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  29.      * @var UuidV6
  30.      */
  31.     private $id;
  32.     /**
  33.      * @ORM\Column(type="string", length=255)
  34.      * @Assert\NotBlank
  35.      */
  36.     private $key;
  37.     /**
  38.      * @ORM\Column(type="string", length=255)
  39.      */
  40.     private $slug;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      * @Assert\NotBlank
  44.      */
  45.     private $name;
  46.     /**
  47.      * @ORM\Column(type="datetime")
  48.      */
  49.     private $lastUploadAt;
  50.     /**
  51.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="channels")
  52.      */
  53.     private $user;
  54.     /**
  55.      * @var bool
  56.      * @ORM\Column(type="boolean")
  57.      */
  58.     private $archive false;
  59.     public function getUser(): ?User
  60.     {
  61.         return $this->user;
  62.     }
  63.     public function setUser(?User $user): self
  64.     {
  65.         $this->user $user;
  66.         return $this;
  67.     }
  68.     public function __construct()
  69.     {
  70.         $this->lastUploadAt = new \DateTime('now');
  71.     }
  72.     public function getId(): ?UuidV6
  73.     {
  74.         return $this->id;
  75.     }
  76.     public function getKey(): ?string
  77.     {
  78.         return $this->key;
  79.     }
  80.     public function setKey(?string $key): void
  81.     {
  82.         $this->key $key;
  83.     }
  84.     public function getSlug(): ?string
  85.     {
  86.         return $this->slug;
  87.     }
  88.     public function setSlug(?string $slug): void
  89.     {
  90.         $this->slug $slug;
  91.     }
  92.     public function getName(): ?string
  93.     {
  94.         return $this->name;
  95.     }
  96.     public function setName(string $name): self
  97.     {
  98.         $this->name $name;
  99.         $s Slugify::create();
  100.         $this->slug $s->slugify($name);
  101.         return $this;
  102.     }
  103.     public function getDataDirectory($absolute false): string
  104.     {
  105.         $s $this->id->toRfc4122();
  106.         return ($absolute ? (__DIR__.'/../../public') : '').'/data/'.$s[strlen($s)-2].'/'.$s[strlen($s)-1].'/';
  107.     }
  108.     public function getImDirectory($absolute false): string
  109.     {
  110.         return $this->getDataDirectory($absolute).'im/';
  111.     }
  112.     public function ensureDirectory(): void
  113.     {
  114.         $filesystem = new Filesystem();
  115.         if (!@is_dir($this->getDataDirectory(true))) {
  116.             $filesystem->mkdir($this->getDataDirectory(true));
  117.         }
  118.     }
  119.     public function ensureImDirectory(): void
  120.     {
  121.         $filesystem = new Filesystem();
  122.         if (!@is_dir($this->getImDirectory(true))) {
  123.             $filesystem->mkdir($this->getImDirectory(true));
  124.         }
  125.     }
  126.     public function getLastUploadAt(): \DateTime
  127.     {
  128.         return $this->lastUploadAt;
  129.     }
  130.     public function setLastUploadAt(\DateTime $dt): void
  131.     {
  132.         $this->lastUploadAt $dt;
  133.     }
  134.     public function getArchive(): ?bool
  135.     {
  136.         return $this->archive;
  137.     }
  138.     /**
  139.      * @ORM\PreRemove
  140.      */
  141.     public function deleteAllFiles(): void
  142.     {
  143.         $filesystem = new Filesystem();
  144.         $filesystem->remove($this->getDataDirectory(true));
  145.     }
  146.     public function deleteOldFiles(): void
  147.     {
  148.         $days 14;
  149.         $finder = new Finder();
  150.         $files $finder->name("*.webp")->date("< $days days ago")->sortByModifiedTime()->in($this->getDataDirectory(true));
  151.         $filesystem = new Filesystem();
  152.         $filesystem->remove($files);
  153.         $finder = new Finder();
  154.         $files $finder->name("*.jpg")->date("< $days days ago")->sortByModifiedTime()->in($this->getDataDirectory(true));
  155.         $filesystem->remove($files);
  156.     }
  157. }