src/FMT/Data/Entity/User.php line 41

Open in your IDE?
  1. <?php
  2. namespace FMT\Data\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use FOS\UserBundle\Model\User as BaseUser;
  6. use Gedmo\Timestampable\Traits\TimestampableEntity;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Notifier\Notification\Notification;
  9. use Symfony\Component\Security\Core\User\EquatableInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Symfony\Component\Validator\Constraints\CollegeEmail;
  13. /**
  14.  * User
  15.  *
  16.  * @var UniqueEntity
  17.  *
  18.  * @ORM\Table(
  19.  *     name="user",
  20.  *     uniqueConstraints={@ORM\UniqueConstraint(name="IX_unique_login", columns={"login"})},
  21.  *     indexes={@ORM\Index(name="FK_user_profile", columns={"profile_id"})}
  22.  * )
  23.  * @ORM\Entity(repositoryClass="FMT\Data\Repository\UserRepository")
  24.  * @ORM\HasLifecycleCallbacks()
  25.  * @UniqueEntity(
  26.  *     fields={"login"},
  27.  *     message="fmt.unique_email.error", groups={"UserPortal"})
  28.  * @UniqueEntity(
  29.  *     fields={"login"},
  30.  *     message="fmt.unique_email.reset_request", groups={"registration"})
  31.  * @UniqueEntity(
  32.  *     fields={"login"},
  33.  *     message="fmt.admin.email_unique", groups={"Admin"})
  34.  * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  35.  * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  36.  */
  37.  
  38. class User extends BaseUser implements EntityInterfaceEquatableInterfaceMinimalUserInterface
  39. {
  40.     use TimestampableEntity;
  41.     const ROLE_STUDENT 'ROLE_STUDENT';
  42.     const ROLE_DONOR 'ROLE_DONOR';
  43.     const ROLE_INCOMPLETE_STUDENT 'ROLE_INCOMPLETE_STUDENT';
  44.     const ROLE_INCOMPLETE_DONOR 'ROLE_INCOMPLETE_DONOR';
  45.     const ROLE_SUPER_ADMIN 'ROLE_SUPER_ADMIN';
  46.     const ROLE_MANAGER 'ROLE_MANAGER';
  47.     const ROLE_REPORT_VIEWER 'ROLE_REPORT_VIEWER';
  48.     const ROLE_MARKETING_USER 'ROLE_MARKETING_USER';
  49.     const ROLE_INTERNAL 'ROLE_INTERNAL';
  50.     const ROLE_SCHOOL_ADMIN 'ROLE_SCHOOL_ADMIN';
  51.     public const INTERNAL_USER_ROLES = [
  52.         self::ROLE_SUPER_ADMIN,
  53.         self::ROLE_MANAGER,
  54.         self::ROLE_REPORT_VIEWER,
  55.         self::ROLE_MARKETING_USER,
  56.         self::ROLE_SCHOOL_ADMIN
  57.     ];
  58.     public const EXTERNAL_USER_ROLES = [
  59.         self::ROLE_DONOR,
  60.         self::ROLE_STUDENT,
  61.     ];
  62.     const DELETED_USER_DELIMITER_MARK '___';
  63.     /**
  64.      * @var integer
  65.      *
  66.      * @ORM\Column(name="id", type="integer")
  67.      * @ORM\Id
  68.      * @ORM\GeneratedValue(strategy="IDENTITY")
  69.      */
  70.     protected $id;
  71.     /**
  72.      * @var UserProfile
  73.      *
  74.      * @ORM\OneToOne(targetEntity="UserProfile", inversedBy="user", cascade={"persist", "remove"})
  75.      * @ORM\JoinColumns({
  76.      *   @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
  77.      * })
  78.      * @Assert\Valid()
  79.      */
  80.     private $profile;
  81.     /**
  82.      * @var string
  83.      *
  84.      * @ORM\Column(name="login", type="string", length=255, nullable=false)
  85.      */
  86.     private $login;
  87.     /**
  88.      * @var Campaign[]|ArrayCollection
  89.      *
  90.      * @ORM\OneToMany(targetEntity="Campaign", mappedBy="user", cascade={"persist"})
  91.      * @ORM\OrderBy({"endDate" = "DESC"})
  92.      */
  93.     private $campaigns;
  94.     /**
  95.      * @var UserTransaction[]|ArrayCollection
  96.      *
  97.      * @ORM\OneToMany(targetEntity="UserTransaction", mappedBy="sender", cascade={"persist"})
  98.      */
  99.     private $transactions;
  100.     /**
  101.      * @var UserContact[]|ArrayCollection
  102.      *
  103.      * @ORM\OneToMany(targetEntity="UserContact", mappedBy="student", cascade={"persist"})
  104.      */
  105.     private $contacts;
  106.     /**
  107.      * @var UserSchoolShopper[]|ArrayCollection
  108.      *
  109.      * @ORM\OneToMany(targetEntity="UserSchoolShopper", mappedBy="user", cascade={"persist"})
  110.      */
  111.     private $userSchoolShoppers;
  112.     /**
  113.      * @var UserStatistic|null
  114.      *
  115.      * @ORM\OneToOne(
  116.      *     targetEntity="\FMT\Data\Entity\UserStatistic",
  117.      *      inversedBy="user",
  118.      *      cascade={"persist", "remove"}
  119.      *     )
  120.      */
  121.     private $statistic;
  122.     /**
  123.      * @var Notification[]|ArrayCollection
  124.      *
  125.      * @ORM\OneToMany(targetEntity="UserNotifications", mappedBy="user", cascade={"persist", "remove"})
  126.      */
  127.     private $notifications;
  128.     /**
  129.      * User constructor.
  130.      */
  131.     public function __construct()
  132.     {
  133.         parent::__construct();
  134.         $this->profile = new UserProfile();
  135.         $this->profile->setUser($this);
  136.         $this->campaigns = new ArrayCollection();
  137.         $this->transactions = new ArrayCollection();
  138.         $this->contacts = new ArrayCollection();
  139.         $this->notifications = new ArrayCollection();
  140.         $this->statistic = new UserStatistic();
  141.         $this->statistic->setUser($this);
  142.     }
  143.     
  144.     /**
  145.      * @return int
  146.      */
  147.     public function getId()
  148.     {
  149.         return $this->id;
  150.     }
  151.     /**
  152.      * @return UserProfile
  153.      */
  154.     public function getProfile()
  155.     {
  156.         return $this->profile;
  157.     }
  158.     /**
  159.      * @param UserProfile $profile
  160.      * @return $this
  161.      */
  162.     public function setProfile($profile)
  163.     {
  164.         $this->profile $profile;
  165.         if ($this->profile) {
  166.             $this->profile->syncFromUser();
  167.         }
  168.         return $this;
  169.     }
  170.     /**
  171.      * @return string
  172.      */
  173.     public function getLogin()
  174.     {
  175.         return $this->login;
  176.     }
  177.     /**
  178.      * @param $login
  179.      * @return $this
  180.      */
  181.     public function setLogin($login)
  182.     {
  183.         $this->login $login;
  184.         $this->email $login;
  185.         $this->username $login;
  186.         $this->usernameCanonical $login;
  187.         $this->emailCanonical $login;
  188.         if ($this->profile) {
  189.             $this->profile->syncFromUser();
  190.         }
  191.         return $this;
  192.     }
  193.     /**
  194.      * @param string $email
  195.      * @return $this
  196.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  197.      */
  198.     public function setEmail($email)
  199.     {
  200.         /**
  201.          * NOTE: Email field should be in sync with login
  202.          */
  203.         return $this;
  204.     }
  205.     /**
  206.      * @param string $username
  207.      * @return $this
  208.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  209.      */
  210.     public function setUsername($username)
  211.     {
  212.         /**
  213.          * NOTE: User name field should be in sync with login
  214.          */
  215.         return $this;
  216.     }
  217.     /**
  218.      * @return bool
  219.      */
  220.     public function isStudent()
  221.     {
  222.         return $this->hasRole(self::ROLE_STUDENT);
  223.     }
  224.     /**
  225.      * @return bool
  226.      */
  227.     public function isIncompleteStudent()
  228.     {
  229.         return $this->hasRole(self::ROLE_INCOMPLETE_STUDENT);
  230.     }
  231.     /**
  232.      * @return bool
  233.      */
  234.     public function isActiveStudent()
  235.     {
  236.         if (!$this->isStudent()) {
  237.             return false;
  238.         }
  239.         return $this->isEnabled();
  240.     }
  241.     /**
  242.      * @return bool
  243.      */
  244.     public function isDonor()
  245.     {
  246.         return $this->hasRole(self::ROLE_DONOR);
  247.     }
  248.     /**
  249.      * @return bool
  250.      */
  251.     public function isIncompleteDonor()
  252.     {
  253.         return $this->hasRole(self::ROLE_INCOMPLETE_DONOR);
  254.     }
  255.     /**
  256.      * @return bool
  257.      */
  258.     public function isAnyStudent()
  259.     {
  260.         return $this->hasRole(User::ROLE_STUDENT) || $this->hasRole(User::ROLE_INCOMPLETE_STUDENT);
  261.     }
  262.     /**
  263.      * @return bool
  264.      */
  265.     public function isAnyDonor()
  266.     {
  267.         return $this->hasRole(User::ROLE_DONOR) || $this->hasRole(User::ROLE_INCOMPLETE_DONOR);
  268.     }
  269.     public function isInternal(): bool
  270.     {
  271.         foreach (self::INTERNAL_USER_ROLES as $role) {
  272.             if ($this->hasRole($role)) {
  273.                 return true;
  274.             }
  275.         }
  276.         return false;
  277.     }
  278.     /**
  279.      * @return bool
  280.      */
  281.     public function isCompleted()
  282.     {
  283.         return $this->hasRole(User::ROLE_DONOR) || $this->hasRole(User::ROLE_STUDENT);
  284.     }
  285.     /**
  286.      * @return bool
  287.      */
  288.     public function isRegistered()
  289.     {
  290.         $hasAnyRole $this->isAnyDonor() || $this->isAnyStudent() || $this->isSuperAdmin();
  291.         return $this->isEnabled() && $hasAnyRole;
  292.     }
  293.     /**
  294.      * @param UserInterface $user
  295.      * @return bool
  296.      */
  297.     public function isEqualTo(UserInterface $user): bool
  298.     {
  299.         if (!$user instanceof User) {
  300.             return false;
  301.         }
  302.         return $this->password == $user->getPassword();
  303.     }
  304.     /**
  305.      * @return Campaign[]|ArrayCollection
  306.      */
  307.     public function getCampaigns()
  308.     {
  309.         return $this->campaigns;
  310.     }
  311.     /**
  312.      * @return Campaign|null
  313.      */
  314.     public function getUnfinishedCampaign()
  315.     {
  316.         foreach ($this->campaigns as $campaign) {
  317.             if (!$campaign->isFinished()) {
  318.                 return $campaign;
  319.             }
  320.         }
  321.         return null;
  322.     }
  323.     /**
  324.      * @return Campaign|null
  325.      */
  326.     public function getFinishedCampaign()
  327.     {
  328.         foreach ($this->campaigns as $campaign) {
  329.             if ($campaign->isFinished()) {
  330.                 return $campaign;
  331.             }
  332.         }
  333.         return null;
  334.     }
  335.     /**
  336.      * @return Campaign|null
  337.      */
  338.     public function getActiveOrUnstartedCampaign()
  339.     {
  340.         foreach ($this->campaigns as $campaign) {
  341.             if ($campaign->isActive() || !$campaign->isStarted() || $campaign->isFinished() ) {
  342.                 return $campaign;
  343.             }
  344.         }
  345.         return null;
  346.     }
  347.     /**
  348.      * @param Campaign $campaign
  349.      * @return $this
  350.      */
  351.     public function addCampaign(Campaign $campaign)
  352.     {
  353.         if (!$this->campaigns->contains($campaign)) {
  354.             $this->campaigns->add($campaign);
  355.             $campaign->setUser($this);
  356.         }
  357.         return $this;
  358.     }
  359.     /**
  360.      * @return bool
  361.      */
  362.     public function hasUnfinishedCampaign()
  363.     {
  364.         return (bool) $this->getUnfinishedCampaign();
  365.     }
  366.     /**
  367.      * @return bool
  368.      */
  369.     public function hasFinishedCampaign()
  370.     {
  371.         return (bool) $this->getFinishedCampaign();
  372.     }
  373.     /**
  374.      * @return ArrayCollection|UserTransaction[]
  375.      */
  376.     public function getTransactions()
  377.     {
  378.         return $this->transactions;
  379.     }
  380.     /**
  381.      * @param UserTransaction $transaction
  382.      * @return $this
  383.      */
  384.     public function addTransaction(UserTransaction $transaction)
  385.     {
  386.         $existing $this->transactions->filter(function (UserTransaction $item) use ($transaction) {
  387.             return $transaction->isEqualTo($item);
  388.         });
  389.         if ($existing->isEmpty()) {
  390.             if (!$transaction->getSender()) {
  391.                 $transaction->setSender($this);
  392.             }
  393.             $this->transactions->add($transaction);
  394.         }
  395.         return $this;
  396.     }
  397.     /**
  398.      * @return ArrayCollection|UserContact[]
  399.      */
  400.     public function getContacts()
  401.     {
  402.         return $this->contacts->filter(function (UserContact $item) {
  403.             return !$item->isContactWasDeleted();
  404.         });
  405.     }
  406.     /**
  407.      * @param User $user
  408.      * @return UserContact|null
  409.      */
  410.     public function findContact(User $user)
  411.     {
  412.         $id $user->getId();
  413.         $exists $this->contacts->filter(function (UserContact $item) use ($id) {
  414.             return $item->getDonor() && $item->getDonor()->getId() == $id;
  415.         });
  416.         return $exists->isEmpty() ? null $exists->first();
  417.     }
  418.     /**
  419.      * @param User $user
  420.      * @return bool
  421.      */
  422.     public function hasContact(User $user)
  423.     {
  424.         return $this->findContact($user) !== null;
  425.     }
  426.     /**
  427.      * @param string|null $email
  428.      * @return UserContact|null
  429.      */
  430.     public function findContactByEmail($email)
  431.     {
  432.         if (!$email) {
  433.             return null;
  434.         }
  435.         $exists $this->contacts->filter(function (UserContact $item) use ($email) {
  436.             return $item->getDonor() && $item->getDonor()->getLogin() == $email;
  437.         });
  438.         return $exists->isEmpty() ? null $exists->first();
  439.     }
  440.     /**
  441.      * @param string|null $email
  442.      * @return bool
  443.      */
  444.     public function hasContactByEmail($email)
  445.     {
  446.         if (!$email) {
  447.             return false;
  448.         }
  449.         return $this->findContactByEmail($email) !== null;
  450.     }
  451.     /**
  452.      * @param User $user
  453.      * @return UserContact
  454.      */
  455.     public function addContact(User $user)
  456.     {
  457.         $result = new UserContact();
  458.         $result->setDonor($user);
  459.         $result->setStudent($this);
  460.         $result->setFirstName($user->getProfile()->getFirstName());
  461.         $result->setLastName($user->getProfile()->getLastName());
  462.         $this->contacts->add($result);
  463.         return $result;
  464.     }
  465.     /**
  466.      * @return $this
  467.      */
  468.     public function syncFromProfile()
  469.     {
  470.         if ($this->profile) {
  471.             $email $this->profile->getEmail();
  472.             $this->login $email;
  473.             $this->email $email;
  474.             $this->username $email;
  475.             $this->usernameCanonical $email;
  476.             $this->emailCanonical $email;
  477.         }
  478.         return $this;
  479.     }
  480.     /**
  481.      * @return UserMajor
  482.      */
  483.     public function getMajor()
  484.     {
  485.         return $this->profile->getMajor();
  486.     }
  487.     /**
  488.      * @return UserStatistic|null
  489.      */
  490.     public function getStatistic()
  491.     {
  492.         return $this->statistic;
  493.     }
  494.     /**
  495.      * @param UserStatistic|null $statistic
  496.      * @return User
  497.      */
  498.     public function setStatistic(UserStatistic $statistic): User
  499.     {
  500.         $this->statistic $statistic;
  501.         if ($statistic->getUser() !== $this) {
  502.             $statistic->setUser($this);
  503.         }
  504.         return $this;
  505.     }
  506.     /**
  507.      * @inheritDoc
  508.      */
  509.     public function getFirstName(): ?string
  510.     {
  511.         return $this->profile->getFirstName();
  512.     }
  513.     /**
  514.      * @inheritDoc
  515.      */
  516.     public function setFirstName($firstName)
  517.     {
  518.         $this->profile->setFirstName($firstName);
  519.         return $this;
  520.     }
  521.     /**
  522.      * @inheritDoc
  523.      */
  524.     public function getLastName(): ?string
  525.     {
  526.         return $this->profile->getLastName();
  527.     }
  528.     /**
  529.      * @inheritDoc
  530.      */
  531.     public function setLastName($lastName)
  532.     {
  533.         $this->profile->setLastName($lastName);
  534.         return $this;
  535.     }
  536.     /**
  537.      * @inheritDoc
  538.      */
  539.     public function getFullName(): ?string
  540.     {
  541.         return $this->profile->getFullName();
  542.     }
  543.     /**
  544.      * @return bool
  545.      */
  546.     public function isUserDeleted()
  547.     {
  548.         return preg_match("/\S*__[0-9]*/"$this->email);
  549.     }
  550.     public function isUserBlocked(): bool
  551.     {
  552.         return ($this->isStudent() || $this->isDonor() || $this->isInternal()) && !$this->isEnabled();
  553.     }
  554.     /**
  555.      * @return UserSchoolShopper[]
  556.      */
  557.     public function getUserSchoolShoppers()
  558.     {
  559.         return $this->userSchoolShoppers;
  560.     }
  561.     /**
  562.      * @return UserSchoolShopper|null
  563.      */
  564.     public function getActiveShopper(): ?UserSchoolShopper
  565.     {
  566.         foreach ($this->getUserSchoolShoppers() as $shopper) {
  567.             if ($shopper->isActive() && $shopper->getSchool()->getId() === $this->getProfile()->getSchool()->getId()) {
  568.                 return $shopper;
  569.             }
  570.         }
  571.         return null;
  572.     }
  573.     public function deactivateShoppers()
  574.     {
  575.         foreach ($this->getUserSchoolShoppers() as $shopper) {
  576.             $shopper->setIsActive(false);
  577.         }
  578.     }
  579.     /**
  580.      * @param UserSchoolShopper $userSchoolShopper
  581.      * @return $this
  582.      */
  583.     public function addActiveUserSchoolShopper(UserSchoolShopper $userSchoolShopper): self
  584.     {
  585.         $this->deactivateShoppers();
  586.         if (!$this->userSchoolShoppers->contains($userSchoolShopper)) {
  587.             $this->userSchoolShoppers->add($userSchoolShopper);
  588.             $userSchoolShopper->setUser($this);
  589.         }
  590.         return $this;
  591.     }
  592.     /**
  593.      * @return ArrayCollection|Notification[]
  594.      */
  595.     public function getNotifications()
  596.     {
  597.         return $this->notifications;
  598.     }
  599.     /**
  600.      * @param UserNotifications $notification
  601.      * @return $this
  602.      */
  603.     public function addNotifications(UserNotifications $notification): self
  604.     {
  605.         if (!$this->notifications->contains($notification)) {
  606.             $this->notifications->add($notification);
  607.             $notification->setUser($this);
  608.         }
  609.         return $this;
  610.     }
  611.     public function getUserIdentifier(): string
  612.     {
  613.         return $this->username ?: '';
  614.     }
  615. }