<?php
namespace App\Entity;
use App\Repository\BeatRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use finfo;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: BeatRepository::class)]
#[Vich\Uploadable]
class Beat
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[Vich\UploadableField(mapping: 'beats_beat', fileNameProperty: 'instrumentalFileName', size: 'instrumentalSize')]
public ?File $instrumentalFile = null;
#[ORM\Column(nullable: true)]
private ?string $instrumentalFileName = null;
#[ORM\Column(nullable: true)]
private ?int $instrumentalSize = null;
#[Vich\UploadableField(mapping: 'beats_image', fileNameProperty: 'imageName', size: 'imageSize')]
public ?File $imageFile = null;
#[ORM\Column(nullable: true)]
private ?string $imageName = null;
#[ORM\Column(nullable: true)]
private ?int $imageSize = null;
#[ORM\Column(nullable: true)]
private ?\DateTime $updatedAt = null;
#[ORM\ManyToMany(targetEntity: BeatCategory::class, inversedBy: 'beats')]
private Collection $category;
#[ORM\ManyToOne(inversedBy: 'beats')]
private ?BeatAuthor $author = null;
#[ORM\OneToMany(mappedBy: 'beat', targetEntity: Battle::class)]
private Collection $battles;
#[ORM\Column(nullable: true)]
private ?\DateTime $beatLength = null;
#[ORM\Column(nullable: true)]
private ?\DateTime $createdAt = null;
#[ORM\ManyToMany(targetEntity: Client::class, mappedBy: 'favoriteBeats')]
private Collection $favoritedClients;
public function __toString()
{
return (string)$this->getName();
}
public function __construct()
{
$this->category = new ArrayCollection();
$this->battles = new ArrayCollection();
$this->favoritedClients = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function setInstrumentalFile(?File $instrumentalFile = null): void
{
$this->instrumentalFile = $instrumentalFile;
if (null !== $instrumentalFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime();
}
}
public function getInstrumentalFile(): ?File
{
return $this->instrumentalFile;
}
public function setInstrumentalFileName(?string $instrumentalFileName): void
{
$this->instrumentalFileName = $instrumentalFileName;
}
public function getInstrumentalFileName(): ?string
{
return $this->instrumentalFileName;
}
public function setInstrumentalSize(?int $instrumentalSize): void
{
$this->instrumentalSize = $instrumentalSize;
}
public function getInstrumentalSize(): ?int
{
return $this->instrumentalSize;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
/**
* @return Collection<int, BeatCategory>
*/
public function getCategory(): Collection
{
return $this->category;
}
public function addCategory(BeatCategory $category): static
{
if (!$this->category->contains($category)) {
$this->category->add($category);
}
return $this;
}
public function removeCategory(BeatCategory $category): static
{
$this->category->removeElement($category);
return $this;
}
public function getAuthor(): ?BeatAuthor
{
return $this->author;
}
public function setAuthor(?BeatAuthor $author): static
{
$this->author = $author;
return $this;
}
/**
* @return Collection<int, Battle>
*/
public function getBattles(): Collection
{
return $this->battles;
}
public function addBattle(Battle $battle): static
{
if (!$this->battles->contains($battle)) {
$this->battles->add($battle);
$battle->setBeat($this);
}
return $this;
}
public function removeBattle(Battle $battle): static
{
if ($this->battles->removeElement($battle)) {
// set the owning side to null (unless already changed)
if ($battle->getBeat() === $this) {
$battle->setBeat(null);
}
}
return $this;
}
public function getBeatLength(): ?\DateTimeInterface
{
return $this->beatLength;
}
public function setBeatLength(?\DateTimeInterface $beatLength): static
{
$this->beatLength = $beatLength;
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTime $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection<int, Client>
*/
public function getFavoritedClients(): Collection
{
return $this->favoritedClients;
}
public function addFavoritedClient(Client $favoritedClient): static
{
if (!$this->favoritedClients->contains($favoritedClient)) {
$this->favoritedClients->add($favoritedClient);
$favoritedClient->addFavoriteBeat($this);
}
return $this;
}
public function removeFavoritedClient(Client $favoritedClient): static
{
if ($this->favoritedClients->removeElement($favoritedClient)) {
$favoritedClient->removeFavoriteBeat($this);
}
return $this;
}
// /**
// * @Vich\UploadableField\PreUpload
// */
// public function validateAudioFile(File $audioFile): void
// {
//
// $finfo = new Finfo(FILEINFO_MIME_TYPE);
// $mimeType = $finfo->file($audioFile->getRealPath());
//
// if (in_array($mimeType, ['audio/mpeg', 'audio/wav', 'audio/ogg'])) {
// $allowedDuration = 60; // 1 minute in seconds
// $fileInfo = ffmpeg_probe($audioFile->getRealPath());
// $duration = $fileInfo['format']['duration'];
//
// if ($duration > $allowedDuration) {
// throw new \Exception("Audio file cannot be longer than 1 minute.");
// }
// }
//
// }
}