src/Entity/User.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Constant\BandTypeConstant;
  4. use App\Constant\EventParticipationConstant;
  5. use App\Constant\FileActionConstant;
  6. use App\Constant\PeopleActionConstant;
  7. use App\Entity\Messages\Model\ParticipantInterface;
  8. use App\Repository\UserRepository;
  9. use DateTime;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use App\Entity\User\BaseUser;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  16. /**
  17.  * @ORM\Entity(repositoryClass=UserRepository::class)
  18.  * @ORM\Table(name="`user`")
  19.  * @UniqueEntity(fields={"email"}, message="user.email.already_used")
  20.  * @UniqueEntity(fields={"username"}, message="user.username.already_used")
  21.  */
  22. class User extends BaseUser implements CommonInformationInterfacePasswordAuthenticatedUserInterfaceParticipantInterface
  23. {
  24.     /**
  25.      * @ORM\OneToOne(targetEntity="CommonInformation", cascade={"persist"})
  26.      * @ORM\JoinColumn(nullable=false)
  27.      */
  28.     private ?CommonInformation $commonInformation null;
  29.     /**
  30.      * @var string|null
  31.      *
  32.      * @ORM\Column(name="first_name", type="string", length=255, nullable=true)
  33.      */
  34.     private ?string $firstName null;
  35.     /**
  36.      * @var string|null
  37.      *
  38.      * @ORM\Column(name="last_name", type="string", length=255, nullable=true)
  39.      */
  40.     private ?string $lastName null;
  41.     /**
  42.      * @var DateTime
  43.      *
  44.      * @ORM\Column(name="birthday", type="date", nullable=false)
  45.      */
  46.     protected DateTime $birthday;
  47.     /**
  48.      * @var string|null
  49.      *
  50.      * @ORM\Column(name="influential_artists", type="string", length=255, nullable=true)
  51.      */
  52.     private ?string $influentialArtists null;
  53.     /**
  54.      * @ORM\ManyToMany(targetEntity="Instrument", cascade={"persist"})
  55.      */
  56.     private Collection $instruments;
  57.     /**
  58.      * @ORM\OneToMany(targetEntity="BandMember", mappedBy="user")
  59.      */
  60.     private Collection $bands;
  61.     /**
  62.      * @ORM\OneToMany(targetEntity="Band", mappedBy="creator")
  63.      */
  64.     private Collection $createdBands;
  65.     /**
  66.      * @ORM\OneToMany(targetEntity="PeopleAction", mappedBy="userPeople", cascade={"remove"})
  67.      */
  68.     private Collection $peopleActions;
  69.     /**
  70.      * @ORM\OneToMany(targetEntity="Action", mappedBy="user", cascade={"remove"})
  71.      */
  72.     private Collection $actions;
  73.     /**
  74.      * @ORM\OneToMany(targetEntity="Folder", mappedBy="creator")
  75.      */
  76.     private Collection $folders;
  77.     /**
  78.      * @ORM\OneToMany(targetEntity="NewMemberSearch", mappedBy="createdBy")
  79.      */
  80.     private Collection $newMemberSearches;
  81.     /**
  82.      * @ORM\OneToMany(targetEntity="Order", mappedBy="user", cascade={"persist"})
  83.      */
  84.     private Collection $orders;
  85.     /**
  86.      * @ORM\OneToMany(targetEntity="Playback", mappedBy="user", cascade={"all"})
  87.      */
  88.     private Collection $playbacks;
  89.     /**
  90.      * @ORM\OneToMany(targetEntity="Visit", mappedBy="user", cascade={"all"})
  91.      */
  92.     private Collection $visits;
  93.     /**
  94.      * @ORM\OneToMany(targetEntity="FileReport", mappedBy="user")
  95.      */
  96.     private Collection $reports;
  97.     /**
  98.      * Get id
  99.      *
  100.      * @return integer
  101.      */
  102.     public function getId(): int
  103.     {
  104.         return $this->id;
  105.     }
  106.     /**
  107.      * Constructor
  108.      */
  109.     public function __construct()
  110.     {
  111.         parent::__construct();
  112.         $this->bands = new ArrayCollection();
  113.         $this->peopleActions = new ArrayCollection();
  114.         $this->createdBands = new ArrayCollection();
  115.         $this->instruments = new ArrayCollection();
  116.         $this->actions = new ArrayCollection();
  117.         $this->folders = new ArrayCollection();
  118.         $this->orders = new ArrayCollection();
  119.         $this->playbacks = new ArrayCollection();
  120.         $this->visits = new ArrayCollection();
  121.         $this->reports = new ArrayCollection();
  122.         $this->password '';
  123.     }
  124.     /**
  125.      * @param Band|null $band
  126.      * @param null $role
  127.      * @return bool
  128.      */
  129.     public function isInBand(?Band $band$role null): bool
  130.     {
  131.         if (!($band instanceof Band)) {
  132.             return false;
  133.         }
  134.         /** @var BandMember $bandMember */
  135.         foreach ($band->getMembers() as $bandMember) {
  136.             if ($bandMember->getUser() === $this && ($role == null || $bandMember->getRole() == $role)) {
  137.                 return true;
  138.             }
  139.         }
  140.         return false;
  141.     }
  142.     /**
  143.      * @param Band $band
  144.      * @return bool
  145.      */
  146.     public function isMemberButNotGuest(Band $band): bool
  147.     {
  148.         /** @var BandMember $bandMember */
  149.         foreach ($band->getMembers(false) as $bandMember) {
  150.             if ($bandMember->getUser() === $this) {
  151.                 return true;
  152.             }
  153.         }
  154.         return false;
  155.     }
  156.     /**
  157.      * @param Band $band
  158.      * @return BandMember|null
  159.      */
  160.     public function getMember(Band $band): ?BandMember
  161.     {
  162.         /** @var BandMember $bandMember */
  163.         foreach ($band->getMembers() as $bandMember) {
  164.             if ($bandMember->getUser()->getId() == $this->getId()) {
  165.                 return $bandMember;
  166.             }
  167.         }
  168.         return null;
  169.     }
  170.     /**
  171.      * Set firstName
  172.      *
  173.      * @param string $firstName
  174.      *
  175.      * @return User
  176.      */
  177.     public function setFirstName(string $firstName): User
  178.     {
  179.         $this->firstName $firstName;
  180.         return $this;
  181.     }
  182.     /**
  183.      * Get firstName
  184.      *
  185.      * @return string|null
  186.      */
  187.     public function getFirstName(): ?string
  188.     {
  189.         return $this->firstName;
  190.     }
  191.     /**
  192.      * Set lastName
  193.      *
  194.      * @param string $lastName
  195.      *
  196.      * @return User
  197.      */
  198.     public function setLastName(string $lastName): User
  199.     {
  200.         $this->lastName $lastName;
  201.         return $this;
  202.     }
  203.     /**
  204.      * Get lastName
  205.      *
  206.      * @return string|null
  207.      */
  208.     public function getLastName(): ?string
  209.     {
  210.         return $this->lastName;
  211.     }
  212.     /**
  213.      * Set influentialArtists
  214.      *
  215.      * @param string $influentialArtists
  216.      *
  217.      * @return User
  218.      */
  219.     public function setInfluentialArtists(string $influentialArtists): User
  220.     {
  221.         $this->influentialArtists $influentialArtists;
  222.         return $this;
  223.     }
  224.     /**
  225.      * Get influentialArtists
  226.      *
  227.      * @return string|null
  228.      */
  229.     public function getInfluentialArtists(): ?string
  230.     {
  231.         return $this->influentialArtists;
  232.     }
  233.     /**
  234.      * Add instrument
  235.      *
  236.      * @param Instrument $instrument
  237.      *
  238.      * @return User
  239.      */
  240.     public function addInstrument(Instrument $instrument): User
  241.     {
  242.         $this->instruments[] = $instrument;
  243.         return $this;
  244.     }
  245.     /**
  246.      * Remove instrument
  247.      *
  248.      * @param Instrument $instrument
  249.      */
  250.     public function removeInstrument(Instrument $instrument)
  251.     {
  252.         $this->instruments->removeElement($instrument);
  253.     }
  254.     /**
  255.      * Get instruments
  256.      *
  257.      * @return Collection
  258.      */
  259.     public function getInstruments()
  260.     {
  261.         return $this->instruments;
  262.     }
  263.     /**
  264.      * Add band
  265.      *
  266.      * @param BandMember $band
  267.      *
  268.      * @return User
  269.      */
  270.     public function addBand(BandMember $band): User
  271.     {
  272.         $this->bands[] = $band;
  273.         return $this;
  274.     }
  275.     /**
  276.      * Remove band
  277.      *
  278.      * @param BandMember $band
  279.      */
  280.     public function removeBand(BandMember $band)
  281.     {
  282.         $this->bands->removeElement($band);
  283.     }
  284.     /**
  285.      * Get bands
  286.      *
  287.      * @return Collection
  288.      */
  289.     public function getBands(): Collection
  290.     {
  291.         return $this->bands;
  292.     }
  293.     /**
  294.      * Get bands which are not users' collaboration
  295.      *
  296.      * @return Collection
  297.      */
  298.     public function getPublicBands(): Collection
  299.     {
  300.         return $this->bands->filter(function(BandMember $bandMember) {
  301.             return $bandMember->getBand()->getType() !== BandTypeConstant::USER_COLLABORATION;
  302.         });
  303.     }
  304.     /**
  305.      * Get bands which are not users' collaboration
  306.      *
  307.      * @return Collection
  308.      */
  309.     public function getPrivateBands(): Collection
  310.     {
  311.         return $this->bands->filter(function(BandMember $bandMember) {
  312.             return $bandMember->getBand()->getType() === BandTypeConstant::USER_COLLABORATION;
  313.         });
  314.     }
  315.     /**
  316.      * Add createdBand
  317.      *
  318.      * @param Band $createdBand
  319.      *
  320.      * @return User
  321.      */
  322.     public function addCreatedBand(Band $createdBand): User
  323.     {
  324.         $this->createdBands[] = $createdBand;
  325.         return $this;
  326.     }
  327.     /**
  328.      * Remove createdBand
  329.      *
  330.      * @param Band $createdBand
  331.      */
  332.     public function removeCreatedBand(Band $createdBand)
  333.     {
  334.         $this->createdBands->removeElement($createdBand);
  335.     }
  336.     /**
  337.      * Get createdBands
  338.      *
  339.      * @return Collection
  340.      */
  341.     public function getCreatedBands(): Collection
  342.     {
  343.         return $this->createdBands;
  344.     }
  345.     /**
  346.      * Add folder
  347.      *
  348.      * @param Folder $folder
  349.      *
  350.      * @return User
  351.      */
  352.     public function addFolder(Folder $folder): User
  353.     {
  354.         $this->folders[] = $folder;
  355.         return $this;
  356.     }
  357.     /**
  358.      * Remove folder
  359.      *
  360.      * @param Folder $folder
  361.      */
  362.     public function removeFolder(Folder $folder)
  363.     {
  364.         $this->folders->removeElement($folder);
  365.     }
  366.     /**
  367.      * Get folders
  368.      *
  369.      * @return Collection
  370.      */
  371.     public function getFolders(): Collection
  372.     {
  373.         return $this->folders;
  374.     }
  375.     /**
  376.      * Add PeopleAction
  377.      *
  378.      * @param PeopleAction $peopleAction
  379.      *
  380.      * @return User
  381.      */
  382.     public function addPeopleAction(PeopleAction $peopleAction): User
  383.     {
  384.         $this->peopleActions[] = $peopleAction;
  385.         return $this;
  386.     }
  387.     /**
  388.      * Remove PeopleAction
  389.      *
  390.      * @param PeopleAction $peopleAction
  391.      */
  392.     public function removePeopleAction(PeopleAction $peopleAction)
  393.     {
  394.         $this->peopleActions->removeElement($peopleAction);
  395.     }
  396.     /**
  397.      * Get PeopleActions
  398.      *
  399.      * @return Collection
  400.      */
  401.     public function getPeopleActions(): Collection
  402.     {
  403.         return $this->peopleActions;
  404.     }
  405.     /**
  406.      * @return Collection
  407.      */
  408.     public function getActions(): Collection
  409.     {
  410.         return $this->actions;
  411.     }
  412.     /**
  413.      * @param mixed $actions
  414.      */
  415.     public function setActions($actions)
  416.     {
  417.         $this->actions $actions;
  418.     }
  419.     public function getSubscriptions(): Collection
  420.     {
  421.         return $this->actions->filter(
  422.             function ($action) {
  423.                 return $action instanceof PeopleAction && $action->getAction() == PeopleActionConstant::FOLLOW;
  424.             }
  425.         );
  426.     }
  427.     public function getSubscribers(): Collection
  428.     {
  429.         return $this->peopleActions->filter(
  430.             function ($action) {
  431.                 return $action->getAction() == PeopleActionConstant::FOLLOW;
  432.             }
  433.         );
  434.     }
  435.     public function getUsersSubscriptions(): Collection
  436.     {
  437.         return $this->actions->filter(
  438.             function ($action) {
  439.                 return $action instanceof PeopleAction && $action->getAction() == PeopleActionConstant::FOLLOW && $action->getUserPeople();
  440.             }
  441.         );
  442.     }
  443.     public function getBandsSubscriptions(): Collection
  444.     {
  445.         return $this->actions->filter(
  446.             function ($action) {
  447.                 return $action instanceof PeopleAction && $action->getAction() == PeopleActionConstant::FOLLOW && $action->getBandPeople();
  448.             }
  449.         );
  450.     }
  451.     public function getCurrentOrder()
  452.     {
  453.         $currentOrders $this->orders->filter(
  454.             function ($order) {
  455.                 /** @var Order $order */
  456.                 return $order->getEndDate() > new DateTime();
  457.             }
  458.         );
  459.         if (count($currentOrders)) {
  460.             return $currentOrders->first();
  461.         } else {
  462.             return null;
  463.         }
  464.     }
  465.     /**
  466.      * Add action
  467.      *
  468.      * @param Action $action
  469.      *
  470.      * @return User
  471.      */
  472.     public function addAction(Action $action): User
  473.     {
  474.         $this->actions[] = $action;
  475.         return $this;
  476.     }
  477.     /**
  478.      * Remove action
  479.      *
  480.      * @param Action $action
  481.      */
  482.     public function removeAction(Action $action)
  483.     {
  484.         $this->actions->removeElement($action);
  485.     }
  486.     /**
  487.      * Get event participations
  488.      *
  489.      * @return Collection
  490.      */
  491.     public function getEventParticipations(): Collection
  492.     {
  493.         return $this->actions->filter(
  494.             function ($action) {
  495.                 return $action instanceof EventParticipation;
  496.             }
  497.         );
  498.     }
  499.     /**
  500.      * Get waiting band invitation
  501.      *
  502.      * @return Collection
  503.      */
  504.     public function getWaitingBandInvitations(): Collection
  505.     {
  506.         return $this->actions->filter(
  507.             function ($action) {
  508.                 return $action instanceof EventParticipation && $action->getAction() == EventParticipationConstant::INVITE;
  509.             }
  510.         );
  511.     }
  512.     public function getLikedFiles(): array
  513.     {
  514.         $likes $this->actions->filter(
  515.             function ($action) {
  516.                 return $action instanceof FileAction
  517.                     && $action->getAction() == FileActionConstant::LIKE;
  518.             }
  519.         );
  520.         $likedFiles = array();
  521.         foreach ($likes as $like) {
  522.             $likedFiles[] = $like->getFile();
  523.         }
  524.         return $likedFiles;
  525.     }
  526.     /**
  527.      * Add order
  528.      *
  529.      * @param Order $order
  530.      *
  531.      * @return User
  532.      */
  533.     public function addOrder(Order $order): User
  534.     {
  535.         $this->orders[] = $order;
  536.         return $this;
  537.     }
  538.     /**
  539.      * Remove order
  540.      *
  541.      * @param Order $order
  542.      */
  543.     public function removeOrder(Order $order)
  544.     {
  545.         $this->orders->removeElement($order);
  546.     }
  547.     /**
  548.      * Get orders
  549.      *
  550.      * @return Collection
  551.      */
  552.     public function getOrders(): Collection
  553.     {
  554.         return $this->orders;
  555.     }
  556.     /**
  557.      * Set common information entity
  558.      *
  559.      * @param CommonInformation $commonInformation
  560.      * @return User
  561.      */
  562.     public function setCommonInformation(CommonInformation $commonInformation): User
  563.     {
  564.         if ($this->commonInformation == null) {
  565.             $this->commonInformation $commonInformation;
  566.         }
  567.         return $this;
  568.     }
  569.     /**
  570.      * Get common information entity
  571.      *
  572.      * @return CommonInformation
  573.      */
  574.     public function getCommonInformation(): ?CommonInformation
  575.     {
  576.         return $this->commonInformation;
  577.     }
  578.     /**
  579.      * @return Collection
  580.      */
  581.     public function getPlaybacks(): Collection
  582.     {
  583.         return $this->playbacks;
  584.     }
  585.     /**
  586.      * @param mixed $playbacks
  587.      */
  588.     public function setPlaybacks($playbacks)
  589.     {
  590.         $this->playbacks $playbacks;
  591.     }
  592.     /**
  593.      * @return Collection
  594.      */
  595.     public function getVisits(): Collection
  596.     {
  597.         return $this->visits;
  598.     }
  599.     /**
  600.      * @param mixed $visits
  601.      */
  602.     public function setVisits($visits)
  603.     {
  604.         $this->visits $visits;
  605.     }
  606.     /**
  607.      * Set birthday
  608.      *
  609.      * @param DateTime|null $birthday
  610.      *
  611.      * @return User
  612.      */
  613.     public function setBirthday(?DateTime $birthday): User
  614.     {
  615.         if ($birthday instanceof DateTime) {
  616.             $this->birthday $birthday;
  617.         }
  618.         return $this;
  619.     }
  620.     /**
  621.      * Get birthday
  622.      *
  623.      * @return DateTime|null
  624.      */
  625.     public function getBirthday(): ?DateTime
  626.     {
  627.         return $this->birthday;
  628.     }
  629.     /**
  630.      * Get age
  631.      *
  632.      * @return integer
  633.      */
  634.     public function getAge(): int
  635.     {
  636.         $TSN strtotime(date_format($this->birthday'Y-m-d'));
  637.         $age date('Y') - date('Y'$TSN);
  638.         if (date('md') < date('md'$TSN)) {
  639.             return $age 1;
  640.         }
  641.         return $age;
  642.     }
  643.     /**
  644.      * @return Collection
  645.      */
  646.     public function getReports(): Collection
  647.     {
  648.         return $this->reports;
  649.     }
  650.     /**
  651.      * @param mixed $reports
  652.      */
  653.     public function setReports($reports)
  654.     {
  655.         $this->reports $reports;
  656.     }
  657.     /**
  658.      * @param NewMemberSearch $newMemberSearch
  659.      *
  660.      * @return User
  661.      */
  662.     public function addNewMemberSearch(NewMemberSearch $newMemberSearch): User
  663.     {
  664.         $this->newMemberSearches[] = $newMemberSearch;
  665.         return $this;
  666.     }
  667.     /**
  668.      * @param NewMemberSearch $newMemberSearch
  669.      */
  670.     public function removeNewMemberSearch(NewMemberSearch $newMemberSearch)
  671.     {
  672.         $this->newMemberSearches->removeElement($newMemberSearch);
  673.     }
  674.     /**
  675.      * @return Collection
  676.      */
  677.     public function getNewMemberSearches(): Collection
  678.     {
  679.         return $this->newMemberSearches;
  680.     }
  681. }