<?php
namespace App\Entity\Filesystem;
use App\Entity\Shared\UlidIdentifiableInterface;
use App\Entity\Shared\UlidIdentifiableTrait;
use Cms\CoreBundle\Model\Interfaces\Blameable\BlameableInterface;
use Cms\CoreBundle\Model\Interfaces\Blameable\BlameableTrait;
use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableInterface;
use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableTrait;
use Cms\TenantBundle\Model\TenantableInterface;
use Cms\TenantBundle\Model\TenantableTrait;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Reinder83\BinaryFlags\Bits;
/**
* @ORM\Entity(
* repositoryClass = "App\Doctrine\Repository\Filesystem\LooseFileRepository",
* )
* @ORM\Table(
* name = "sys__filesystem__temp_file",
* )
*/
class LooseFile
implements
UlidIdentifiableInterface,
TenantableInterface,
TimestampableInterface,
BlameableInterface
{
const MAPPINGS = [
self::TYPES__IMAGE => [
'bmp',
'gif',
'jpeg',
'jpg',
'png',
'svg',
],
self::TYPES__VIDEO => [
'avi',
'mov',
'mp4',
'mpg',
'mpeg',
],
self::TYPES__DOCUMENT => [
'doc',
'docx',
'pdf',
],
];
const TYPES__UNKNOWN = 0;
const TYPES__OTHER = Bits::BIT_1;
const TYPES__IMAGE = Bits::BIT_2;
const TYPES__VIDEO = Bits::BIT_3;
const TYPES__DOCUMENT = Bits::BIT_4;
const STATUSES = [
self::STATUSES__ALLOCATED,
self::STATUSES__UPLOADED,
self::STATUSES__PROCESSED,
];
const STATUSES__ALLOCATED = 0;
const STATUSES__UPLOADED = Bits::BIT_1;
const STATUSES__OPTIMIZATIONS__CROP = Bits::BIT_2;
const STATUSES__OPTIMIZATIONS__THUMB = Bits::BIT_3;
const STATUSES__OPTIMIZATIONS__PREVIEW = Bits::BIT_4;
const STATUSES__OPTIMIZED = (0
| self::STATUSES__OPTIMIZATIONS__CROP
| self::STATUSES__OPTIMIZATIONS__THUMB
| self::STATUSES__OPTIMIZATIONS__PREVIEW
);
const STATUSES__PROCESSED = (0
| self::STATUSES__UPLOADED
| self::STATUSES__OPTIMIZED
);
use UlidIdentifiableTrait;
use TenantableTrait;
use TimestampableTrait;
use BlameableTrait;
/**
* @var LooseFolder|null
*
* @ORM\ManyToOne(
* targetEntity = "App\Entity\Filesystem\LooseFolder",
* inversedBy = "files",
* )
* @ORM\JoinColumn(
* name = "folder",
* referencedColumnName = "id",
* nullable = false,
* onDelete = "CASCADE",
* )
*/
protected ?LooseFolder $folder = null;
/**
* @var int
*
* @ORM\Column(
* type = "integer",
* nullable = false,
* options = {
* "default" = LooseFile::TYPES__UNKNOWN,
* },
* )
*/
protected int $type = self::TYPES__UNKNOWN;
/**
* @var int
*
* @ORM\Column(
* type = "integer",
* nullable = false,
* options = {
* "default" = LooseFile::STATUSES__ALLOCATED,
* },
* )
*/
protected int $status = self::STATUSES__ALLOCATED;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $name = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $fileName = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $fileExtension = null;
/**
* @var int|null
*
* @ORM\Column(
* type = "integer",
* nullable = true,
* )
*/
protected ?int $migrationId = null;
/**
* @var UuidInterface|null
*
* @ORM\Column(
* type = "uuid",
* nullable = true,
* )
*/
protected ?UuidInterface $migrationUid = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $remoteJobId = null;
/**
* @return $this
*/
public function refresh(): self
{
// break apart the name based on the last period and set cache fields
$pos = mb_strrpos($this->getName(), '.');
$this->fileName = mb_substr($this->getName(), 0, $pos);
$this->fileExtension = $ext = mb_substr($this->getName(), $pos + 1) ?: null;
// convert extension to lower case for type checking
$ext = mb_strtolower($ext);
// determine the type based on the extension
// assume other unless something specifically matches
$this->type = self::TYPES__OTHER;
foreach (self::MAPPINGS as $type => $mappings) {
if (in_array($ext, $mappings)) {
$this->type = $type;
break;
}
}
return $this;
}
/**
* @return int|null
*/
public function getMigrationId(): ?int
{
return $this->migrationId;
}
/**
* @param int|null $migrationId
* @return $this
*/
public function setMigrationId(?int $migrationId): self
{
$this->migrationId = $migrationId;
return $this;
}
/**
* @return UuidInterface|null
*/
public function getMigrationUid(): ?UuidInterface
{
return $this->migrationUid;
}
/**
* @param string|UuidInterface|null $migrationUid
* @return $this
*/
public function setMigrationUid($migrationUid): self
{
if ($migrationUid) {
if (is_string($migrationUid)) {
$migrationUid = Uuid::fromString($migrationUid);
}
if ( ! $migrationUid instanceof UuidInterface) {
throw new \Exception();
}
}
$this->migrationUid = $migrationUid ?: null;
return $this;
}
/**
* @return LooseFolder|null
*/
public function getFolder(): ?LooseFolder
{
return $this->folder;
}
/**
* @param LooseFolder $folder
* @return $this
*/
public function setFolder(LooseFolder $folder): self
{
$this->folder = $folder;
return $this;
}
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this->refresh();
}
/**
* @return string|null
*/
public function getFileName(): ?string
{
return $this->fileName;
}
/**
* @return string|null
*/
public function getFileExtension(): ?string
{
return $this->fileExtension;
}
/**
* @return int
*/
public function getStatus(): int
{
return $this->status;
}
/**
* @param int $status
* @return $this
*/
public function setStatus(int $status): self
{
if ( ! in_array($status, self::STATUSES)) {
throw new \Exception();
}
$this->status = $status;
return $this;
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param int $type
* @return bool
*/
public function isType(int $type): bool
{
return ($this->getType() === $type);
}
/**
* @return string|null
*/
public function getRemoteJobId(): ?string
{
return $this->remoteJobId;
}
/**
* @param string|null $remoteJobId
* @return $this
*/
public function setRemoteJobId(?string $remoteJobId): self
{
$this->remoteJobId = $remoteJobId;
return $this;
}
}