src/App/Entity/Filesystem/LooseFile.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Filesystem;
  3. use App\Entity\Shared\UlidIdentifiableInterface;
  4. use App\Entity\Shared\UlidIdentifiableTrait;
  5. use Cms\CoreBundle\Model\Interfaces\Blameable\BlameableInterface;
  6. use Cms\CoreBundle\Model\Interfaces\Blameable\BlameableTrait;
  7. use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableInterface;
  8. use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableTrait;
  9. use Cms\TenantBundle\Model\TenantableInterface;
  10. use Cms\TenantBundle\Model\TenantableTrait;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Ramsey\Uuid\Uuid;
  13. use Ramsey\Uuid\UuidInterface;
  14. use Reinder83\BinaryFlags\Bits;
  15. /**
  16.  * @ORM\Entity(
  17.  *     repositoryClass = "App\Doctrine\Repository\Filesystem\LooseFileRepository",
  18.  * )
  19.  * @ORM\Table(
  20.  *     name = "sys__filesystem__temp_file",
  21.  * )
  22.  */
  23. class LooseFile
  24.     implements
  25.         UlidIdentifiableInterface,
  26.         TenantableInterface,
  27.         TimestampableInterface,
  28.         BlameableInterface
  29. {
  30.     const MAPPINGS = [
  31.         self::TYPES__IMAGE => [
  32.             'bmp',
  33.             'gif',
  34.             'jpeg',
  35.             'jpg',
  36.             'png',
  37.             'svg',
  38.         ],
  39.         self::TYPES__VIDEO => [
  40.             'avi',
  41.             'mov',
  42.             'mp4',
  43.             'mpg',
  44.             'mpeg',
  45.         ],
  46.         self::TYPES__DOCUMENT => [
  47.             'doc',
  48.             'docx',
  49.             'pdf',
  50.         ],
  51.     ];
  52.     const TYPES__UNKNOWN 0;
  53.     const TYPES__OTHER Bits::BIT_1;
  54.     const TYPES__IMAGE Bits::BIT_2;
  55.     const TYPES__VIDEO Bits::BIT_3;
  56.     const TYPES__DOCUMENT Bits::BIT_4;
  57.     const STATUSES = [
  58.         self::STATUSES__ALLOCATED,
  59.         self::STATUSES__UPLOADED,
  60.         self::STATUSES__PROCESSED,
  61.     ];
  62.     const STATUSES__ALLOCATED 0;
  63.     const STATUSES__UPLOADED Bits::BIT_1;
  64.     const STATUSES__OPTIMIZATIONS__CROP Bits::BIT_2;
  65.     const STATUSES__OPTIMIZATIONS__THUMB Bits::BIT_3;
  66.     const STATUSES__OPTIMIZATIONS__PREVIEW Bits::BIT_4;
  67.     const STATUSES__OPTIMIZED = (0
  68.         self::STATUSES__OPTIMIZATIONS__CROP
  69.         self::STATUSES__OPTIMIZATIONS__THUMB
  70.         self::STATUSES__OPTIMIZATIONS__PREVIEW
  71.     );
  72.     const STATUSES__PROCESSED = (0
  73.         self::STATUSES__UPLOADED
  74.         self::STATUSES__OPTIMIZED
  75.     );
  76.     use UlidIdentifiableTrait;
  77.     use TenantableTrait;
  78.     use TimestampableTrait;
  79.     use BlameableTrait;
  80.     /**
  81.      * @var LooseFolder|null
  82.      *
  83.      * @ORM\ManyToOne(
  84.      *     targetEntity = "App\Entity\Filesystem\LooseFolder",
  85.      *     inversedBy = "files",
  86.      * )
  87.      * @ORM\JoinColumn(
  88.      *     name = "folder",
  89.      *     referencedColumnName = "id",
  90.      *     nullable = false,
  91.      *     onDelete = "CASCADE",
  92.      * )
  93.      */
  94.     protected ?LooseFolder $folder null;
  95.     /**
  96.      * @var int
  97.      *
  98.      * @ORM\Column(
  99.      *     type = "integer",
  100.      *     nullable = false,
  101.      *     options = {
  102.      *         "default" = LooseFile::TYPES__UNKNOWN,
  103.      *     },
  104.      * )
  105.      */
  106.     protected int $type self::TYPES__UNKNOWN;
  107.     /**
  108.      * @var int
  109.      *
  110.      * @ORM\Column(
  111.      *     type = "integer",
  112.      *     nullable = false,
  113.      *     options = {
  114.      *         "default" = LooseFile::STATUSES__ALLOCATED,
  115.      *     },
  116.      * )
  117.      */
  118.     protected int $status self::STATUSES__ALLOCATED;
  119.     /**
  120.      * @var string|null
  121.      *
  122.      * @ORM\Column(
  123.      *     type = "string",
  124.      *     nullable = false,
  125.      * )
  126.      */
  127.     protected ?string $name null;
  128.     /**
  129.      * @var string|null
  130.      *
  131.      * @ORM\Column(
  132.      *     type = "string",
  133.      *     nullable = false,
  134.      * )
  135.      */
  136.     protected ?string $fileName null;
  137.     /**
  138.      * @var string|null
  139.      *
  140.      * @ORM\Column(
  141.      *     type = "string",
  142.      *     nullable = true,
  143.      * )
  144.      */
  145.     protected ?string $fileExtension null;
  146.     /**
  147.      * @var int|null
  148.      *
  149.      * @ORM\Column(
  150.      *     type = "integer",
  151.      *     nullable = true,
  152.      * )
  153.      */
  154.     protected ?int $migrationId null;
  155.     /**
  156.      * @var UuidInterface|null
  157.      *
  158.      * @ORM\Column(
  159.      *     type = "uuid",
  160.      *     nullable = true,
  161.      * )
  162.      */
  163.     protected ?UuidInterface $migrationUid null;
  164.     /**
  165.      * @var string|null
  166.      *
  167.      * @ORM\Column(
  168.      *     type = "string",
  169.      *     nullable = true,
  170.      * )
  171.      */
  172.     protected ?string $remoteJobId null;
  173.     /**
  174.      * @return $this
  175.      */
  176.     public function refresh(): self
  177.     {
  178.         // break apart the name based on the last period and set cache fields
  179.         $pos mb_strrpos($this->getName(), '.');
  180.         $this->fileName mb_substr($this->getName(), 0$pos);
  181.         $this->fileExtension $ext mb_substr($this->getName(), $pos 1) ?: null;
  182.         // convert extension to lower case for type checking
  183.         $ext mb_strtolower($ext);
  184.         // determine the type based on the extension
  185.         // assume other unless something specifically matches
  186.         $this->type self::TYPES__OTHER;
  187.         foreach (self::MAPPINGS as $type => $mappings) {
  188.             if (in_array($ext$mappings)) {
  189.                 $this->type $type;
  190.                 break;
  191.             }
  192.         }
  193.         return $this;
  194.     }
  195.     /**
  196.      * @return int|null
  197.      */
  198.     public function getMigrationId(): ?int
  199.     {
  200.         return $this->migrationId;
  201.     }
  202.     /**
  203.      * @param int|null $migrationId
  204.      * @return $this
  205.      */
  206.     public function setMigrationId(?int $migrationId): self
  207.     {
  208.         $this->migrationId $migrationId;
  209.         return $this;
  210.     }
  211.     /**
  212.      * @return UuidInterface|null
  213.      */
  214.     public function getMigrationUid(): ?UuidInterface
  215.     {
  216.         return $this->migrationUid;
  217.     }
  218.     /**
  219.      * @param string|UuidInterface|null $migrationUid
  220.      * @return $this
  221.      */
  222.     public function setMigrationUid($migrationUid): self
  223.     {
  224.         if ($migrationUid) {
  225.             if (is_string($migrationUid)) {
  226.                 $migrationUid Uuid::fromString($migrationUid);
  227.             }
  228.             if ( ! $migrationUid instanceof UuidInterface) {
  229.                 throw new \Exception();
  230.             }
  231.         }
  232.         $this->migrationUid $migrationUid ?: null;
  233.         return $this;
  234.     }
  235.     /**
  236.      * @return LooseFolder|null
  237.      */
  238.     public function getFolder(): ?LooseFolder
  239.     {
  240.         return $this->folder;
  241.     }
  242.     /**
  243.      * @param LooseFolder $folder
  244.      * @return $this
  245.      */
  246.     public function setFolder(LooseFolder $folder): self
  247.     {
  248.         $this->folder $folder;
  249.         return $this;
  250.     }
  251.     /**
  252.      * @return string|null
  253.      */
  254.     public function getName(): ?string
  255.     {
  256.         return $this->name;
  257.     }
  258.     /**
  259.      * @param string $name
  260.      * @return $this
  261.      */
  262.     public function setName(string $name): self
  263.     {
  264.         $this->name $name;
  265.         return $this->refresh();
  266.     }
  267.     /**
  268.      * @return string|null
  269.      */
  270.     public function getFileName(): ?string
  271.     {
  272.         return $this->fileName;
  273.     }
  274.     /**
  275.      * @return string|null
  276.      */
  277.     public function getFileExtension(): ?string
  278.     {
  279.         return $this->fileExtension;
  280.     }
  281.     /**
  282.      * @return int
  283.      */
  284.     public function getStatus(): int
  285.     {
  286.         return $this->status;
  287.     }
  288.     /**
  289.      * @param int $status
  290.      * @return $this
  291.      */
  292.     public function setStatus(int $status): self
  293.     {
  294.         if ( ! in_array($statusself::STATUSES)) {
  295.             throw new \Exception();
  296.         }
  297.         $this->status $status;
  298.         return $this;
  299.     }
  300.     /**
  301.      * @return int
  302.      */
  303.     public function getType(): int
  304.     {
  305.         return $this->type;
  306.     }
  307.     /**
  308.      * @param int $type
  309.      * @return bool
  310.      */
  311.     public function isType(int $type): bool
  312.     {
  313.         return ($this->getType() === $type);
  314.     }
  315.     /**
  316.      * @return string|null
  317.      */
  318.     public function getRemoteJobId(): ?string
  319.     {
  320.         return $this->remoteJobId;
  321.     }
  322.     /**
  323.      * @param string|null $remoteJobId
  324.      * @return $this
  325.      */
  326.     public function setRemoteJobId(?string $remoteJobId): self
  327.     {
  328.         $this->remoteJobId $remoteJobId;
  329.         return $this;
  330.     }
  331. }