<?php
namespace App\Entity;
use App\Constant\BandMemberRoleConstant;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use App\Constant\ActionStatusConstant;
use App\Constant\InvitationStatusConstant;
use App\Constant\PeopleActionConstant;
use App\Constant\BandTypeConstant;
use App\Constant\FileStatusConstant;
use App\Constant\FileTypeConstant;
use DateTime;
/**
* Band
*
* @ORM\Table(name="band")
* @ORM\Entity(repositoryClass="App\Repository\BandRepository")
*/
class Band implements CommonInformationInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected int $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
protected string $name;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private string $type = BandTypeConstant::USER_COLLABORATION;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="createdBands")
* @ORM\JoinColumn(nullable=false)
*/
private User $creator;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255)
*/
private string $city = '';
/**
* @ORM\OneToMany(targetEntity="BandMember", mappedBy="band", cascade={"all"})
*/
private Collection $members;
/**
* @ORM\OneToMany(targetEntity="NewMemberSearch", mappedBy="band", cascade={"all"})
*/
private Collection $newMemberSearches;
/**
* @ORM\OneToMany(targetEntity="PeopleAction", mappedBy="bandPeople", cascade={"remove"})
*/
private Collection $peopleActions;
/**
* @ORM\OneToMany(targetEntity="Folder", mappedBy="owner", cascade={"remove"}))
*/
private Collection $folders;
/**
* @ORM\OneToMany(targetEntity="EventAction", mappedBy="band", cascade={"remove"})
*/
private Collection $eventActions;
/**
* @ORM\OneToMany(targetEntity="Invitation", mappedBy="band", cascade={"all"})
*/
private Collection $invitations;
/**
* @ORM\OneToMany(targetEntity="Thread", mappedBy="band", cascade={"all"})
*/
private Collection $threads;
/**
* @ORM\OneToOne(targetEntity="CommonInformation", cascade={"all"})c
* @ORM\JoinColumn(nullable=false)
*/
private ?CommonInformation $commonInformation = null;
/**
* @ORM\OneToMany(targetEntity="Order", mappedBy="band", cascade={"persist"})
*/
private Collection $orders;
/**
* @ORM\OneToMany(targetEntity="Project", mappedBy="band")
*/
private Collection $projects;
/**
* Constructor
*/
public function __construct()
{
$this->members = new ArrayCollection();
$this->peopleActions = new ArrayCollection();
$this->folders = new ArrayCollection();
$this->eventActions = new ArrayCollection();
$this->projects = new ArrayCollection();
$this->newMemberSearches = new ArrayCollection();
$this->invitations = new ArrayCollection();
$this->threads = new ArrayCollection();
$this->orders = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId(): int
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Band
*/
public function setName(string $name): Band
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Set city
*
* @param string $city
*
* @return Band
*/
public function setCity(string $city): Band
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* Set creator
*
* @param User $creator
*
* @return Band
*/
public function setCreator(User $creator): Band
{
$this->creator = $creator;
return $this;
}
/**
* Get creator
*
* @return User
*/
public function getCreator(): User
{
return $this->creator;
}
/**
* Add member
*
* @param BandMember $member
*
* @return Band
*/
public function addMember(BandMember $member): Band
{
$member->setBand($this);
$this->members[] = $member;
return $this;
}
/**
* Remove member
*
* @param BandMember $member
*/
public function removeMember(BandMember $member)
{
$this->members->removeElement($member);
}
/**
* Get members
*
* @return Collection
*/
public function getMembers($withGuest = true)
{
return $withGuest ? $this->members : $this->members->filter(function(BandMember $bandMember) {
return $bandMember->getRole() !== BandMemberRoleConstant::GUEST;
});
}
/**
* Add folder
*
* @param Folder $folder
*
* @return Band
*/
public function addFolder(Folder $folder): Band
{
$this->folders[] = $folder;
return $this;
}
/**
* Remove folder
*
* @param Folder $folder
*/
public function removeFolder(Folder $folder)
{
$this->folders->removeElement($folder);
}
/**
* Get folders
*
* @return Collection
*/
public function getFolders()
{
return $this->folders;
}
public function getAllFiles(): array
{
$allFiles = array();
foreach ($this->folders as $folder) {
foreach ($folder->getFiles() as $file) {
$allFiles[] = $file;
}
}
return $allFiles;
}
public function getAllPublicFiles($type): array
{
$publicFiles = array();
foreach ($this->getAllFiles() as $file) {
if ($file->getStatus() == FileStatusConstant::STATUS_PUBLIC && $file->getType() == $type) {
$publicFiles[] = $file;
}
}
return $publicFiles;
}
public function getWaitingInvitations(): array
{
$acceptInvite = array();
foreach ($this->getInvitations() as $invitation) {
if ($invitation->getStatus() == InvitationStatusConstant::WAITING) {
$acceptInvite[] = $invitation;
}
}
return $acceptInvite;
}
public function getBaseFolders(): array
{
$baseFolders = array();
foreach ($this->folders as $folder) {
if (!$folder->getParent()) {
$baseFolders[] = $folder;
}
}
return $baseFolders;
}
public function getBaseFolder($fileType = FileTypeConstant::MUSIC)
{
$baseFolder = null;
foreach ($this->folders as $folder) {
if (!$folder->getParent() && $folder->getFilesType() == $fileType && (!$baseFolder || ($folder->getId() < $baseFolder->getId()))) {
$baseFolder = $folder;
}
}
return $baseFolder;
}
/**
* @return mixed|null
*/
public function getCurrentOrder()
{
$currentOrders = $this->orders->filter(
function ($order) {
/** @var Order $order */
return $order->getEndDate() > new DateTime();
}
);
if (count($currentOrders)) {
return $currentOrders->first();
} else {
return null;
}
}
/**
* Set type
*
* @param string $type
*
* @return Band
*/
public function setType(string $type): Band
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* Add PeopleAction
*
* @param PeopleAction $PeopleAction
*
* @return Band
*/
public function addPeopleAction(PeopleAction $PeopleAction): Band
{
$this->peopleActions[] = $PeopleAction;
return $this;
}
/**
* Remove PeopleAction
*
* @param PeopleAction $PeopleAction
*/
public function removePeopleAction(PeopleAction $PeopleAction)
{
$this->peopleActions->removeElement($PeopleAction);
}
/**
* Get PeopleActions
*
* @return Collection
*/
public function getPeopleActions()
{
return $this->peopleActions;
}
public function getSubscribers(): Collection
{
return $this->peopleActions->filter(
function ($action) {
return $action->getAction() == PeopleActionConstant::FOLLOW;
}
);
}
/**
* Add eventAction
*
* @param EventAction $eventAction
*
* @return Band
*/
public function addEventAction(EventAction $eventAction): Band
{
$this->eventActions[] = $eventAction;
return $this;
}
/**
* Remove eventAction
*
* @param EventAction $eventAction
*/
public function removeEventAction(EventAction $eventAction)
{
$this->eventActions->removeElement($eventAction);
}
/**
* Get eventActions
*
* @return Collection
*/
public function getEventActions()
{
return $this->eventActions;
}
public function getPublicEvents(): Collection
{
return $this->eventActions->filter(
function ($action) {
return $action->getStatus() == ActionStatusConstant::STATUS_PUBLIC;
}
);
}
/**
* Add invitation
*
* @param Invitation $invitation
*
* @return Band
*/
public function addInvitation(Invitation $invitation): Band
{
$this->invitations[] = $invitation;
return $this;
}
/**
* Remove invitation
*
* @param Invitation $invitation
*/
public function removeInvitation(Invitation $invitation)
{
$this->invitations->removeElement($invitation);
}
/**
* Get invitations
*
* @return Collection
*/
public function getInvitations()
{
return $this->invitations;
}
/**
* Add thread
*
* @param Thread $thread
*
* @return Band
*/
public function addThread(Thread $thread): Band
{
$this->threads[] = $thread;
return $this;
}
/**
* Remove thread
*
* @param Thread $thread
*/
public function removeThread(Thread $thread)
{
$this->threads->removeElement($thread);
}
/**
* Get threads
*
* @return Collection
*/
public function getThreads()
{
return $this->threads;
}
/**
* Get no deleted files
*
* @return Collection
*/
public function getFiles()
{
$files = new ArrayCollection();
foreach ($this->folders as $folder) {
foreach ($folder->getFiles() as $file) {
if (!$file->getDeleted()) {
$files->add($file);
}
}
}
return $files;
}
/**
* Get no deleted files
*
* @return int
*/
public function getTotalPlaybackNumber(): int
{
$files = $this->getFiles();
$totalPlaybackCount = 0;
foreach ($files as $file) {
if ($file->getPlayable()) {
$totalPlaybackCount += count($file->getPlayable()->getPlaybacks());
}
}
return $totalPlaybackCount;
}
/**
* Add order
*
* @param Order $order
*
* @return Band
*/
public function addOrder(Order $order): Band
{
$this->orders[] = $order;
return $this;
}
/**
* Remove order
*
* @param Order $order
*/
public function removeOrder(Order $order)
{
$this->orders->removeElement($order);
}
/**
* Get orders
*
* @return Collection
*/
public function getOrders(): Collection
{
return $this->orders;
}
/**
* Set common information entity
*
* @param CommonInformation $commonInformation
*
* @return Band
*/
public function setCommonInformation(CommonInformation $commonInformation): Band
{
if ($this->commonInformation == null) {
$this->commonInformation = $commonInformation;
}
return $this;
}
/**
* Get common information entity
*
* @return CommonInformation|null
*/
public function getCommonInformation(): ?CommonInformation
{
return $this->commonInformation;
}
/**
* @param NewMemberSearch $newMemberSearch
*
* @return Band
*/
public function addNewMemberSearch(NewMemberSearch $newMemberSearch): Band
{
$this->newMemberSearches[] = $newMemberSearch;
return $this;
}
/**
* @param NewMemberSearch $newMemberSearch
*/
public function removeNewMemberSearch(NewMemberSearch $newMemberSearch)
{
$this->newMemberSearches->removeElement($newMemberSearch);
}
/**
* @return Collection
*/
public function getNewMemberSearches(): Collection
{
return $this->newMemberSearches;
}
/**
* @return Collection
*/
public function getActiveNewMemberSearches(): Collection
{
return $this->newMemberSearches->filter(function($newMemberSearch) {
return !$newMemberSearch->getDeleted();
});
}
/**
* Add project
*
* @param Project $project
*
* @return Band
*/
public function addProject(Project $project): self
{
$this->projects[] = $project;
return $this;
}
/**
* Remove project
*
* @param Project $project
*/
public function removeProject(Project $project)
{
$this->projects->removeElement($project);
}
/**
* Get projects
*
* @return Collection
*/
public function getProjects(): Collection
{
return $this->projects;
}
}