<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\Common\Collections\ArrayCollection;
use App\Constant\FileActionConstant;
use App\Constant\FilePlatformConstant;
use App\Constant\FileStatusConstant;
use App\Constant\FileTypeConstant;
/**
* File
*
* @ORM\Table(name="file")
* @ORM\Entity(repositoryClass="App\Repository\FileRepository")
* @ORM\HasLifecycleCallbacks
*/
class File
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var bool
*
* @ORM\Column(name="deleted", type="boolean")
*/
private $deleted = false;
/**
* @ORM\Column(name="type", type="string")
*/
private $type;
/**
* @var Folder $folder
*
* @ORM\ManyToOne(targetEntity="Folder", inversedBy="files")
* @ORM\JoinColumn(nullable=false)
*/
private $folder;
/**
* @ORM\ManyToOne(targetEntity="Project", inversedBy="files")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $project;
/**
* @ORM\Column(name="status", type="string", length=255)
*/
private $status = FileStatusConstant::STATUS_PRIVATE;
/**
* @ORM\OneToMany(targetEntity="FileAction", mappedBy="file", cascade={"remove"})
*/
private $fileActions;
/**
* @ORM\Column(name="extension", type="string", length=255, nullable=true)
*/
private $extension;
/**
* @ORM\Column(name="mime_type", type="string", length=255, nullable=true)
*/
private $mimeType;
/**
* @ORM\Column(name="real_name", type="string", length=255, nullable=true)
*/
private $realName;
/**
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description = '';
private $tempFilename;
/**
* @var UploadedFile $file
*/
private $file;
/**
* @ORM\OneToOne(targetEntity="Playable", cascade={"all"})
* @ORM\JoinColumn(nullable=true)
*/
private $playable;
/**
* @ORM\OneToMany(targetEntity="FileBandMember", mappedBy="file", cascade={"remove"}))
*/
private $bandMembers;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Picture", cascade={"all"})
* @ORM\joinColumn(onDelete="SET NULL")
*/
protected $picture;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255, nullable=true)
*/
private $url;
/**
* Constructor
*/
public function __construct()
{
$this->fileActions = new ArrayCollection();
$this->bandMembers = new ArrayCollection();
}
/**
* get entity to string
*/
public function __toString()
{
return $this->name;
}
public function getFile(): ?UploadedFile
{
return $this->file;
}
public function setFile(UploadedFile $file)
{
$this->file = $file;
if ($this->file->getMimeType()) {
$this->mimeType = $this->file->getMimeType();
} else {
$this->mimeType = $this->file->getClientMimeType();
}
$this->extension = $this->file->getClientOriginalExtension();
$this->createdAt = new DateTime();
$this->realName = uniqid();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move(
$this->getUploadDirectory(),
$this->getFullName()
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFilename = $this->getPath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($this->url == null && file_exists($this->tempFilename)) {
unlink($this->tempFilename);
}
}
public function getUploadDirectory(): string
{
if ($this->getType() == FileTypeConstant::PICTURE){
return 'uploads/pictures/bands/';
}else{
return 'uploads/files/';
}
}
public function getFullName(): string
{
return $this->realName . '.' . $this->extension;
}
public function getPath(): string
{
return $this->getUploadDirectory() . $this->getFullName();
}
/**
* Get id
*
* @return integer
*/
public function getId(): int
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return File
*/
public function setName(string $name): File
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Get file size in Mo, return 0 if not stored on server
*
* @return numeric
*/
public function getSize()
{
if ($this->getUrl() !== null || !file_exists($this->getPath())) {
return 0;
}
return (float)number_format(filesize($this->getPath()) / (1024 * 1024), 2);
}
/**
* Set extension
*
* @param string $extension
*
* @return File
*/
public function setExtension(string $extension): File
{
$this->extension = $extension;
return $this;
}
/**
* Get extension
*
* @return string
*/
public function getExtension(): string
{
return $this->extension;
}
/**
* Set realName
*
* @param string $realName
*
* @return File
*/
public function setRealName(string $realName): File
{
$this->realName = $realName;
return $this;
}
/**
* Get realName
*
* @return string
*/
public function getRealName(): string
{
return $this->realName;
}
/**
* Set description
*
* @param string|null $description
*
* @return File
*/
public function setDescription(?string $description): File
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Get band
*
* @return Band
*/
public function getBand(): Band
{
return $this->folder->getOwner();
}
/**
* Set folder
*
* @param Folder $folder
*
* @return File
*/
public function setFolder(Folder $folder): File
{
$this->folder = $folder;
return $this;
}
/**
* Get folder
*
* @return Folder
*/
public function getFolder(): Folder
{
return $this->folder;
}
/**
* Add fileAction
*
* @param FileAction $fileAction
*
* @return File
*/
public function addFileAction(FileAction $fileAction): File
{
$this->fileActions[] = $fileAction;
return $this;
}
/**
* Remove fileAction
*
* @param FileAction $fileAction
*/
public function removeFileAction(FileAction $fileAction)
{
$this->fileActions->removeElement($fileAction);
}
/**
* Get fileActions
*
* @return Collection
*/
public function getFileActions()
{
return $this->fileActions;
}
/**
* Get file likes
*
* @return Collection
*/
public function getLikes(): Collection
{
return $this->fileActions->filter(
function ($action) {
return $action->getAction() == FileActionConstant::LIKE;
}
);
}
/**
* Get file likes
*
* @return Action|null
*/
public function getPublishAction(): ?Action
{
$action = $this->fileActions->filter(
function ($action) {
return $action->getAction() == FileActionConstant::PUBLISH;
}
)->first();
return ($action instanceof Action) ? $action : null;
}
/**
* Know if specified user like this file
*
* @param User $user
* @return boolean
*/
public function isLikedByUser(User $user): bool
{
$likes = $this->fileActions->filter(
function ($action) use ($user) {
return $action->getAction() === FileActionConstant::LIKE
&& $action->getUser() === $user;
}
);
return (bool)count($likes);
}
/**
* Set playable
*
* @param Playable|null $playable
*
* @return File
*/
public function setPlayable(Playable $playable = null): File
{
$this->playable = $playable;
return $this;
}
/**
* Get playable
*
* @return Playable|null
*/
public function getPlayable(): ?Playable
{
return $this->playable;
}
/**
* Add bandMember
*
* @param FileBandMember $bandMember
*
* @return File
*/
public function addBandMember(FileBandMember $bandMember): File
{
$this->bandMembers[] = $bandMember;
return $this;
}
/**
* Remove bandMember
*
* @param FileBandMember $bandMember
*/
public function removeBandMember(FileBandMember $bandMember)
{
$this->bandMembers->removeElement($bandMember);
}
/**
* Get bandMembers
*
* @return Collection
*/
public function getBandMembers()
{
return $this->bandMembers;
}
/**
* Set picture
*
* @param Picture|null $picture
*
* @return File
*/
public function setPicture(Picture $picture = null): File
{
$this->picture = $picture;
return $this;
}
/**
* Get picture
*
* @return Picture|null
*/
public function getPicture(): ?Picture
{
return $this->picture;
}
/**
* @param boolean $deleted
*/
public function setDeleted(bool $deleted)
{
$this->deleted = $deleted;
}
/**
* Set mime type
*
* @param string $mimeType
*
* @return File
*/
public function setMimeType(string $mimeType): File
{
$this->mimeType = $mimeType;
return $this;
}
/**
* Get mime type
*
* @return string
*/
public function getMimeType(): string
{
return $this->mimeType;
}
/**
* Set status
*
* @param string $status
*
* @return File
*/
public function setStatus(string $status): File
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
/**
* Set status
*
* @param string $type
*
* @return File
*/
public function setType(string $type): File
{
$this->type = $type;
return $this;
}
/**
* Get status
*
* @return string|null
*/
public function getType(): ?string
{
return $this->type;
}
/**
* Get deleted
*
* @return boolean
*/
public function getDeleted(): bool
{
return $this->deleted;
}
/**
* Set createdAt
*
* @param DateTime $createdAt
*
* @return File
*/
public function setCreatedAt(DateTime $createdAt): File
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
/**
* @return User
*/
public function getCreatedBy(): User
{
/** @var FileAction $addAction */
$addAction = $this->fileActions->filter(
function ($action) {
return $action->getAction() == FileActionConstant::ADD;
}
)->first();
return $addAction->getUser();
}
/**
* Set url
*
* @param string $url
*
* @return File
*/
public function setUrl(string $url): File
{
$this->url = $url;
return $this;
}
/**
* Get real url in db field
*
* @return string|null
*/
public function getUrl(): ?string
{
return $this->url;
}
/**
* Get full url
*
* @return string
*/
public function getFullUrl(): string
{
$explodedUrl = explode('#!', $this->url);
if (count($explodedUrl) > 1 && $explodedUrl[0] == FilePlatformConstant::YOUTUBE){
return 'https://www.youtube.com/embed/'.$explodedUrl[1];
}
return $this->url;
}
/**
* Get platform
*
* @return string
*/
public function getPlatform(): ?string
{
$explodedUrl = explode('#!', $this->url);
if (count($explodedUrl) > 1 && $explodedUrl[0] == FilePlatformConstant::YOUTUBE){
return FilePlatformConstant::YOUTUBE;
}
return null;
}
/**
* Know if file is playable
*
* @return boolean
*/
public function isPlayable(): bool
{
return $this->playable !== null;
}
/**
* Set linked project
*
* @param Project|null $project
*
* @return File
*/
public function setProject(?Project $project): File
{
$this->project = $project;
return $this;
}
/**
* Get linked project
*
* @return Project|null
*/
public function getProject(): ?Project
{
return $this->project;
}
/**
* Get instruments played by $bandMembers
*
* @return array
*/
public function getInstruments(): array
{
$instrumentsOnFile = [];
$fileBandMembers = $this->getBandMembers();
/** @var FileBandMember $fileBandMember */
foreach ($fileBandMembers as $fileBandMember) {
$instrumentsOnFile = array_merge($instrumentsOnFile, $fileBandMember->getInstruments()->toArray());
}
sort($instrumentsOnFile);
return $instrumentsOnFile;
}
}