<?php
namespace App\Entity;
use App\Constant\BandTypeConstant;
use App\Constant\EventParticipationConstant;
use App\Constant\FileActionConstant;
use App\Constant\PeopleActionConstant;
use App\Entity\Messages\Model\ParticipantInterface;
use App\Repository\UserRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\User\BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
* @UniqueEntity(fields={"email"}, message="user.email.already_used")
* @UniqueEntity(fields={"username"}, message="user.username.already_used")
*/
class User extends BaseUser implements CommonInformationInterface, PasswordAuthenticatedUserInterface, ParticipantInterface
{
/**
* @ORM\OneToOne(targetEntity="CommonInformation", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private ?CommonInformation $commonInformation = null;
/**
* @var string|null
*
* @ORM\Column(name="first_name", type="string", length=255, nullable=true)
*/
private ?string $firstName = null;
/**
* @var string|null
*
* @ORM\Column(name="last_name", type="string", length=255, nullable=true)
*/
private ?string $lastName = null;
/**
* @var DateTime
*
* @ORM\Column(name="birthday", type="date", nullable=false)
*/
protected DateTime $birthday;
/**
* @var string|null
*
* @ORM\Column(name="influential_artists", type="string", length=255, nullable=true)
*/
private ?string $influentialArtists = null;
/**
* @ORM\ManyToMany(targetEntity="Instrument", cascade={"persist"})
*/
private Collection $instruments;
/**
* @ORM\OneToMany(targetEntity="BandMember", mappedBy="user")
*/
private Collection $bands;
/**
* @ORM\OneToMany(targetEntity="Band", mappedBy="creator")
*/
private Collection $createdBands;
/**
* @ORM\OneToMany(targetEntity="PeopleAction", mappedBy="userPeople", cascade={"remove"})
*/
private Collection $peopleActions;
/**
* @ORM\OneToMany(targetEntity="Action", mappedBy="user", cascade={"remove"})
*/
private Collection $actions;
/**
* @ORM\OneToMany(targetEntity="Folder", mappedBy="creator")
*/
private Collection $folders;
/**
* @ORM\OneToMany(targetEntity="NewMemberSearch", mappedBy="createdBy")
*/
private Collection $newMemberSearches;
/**
* @ORM\OneToMany(targetEntity="Order", mappedBy="user", cascade={"persist"})
*/
private Collection $orders;
/**
* @ORM\OneToMany(targetEntity="Playback", mappedBy="user", cascade={"all"})
*/
private Collection $playbacks;
/**
* @ORM\OneToMany(targetEntity="Visit", mappedBy="user", cascade={"all"})
*/
private Collection $visits;
/**
* @ORM\OneToMany(targetEntity="FileReport", mappedBy="user")
*/
private Collection $reports;
/**
* Get id
*
* @return integer
*/
public function getId(): int
{
return $this->id;
}
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->bands = new ArrayCollection();
$this->peopleActions = new ArrayCollection();
$this->createdBands = new ArrayCollection();
$this->instruments = new ArrayCollection();
$this->actions = new ArrayCollection();
$this->folders = new ArrayCollection();
$this->orders = new ArrayCollection();
$this->playbacks = new ArrayCollection();
$this->visits = new ArrayCollection();
$this->reports = new ArrayCollection();
$this->password = '';
}
/**
* @param Band|null $band
* @param null $role
* @return bool
*/
public function isInBand(?Band $band, $role = null): bool
{
if (!($band instanceof Band)) {
return false;
}
/** @var BandMember $bandMember */
foreach ($band->getMembers() as $bandMember) {
if ($bandMember->getUser() === $this && ($role == null || $bandMember->getRole() == $role)) {
return true;
}
}
return false;
}
/**
* @param Band $band
* @return bool
*/
public function isMemberButNotGuest(Band $band): bool
{
/** @var BandMember $bandMember */
foreach ($band->getMembers(false) as $bandMember) {
if ($bandMember->getUser() === $this) {
return true;
}
}
return false;
}
/**
* @param Band $band
* @return BandMember|null
*/
public function getMember(Band $band): ?BandMember
{
/** @var BandMember $bandMember */
foreach ($band->getMembers() as $bandMember) {
if ($bandMember->getUser()->getId() == $this->getId()) {
return $bandMember;
}
}
return null;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return User
*/
public function setFirstName(string $firstName): User
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string|null
*/
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
*
* @return User
*/
public function setLastName(string $lastName): User
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string|null
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* Set influentialArtists
*
* @param string $influentialArtists
*
* @return User
*/
public function setInfluentialArtists(string $influentialArtists): User
{
$this->influentialArtists = $influentialArtists;
return $this;
}
/**
* Get influentialArtists
*
* @return string|null
*/
public function getInfluentialArtists(): ?string
{
return $this->influentialArtists;
}
/**
* Add instrument
*
* @param Instrument $instrument
*
* @return User
*/
public function addInstrument(Instrument $instrument): User
{
$this->instruments[] = $instrument;
return $this;
}
/**
* Remove instrument
*
* @param Instrument $instrument
*/
public function removeInstrument(Instrument $instrument)
{
$this->instruments->removeElement($instrument);
}
/**
* Get instruments
*
* @return Collection
*/
public function getInstruments()
{
return $this->instruments;
}
/**
* Add band
*
* @param BandMember $band
*
* @return User
*/
public function addBand(BandMember $band): User
{
$this->bands[] = $band;
return $this;
}
/**
* Remove band
*
* @param BandMember $band
*/
public function removeBand(BandMember $band)
{
$this->bands->removeElement($band);
}
/**
* Get bands
*
* @return Collection
*/
public function getBands(): Collection
{
return $this->bands;
}
/**
* Get bands which are not users' collaboration
*
* @return Collection
*/
public function getPublicBands(): Collection
{
return $this->bands->filter(function(BandMember $bandMember) {
return $bandMember->getBand()->getType() !== BandTypeConstant::USER_COLLABORATION;
});
}
/**
* Get bands which are not users' collaboration
*
* @return Collection
*/
public function getPrivateBands(): Collection
{
return $this->bands->filter(function(BandMember $bandMember) {
return $bandMember->getBand()->getType() === BandTypeConstant::USER_COLLABORATION;
});
}
/**
* Add createdBand
*
* @param Band $createdBand
*
* @return User
*/
public function addCreatedBand(Band $createdBand): User
{
$this->createdBands[] = $createdBand;
return $this;
}
/**
* Remove createdBand
*
* @param Band $createdBand
*/
public function removeCreatedBand(Band $createdBand)
{
$this->createdBands->removeElement($createdBand);
}
/**
* Get createdBands
*
* @return Collection
*/
public function getCreatedBands(): Collection
{
return $this->createdBands;
}
/**
* Add folder
*
* @param Folder $folder
*
* @return User
*/
public function addFolder(Folder $folder): User
{
$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(): Collection
{
return $this->folders;
}
/**
* Add PeopleAction
*
* @param PeopleAction $peopleAction
*
* @return User
*/
public function addPeopleAction(PeopleAction $peopleAction): User
{
$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(): Collection
{
return $this->peopleActions;
}
/**
* @return Collection
*/
public function getActions(): Collection
{
return $this->actions;
}
/**
* @param mixed $actions
*/
public function setActions($actions)
{
$this->actions = $actions;
}
public function getSubscriptions(): Collection
{
return $this->actions->filter(
function ($action) {
return $action instanceof PeopleAction && $action->getAction() == PeopleActionConstant::FOLLOW;
}
);
}
public function getSubscribers(): Collection
{
return $this->peopleActions->filter(
function ($action) {
return $action->getAction() == PeopleActionConstant::FOLLOW;
}
);
}
public function getUsersSubscriptions(): Collection
{
return $this->actions->filter(
function ($action) {
return $action instanceof PeopleAction && $action->getAction() == PeopleActionConstant::FOLLOW && $action->getUserPeople();
}
);
}
public function getBandsSubscriptions(): Collection
{
return $this->actions->filter(
function ($action) {
return $action instanceof PeopleAction && $action->getAction() == PeopleActionConstant::FOLLOW && $action->getBandPeople();
}
);
}
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;
}
}
/**
* Add action
*
* @param Action $action
*
* @return User
*/
public function addAction(Action $action): User
{
$this->actions[] = $action;
return $this;
}
/**
* Remove action
*
* @param Action $action
*/
public function removeAction(Action $action)
{
$this->actions->removeElement($action);
}
/**
* Get event participations
*
* @return Collection
*/
public function getEventParticipations(): Collection
{
return $this->actions->filter(
function ($action) {
return $action instanceof EventParticipation;
}
);
}
/**
* Get waiting band invitation
*
* @return Collection
*/
public function getWaitingBandInvitations(): Collection
{
return $this->actions->filter(
function ($action) {
return $action instanceof EventParticipation && $action->getAction() == EventParticipationConstant::INVITE;
}
);
}
public function getLikedFiles(): array
{
$likes = $this->actions->filter(
function ($action) {
return $action instanceof FileAction
&& $action->getAction() == FileActionConstant::LIKE;
}
);
$likedFiles = array();
foreach ($likes as $like) {
$likedFiles[] = $like->getFile();
}
return $likedFiles;
}
/**
* Add order
*
* @param Order $order
*
* @return User
*/
public function addOrder(Order $order): User
{
$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 User
*/
public function setCommonInformation(CommonInformation $commonInformation): User
{
if ($this->commonInformation == null) {
$this->commonInformation = $commonInformation;
}
return $this;
}
/**
* Get common information entity
*
* @return CommonInformation
*/
public function getCommonInformation(): ?CommonInformation
{
return $this->commonInformation;
}
/**
* @return Collection
*/
public function getPlaybacks(): Collection
{
return $this->playbacks;
}
/**
* @param mixed $playbacks
*/
public function setPlaybacks($playbacks)
{
$this->playbacks = $playbacks;
}
/**
* @return Collection
*/
public function getVisits(): Collection
{
return $this->visits;
}
/**
* @param mixed $visits
*/
public function setVisits($visits)
{
$this->visits = $visits;
}
/**
* Set birthday
*
* @param DateTime|null $birthday
*
* @return User
*/
public function setBirthday(?DateTime $birthday): User
{
if ($birthday instanceof DateTime) {
$this->birthday = $birthday;
}
return $this;
}
/**
* Get birthday
*
* @return DateTime|null
*/
public function getBirthday(): ?DateTime
{
return $this->birthday;
}
/**
* Get age
*
* @return integer
*/
public function getAge(): int
{
$TSN = strtotime(date_format($this->birthday, 'Y-m-d'));
$age = date('Y') - date('Y', $TSN);
if (date('md') < date('md', $TSN)) {
return $age - 1;
}
return $age;
}
/**
* @return Collection
*/
public function getReports(): Collection
{
return $this->reports;
}
/**
* @param mixed $reports
*/
public function setReports($reports)
{
$this->reports = $reports;
}
/**
* @param NewMemberSearch $newMemberSearch
*
* @return User
*/
public function addNewMemberSearch(NewMemberSearch $newMemberSearch): User
{
$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;
}
}