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.     public function setPassword($password): self
  306.     {
  307.         $this->password $password;
  308.         return $this;
  309.     }
  310.     /**
  311.      * @return Campaign[]|ArrayCollection
  312.      */
  313.     public function getCampaigns()
  314.     {
  315.         return $this->campaigns;
  316.     }
  317.     /**
  318.      * @return Campaign|null
  319.      */
  320.     public function getUnfinishedCampaign()
  321.     {
  322.         foreach ($this->campaigns as $campaign) {
  323.             if (!$campaign->isFinished()) {
  324.                 return $campaign;
  325.             }
  326.         }
  327.         return null;
  328.     }
  329.     /**
  330.      * @return Campaign|null
  331.      */
  332.     public function getFinishedCampaign()
  333.     {
  334.         foreach ($this->campaigns as $campaign) {
  335.             if ($campaign->isFinished()) {
  336.                 return $campaign;
  337.             }
  338.         }
  339.         return null;
  340.     }
  341.     /**
  342.      * @return Campaign|null
  343.      */
  344.     public function getActiveOrUnstartedCampaign()
  345.     {
  346.         foreach ($this->campaigns as $campaign) {
  347.             if ($campaign->isActive() || !$campaign->isStarted() || $campaign->isFinished() ) {
  348.                 return $campaign;
  349.             }
  350.         }
  351.         return null;
  352.     }
  353.     /**
  354.      * @param Campaign $campaign
  355.      * @return $this
  356.      */
  357.     public function addCampaign(Campaign $campaign)
  358.     {
  359.         if (!$this->campaigns->contains($campaign)) {
  360.             $this->campaigns->add($campaign);
  361.             $campaign->setUser($this);
  362.         }
  363.         return $this;
  364.     }
  365.     /**
  366.      * @return bool
  367.      */
  368.     public function hasUnfinishedCampaign()
  369.     {
  370.         return (bool) $this->getUnfinishedCampaign();
  371.     }
  372.     /**
  373.      * @return bool
  374.      */
  375.     public function hasFinishedCampaign()
  376.     {
  377.         return (bool) $this->getFinishedCampaign();
  378.     }
  379.     /**
  380.      * @return ArrayCollection|UserTransaction[]
  381.      */
  382.     public function getTransactions()
  383.     {
  384.         return $this->transactions;
  385.     }
  386.     /**
  387.      * @param UserTransaction $transaction
  388.      * @return $this
  389.      */
  390.     public function addTransaction(UserTransaction $transaction)
  391.     {
  392.         $existing $this->transactions->filter(function (UserTransaction $item) use ($transaction) {
  393.             return $transaction->isEqualTo($item);
  394.         });
  395.         if ($existing->isEmpty()) {
  396.             if (!$transaction->getSender()) {
  397.                 $transaction->setSender($this);
  398.             }
  399.             $this->transactions->add($transaction);
  400.         }
  401.         return $this;
  402.     }
  403.     /**
  404.      * @return ArrayCollection|UserContact[]
  405.      */
  406.     public function getContacts()
  407.     {
  408.         return $this->contacts->filter(function (UserContact $item) {
  409.             return !$item->isContactWasDeleted();
  410.         });
  411.     }
  412.     /**
  413.      * @param User $user
  414.      * @return UserContact|null
  415.      */
  416.     public function findContact(User $user)
  417.     {
  418.         $id $user->getId();
  419.         $exists $this->contacts->filter(function (UserContact $item) use ($id) {
  420.             return $item->getDonor() && $item->getDonor()->getId() == $id;
  421.         });
  422.         return $exists->isEmpty() ? null $exists->first();
  423.     }
  424.     /**
  425.      * @param User $user
  426.      * @return bool
  427.      */
  428.     public function hasContact(User $user)
  429.     {
  430.         return $this->findContact($user) !== null;
  431.     }
  432.     /**
  433.      * @param string|null $email
  434.      * @return UserContact|null
  435.      */
  436.     public function findContactByEmail($email)
  437.     {
  438.         if (!$email) {
  439.             return null;
  440.         }
  441.         $exists $this->contacts->filter(function (UserContact $item) use ($email) {
  442.             return $item->getDonor() && $item->getDonor()->getLogin() == $email;
  443.         });
  444.         return $exists->isEmpty() ? null $exists->first();
  445.     }
  446.     /**
  447.      * @param string|null $email
  448.      * @return bool
  449.      */
  450.     public function hasContactByEmail($email)
  451.     {
  452.         if (!$email) {
  453.             return false;
  454.         }
  455.         return $this->findContactByEmail($email) !== null;
  456.     }
  457.     /**
  458.      * @param User $user
  459.      * @return UserContact
  460.      */
  461.     public function addContact(User $user)
  462.     {
  463.         $result = new UserContact();
  464.         $result->setDonor($user);
  465.         $result->setStudent($this);
  466.         $result->setFirstName($user->getProfile()->getFirstName());
  467.         $result->setLastName($user->getProfile()->getLastName());
  468.         $this->contacts->add($result);
  469.         return $result;
  470.     }
  471.     /**
  472.      * @return $this
  473.      */
  474.     public function syncFromProfile()
  475.     {
  476.         if ($this->profile) {
  477.             $email $this->profile->getEmail();
  478.             $this->login $email;
  479.             $this->email $email;
  480.             $this->username $email;
  481.             $this->usernameCanonical $email;
  482.             $this->emailCanonical $email;
  483.         }
  484.         return $this;
  485.     }
  486.     /**
  487.      * @return UserMajor
  488.      */
  489.     public function getMajor()
  490.     {
  491.         return $this->profile->getMajor();
  492.     }
  493.     /**
  494.      * @return UserStatistic|null
  495.      */
  496.     public function getStatistic()
  497.     {
  498.         return $this->statistic;
  499.     }
  500.     /**
  501.      * @param UserStatistic|null $statistic
  502.      * @return User
  503.      */
  504.     public function setStatistic(UserStatistic $statistic): User
  505.     {
  506.         $this->statistic $statistic;
  507.         if ($statistic->getUser() !== $this) {
  508.             $statistic->setUser($this);
  509.         }
  510.         return $this;
  511.     }
  512.     /**
  513.      * @inheritDoc
  514.      */
  515.     public function getFirstName(): ?string
  516.     {
  517.         return $this->profile->getFirstName();
  518.     }
  519.     /**
  520.      * @inheritDoc
  521.      */
  522.     public function setFirstName($firstName)
  523.     {
  524.         $this->profile->setFirstName($firstName);
  525.         return $this;
  526.     }
  527.     /**
  528.      * @inheritDoc
  529.      */
  530.     public function getLastName(): ?string
  531.     {
  532.         return $this->profile->getLastName();
  533.     }
  534.     /**
  535.      * @inheritDoc
  536.      */
  537.     public function setLastName($lastName)
  538.     {
  539.         $this->profile->setLastName($lastName);
  540.         return $this;
  541.     }
  542.     /**
  543.      * @inheritDoc
  544.      */
  545.     public function getFullName(): ?string
  546.     {
  547.         return $this->profile->getFullName();
  548.     }
  549.     /**
  550.      * @return bool
  551.      */
  552.     public function isUserDeleted()
  553.     {
  554.         return preg_match("/\S*__[0-9]*/"$this->email);
  555.     }
  556.     public function isUserBlocked(): bool
  557.     {
  558.         return ($this->isStudent() || $this->isDonor() || $this->isInternal()) && !$this->isEnabled();
  559.     }
  560.     /**
  561.      * @return UserSchoolShopper[]
  562.      */
  563.     public function getUserSchoolShoppers()
  564.     {
  565.         return $this->userSchoolShoppers;
  566.     }
  567.     /**
  568.      * @return UserSchoolShopper|null
  569.      */
  570.     public function getActiveShopper(): ?UserSchoolShopper
  571.     {
  572.         foreach ($this->getUserSchoolShoppers() as $shopper) {
  573.             if ($shopper->isActive() && $shopper->getSchool()->getId() === $this->getProfile()->getSchool()->getId()) {
  574.                 return $shopper;
  575.             }
  576.         }
  577.         return null;
  578.     }
  579.     public function deactivateShoppers()
  580.     {
  581.         foreach ($this->getUserSchoolShoppers() as $shopper) {
  582.             $shopper->setIsActive(false);
  583.         }
  584.     }
  585.     /**
  586.      * @param UserSchoolShopper $userSchoolShopper
  587.      * @return $this
  588.      */
  589.     public function addActiveUserSchoolShopper(UserSchoolShopper $userSchoolShopper): self
  590.     {
  591.         $this->deactivateShoppers();
  592.         if (!$this->userSchoolShoppers->contains($userSchoolShopper)) {
  593.             $this->userSchoolShoppers->add($userSchoolShopper);
  594.             $userSchoolShopper->setUser($this);
  595.         }
  596.         return $this;
  597.     }
  598.     /**
  599.      * @return ArrayCollection|Notification[]
  600.      */
  601.     public function getNotifications()
  602.     {
  603.         return $this->notifications;
  604.     }
  605.     /**
  606.      * @param UserNotifications $notification
  607.      * @return $this
  608.      */
  609.     public function addNotifications(UserNotifications $notification): self
  610.     {
  611.         if (!$this->notifications->contains($notification)) {
  612.             $this->notifications->add($notification);
  613.             $notification->setUser($this);
  614.         }
  615.         return $this;
  616.     }
  617.     public function getUserIdentifier(): string
  618.     {
  619.         return $this->username ?: '';
  620.     }
  621. }