src/Entity/User.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Prescriber\Prescriber;
  4. use App\Utils\Formatter;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Doctrine\ORM\PersistentCollection;
  9. use FOS\UserBundle\Model\UserInterface;
  10. use FOS\UserBundle\Model\User as BaseUser;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Wits\PaymentBundle\Entity\Enum\PaymentMethodTypeEnum;
  15. use Wits\PaymentBundle\Entity\PaymentMethod;
  16. use Wits\PaymentBundle\Entity\StripeUserInterface;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. /**
  19.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  20.  */
  21. class User extends BaseUser implements UserInterfaceStripeUserInterfaceTwoFactorInterface
  22. {
  23.     const ROLE_ADMIN 'ROLE_ADMIN';
  24.     const ROLE_FREELANCER 'ROLE_FREELANCER';
  25.     const ROLE_BUSINESS_INTRODUCER 'ROLE_BUSINESS_INTRODUCER';
  26.     const ROLE_CENTRAL_PURCHASING 'ROLE_CENTRAL_PURCHASING';
  27.     const ROLE_FREELANCER_ASSISTANT 'ROLE_FREELANCER_ASSISTANT';
  28.     const ROLE_PRESCRIBER 'ROLE_PRESCRIBER';
  29.     const ROLE_EDITOR 'ROLE_EDITOR';
  30.     const ROLE_USER 'ROLE_USER';
  31.     /**
  32.      * @ORM\Id()
  33.      * @ORM\GeneratedValue()
  34.      * @ORM\Column(type="integer")
  35.      */
  36.     protected $id;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      *
  40.      * @Assert\NotBlank(message="Merci d'entrer votre prénom.", groups={"Registration", "Profile"})
  41.      * @Assert\Length(
  42.      *     min=1,
  43.      *     max=255,
  44.      *     minMessage="Le prénom est trop court.",
  45.      *     maxMessage="Le prénom est trop long.",
  46.      *     groups={"Registration", "Profile"}
  47.      * )
  48.      */
  49.     protected $firstname;
  50.     /**
  51.      * @ORM\Column(type="string", length=255, nullable=true)
  52.      *
  53.      * @Assert\NotBlank(message="Merci d'entrer votre nom.", groups={"Registration", "Profile"})
  54.      * @Assert\Length(
  55.      *     min=1,
  56.      *     max=255,
  57.      *     minMessage="Le nom est trop court.",
  58.      *     maxMessage="Le nom est trop long.",
  59.      *     groups={"Registration", "Profile"}
  60.      * )
  61.      */
  62.     protected $lastname;
  63.     /**
  64.      * @ORM\Column(type="string", length=255, nullable=true)
  65.      *
  66.      * @Assert\NotBlank(message="Merci d'entrer votre téléphone.", groups={"Registration", "Profile"})
  67.      * @Assert\Regex(
  68.      *     pattern="/^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/",
  69.      *     message="Le téléphone est invalide.",
  70.      *     groups={"Registration", "Profile"}
  71.      * )
  72.      */
  73.     protected $phoneNumber;
  74.     /**
  75.      * @ORM\ManyToMany(targetEntity="App\Entity\HearingBrand")
  76.      */
  77.     private $authorizedBrands;
  78.     /**
  79.      * @Assert\Valid
  80.      * @ORM\OneToOne(targetEntity="App\Entity\Administrator", mappedBy="user", cascade={"persist", "remove"})
  81.      */
  82.     private $administrator;
  83.     /**
  84.      * @ORM\Column(type="string", length=255, nullable=true)
  85.      */
  86.     protected $stripeId;
  87.     /**
  88.      * @ORM\OneToOne(targetEntity=CentralPurchasing::class, mappedBy="user", cascade={"persist", "remove"})
  89.      */
  90.     private $centralPurchasing;
  91.     /**
  92.      * @ORM\OneToOne(targetEntity=FreelancerAssistant::class, mappedBy="user", cascade={"persist", "remove"})
  93.      */
  94.     private $freelancerAssistant;
  95.     /**
  96.      * @Gedmo\Timestampable(on="create")
  97.      * @ORM\Column(type="datetime")
  98.      */
  99.     private $createdAt;
  100.     /**
  101.      * @ORM\Column(type="integer", nullable=true)
  102.      *
  103.      * null tant que le set-up est désactivé
  104.      * mettre 0 comme valeur init pour activer la fonctionnalité
  105.      * activé le 15/06
  106.      */
  107.     private $configStep 0;
  108.     /**
  109.      * @ORM\OneToMany(targetEntity=UserTracking::class, mappedBy="user", cascade={"remove"})
  110.      */
  111.     private $userTracking;
  112.     /**
  113.      * @ORM\OneToOne(targetEntity=UserTrackingDuration::class, inversedBy="user", cascade={"persist", "remove"})
  114.      */
  115.     private $userTrackingDuration;
  116.     /**
  117.      * @ORM\OneToMany(targetEntity=PaymentMethod::class, mappedBy="user", orphanRemoval=true)
  118.      */
  119.     private $paymentMethods;
  120.     /**
  121.      * @ORM\OneToOne(targetEntity=PaymentMethod::class)
  122.      */
  123.     private $defaultPaymentMethod;
  124.     /**
  125.      * @ORM\OneToOne(targetEntity=Store::class)
  126.      */
  127.     private $storeToAssociateSepaWith;
  128.     /**
  129.      * Two factor authentification code
  130.      * @ORM\Column(type="string", nullable=true)
  131.      */
  132.     private string $twoFactorAuthCode;
  133.     /**
  134.      * Two factor enabled ?
  135.      * @ORM\Column(type="boolean")
  136.      */
  137.     private bool $emailAuthEnabled false;
  138.     /**
  139.      * @ORM\OneToOne(targetEntity=Prescriber::class, mappedBy="user", cascade={"persist", "remove"})
  140.      */
  141.     private $prescriber;
  142.     public function __construct()
  143.     {
  144.         parent::__construct();
  145.         $this->authorizedBrands = new ArrayCollection();
  146.         $this->userTracking = new ArrayCollection();
  147.         $this->freelancerComments = new ArrayCollection();
  148.         $this->paymentMethods = new ArrayCollection();
  149.     }
  150.     /**
  151.      * Représente l'entité par défault
  152.      * @return string
  153.      */
  154.     public function __toString()
  155.     {
  156.         return 'Utilisateur ' $this->getUsername() . ' [' $this->getId() . ']';
  157.     }
  158.     /**
  159.      * Représente l'entité dans le BO
  160.      * @return string
  161.      */
  162.     public function getLabel()
  163.     {
  164.         return $this->getEmail();
  165.     }
  166.     public function getId(): ?int
  167.     {
  168.         return $this->id;
  169.     }
  170.     public function getMainRole(): string
  171.     {
  172.         if ($this->isAdmin()) {
  173.             return User::ROLE_ADMIN;
  174.         } elseif ($this->isTypeFreelancer()) {
  175.             return User::ROLE_FREELANCER;
  176.         } elseif ($this->isTypeFreelancerAssistant()) {
  177.             return User::ROLE_FREELANCER_ASSISTANT;
  178.         } else {
  179.             return User::ROLE_USER;
  180.         }
  181.     }
  182.     public function isAdmin(): bool
  183.     {
  184.         return $this->hasRole(User::ROLE_ADMIN) || $this->hasRole(User::ROLE_SUPER_ADMIN);
  185.     }
  186.     public function hasRoleBusinessIntroducer(): bool
  187.     {
  188.         return $this->hasRole(User::ROLE_BUSINESS_INTRODUCER);
  189.     }
  190.     public function hasRoleCentralPurchasing(): bool
  191.     {
  192.         return $this->hasRole(User::ROLE_CENTRAL_PURCHASING);
  193.     }
  194.     public function hasRolePrescriber(): bool
  195.     {
  196.         return $this->hasRole(User::ROLE_PRESCRIBER);
  197.     }
  198.     /**
  199.      * @return Collection|HearingBrand[]
  200.      */
  201.     public function getAuthorizedBrands(): Collection
  202.     {
  203.         return $this->authorizedBrands;
  204.     }
  205.     public function addAuthorizedBrand(HearingBrand $authorizedBrand): self
  206.     {
  207.         if (!$this->authorizedBrands->contains($authorizedBrand)) {
  208.             $this->authorizedBrands[] = $authorizedBrand;
  209.         }
  210.         return $this;
  211.     }
  212.     public function removeAuthorizedBrand(HearingBrand $authorizedBrand): self
  213.     {
  214.         if ($this->authorizedBrands->contains($authorizedBrand)) {
  215.             $this->authorizedBrands->removeElement($authorizedBrand);
  216.         }
  217.         return $this;
  218.     }
  219.     public function getAdministrator(): ?Administrator
  220.     {
  221.         return $this->administrator;
  222.     }
  223.     public function setAdministrator(?Administrator $administrator): self
  224.     {
  225.         $this->administrator $administrator;
  226.         // set (or unset) the owning side of the relation if necessary
  227.         $newUser $administrator === null null $this;
  228.         if ($newUser !== $administrator->getUser()) {
  229.             $administrator->setUser($newUser);
  230.         }
  231.         return $this;
  232.     }
  233.     public function getFirstname(): ?string
  234.     {
  235.         return $this->firstname;
  236.     }
  237.     public function setFirstname(string $firstname): self
  238.     {
  239.         $this->firstname $firstname;
  240.         return $this;
  241.     }
  242.     public function getLastname(): ?string
  243.     {
  244.         return $this->lastname;
  245.     }
  246.     public function setLastname(string $lastname): self
  247.     {
  248.         $this->lastname $lastname;
  249.         return $this;
  250.     }
  251.     public function getPhoneNumber(): ?string
  252.     {
  253.         return $this->phoneNumber;
  254.     }
  255.     public function setPhoneNumber(string $phoneNumber): self
  256.     {
  257.         $phoneNumber Formatter::phoneNumberFrenchFormat($phoneNumber);
  258.         $this->phoneNumber $phoneNumber;
  259.         return $this;
  260.     }
  261.     public function getPhoneNumberFormatted()
  262.     {
  263.         return wordwrap($this->getPhoneNumber(), 2' 'true);
  264.     }
  265.     public function setEmail($email)
  266.     {
  267.         $email is_null($email) ? '' $email;
  268.         parent::setEmail($email);
  269.         $this->setUsername($email);
  270.         return $this;
  271.     }
  272.     public function getFullname(): ?string
  273.     {
  274.         return ucfirst($this->firstname) . ' ' strtoupper($this->lastname);
  275.     }
  276.     public function getFullnameAndEmail(): ?string
  277.     {
  278.         return $this->getFullname() . ' (' $this->getEmail() . ')';
  279.     }
  280.     /**
  281.      * @return mixed
  282.      */
  283.     public function getStripeId(): ?string
  284.     {
  285.         return $this->stripeId;
  286.     }
  287.     /**
  288.      * @param mixed $stripeId
  289.      */
  290.     public function setStripeId($stripeId): void
  291.     {
  292.         $this->stripeId $stripeId;
  293.     }
  294.     public function getCentralPurchasing(): ?CentralPurchasing
  295.     {
  296.         return $this->centralPurchasing;
  297.     }
  298.     public function setCentralPurchasing(CentralPurchasing $centralPurchasing): self
  299.     {
  300.         $this->centralPurchasing $centralPurchasing;
  301.         // set the owning side of the relation if necessary
  302.         if ($centralPurchasing->getUser() !== $this) {
  303.             $centralPurchasing->setUser($this);
  304.         }
  305.         return $this;
  306.     }
  307.     public function getCreatedAt(): ?\DateTimeInterface
  308.     {
  309.         return $this->createdAt;
  310.     }
  311.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  312.     {
  313.         $this->createdAt $createdAt;
  314.         return $this;
  315.     }
  316.     public function getFreelancerAssistant(): ?FreelancerAssistant
  317.     {
  318.         return $this->freelancerAssistant;
  319.     }
  320.     public function setFreelancerAssistant(FreelancerAssistant $freelancerAssistant): self
  321.     {
  322.         $this->freelancerAssistant $freelancerAssistant;
  323.         // set the owning side of the relation if necessary
  324.         if ($freelancerAssistant->getUser() !== $this) {
  325.             $freelancerAssistant->setUser($this);
  326.         }
  327.         return $this;
  328.     }
  329.     public function isTypeFreelancer()
  330.     {
  331.         return $this->administrator instanceof Freelancer;
  332.     }
  333.     public function isTypePartner()
  334.     {
  335.         return $this->administrator instanceof Partner;
  336.     }
  337.     public function isTypeCentralAchat()
  338.     {
  339.         return !is_null($this->centralPurchasing);
  340.     }
  341.     public function isTypeFreelancerAssistant()
  342.     {
  343.         return !is_null($this->freelancerAssistant);
  344.     }
  345.     public function getConfigStep(): ?int
  346.     {
  347.         return $this->configStep;
  348.     }
  349.     public function setConfigStep(?int $configStep): self
  350.     {
  351.         $this->configStep $configStep;
  352.         return $this;
  353.     }
  354.     public function hasFinishedConfigStep()
  355.     {
  356.         return true;//is_null($this->configStep);
  357.     }
  358.     /**
  359.      * @return Collection|UserTracking[]
  360.      */
  361.     public function getUserTracking(): Collection
  362.     {
  363.         return $this->userTracking;
  364.     }
  365.     public function addUserTracking(UserTracking $userTracking): self
  366.     {
  367.         if (!$this->userTracking->contains($userTracking)) {
  368.             $this->userTracking[] = $userTracking;
  369.             $userTracking->setUser($this);
  370.         }
  371.         return $this;
  372.     }
  373.     public function removeUserTracking(UserTracking $userTracking): self
  374.     {
  375.         if ($this->userTracking->contains($userTracking)) {
  376.             $this->userTracking->removeElement($userTracking);
  377.             // set the owning side to null (unless already changed)
  378.             if ($userTracking->getUser() === $this) {
  379.                 $userTracking->setUser(null);
  380.             }
  381.         }
  382.         return $this;
  383.     }
  384.     public function getUserTrackingDuration(): ?UserTrackingDuration
  385.     {
  386.         return $this->userTrackingDuration;
  387.     }
  388.     public function setUserTrackingDuration(?UserTrackingDuration $userTrackingDuration): self
  389.     {
  390.         $this->userTrackingDuration $userTrackingDuration;
  391.         return $this;
  392.     }
  393.     /**
  394.      * @return int|null
  395.      */
  396.     public function getUserTrackingAverageDuration(): ?int
  397.     {
  398.         return ($this->getUserTrackingDuration() ? $this->getUserTrackingDuration()->getAverageDuration() : null);
  399.     }
  400.     /**
  401.      * @return Collection|FreelancerComment[]
  402.      */
  403.     public function getFreelancerComments(): Collection
  404.     {
  405.         return $this->freelancerComments;
  406.     }
  407.     public function addFreelancerComment(FreelancerComment $freelancerComment): self
  408.     {
  409.         if (!$this->freelancerComments->contains($freelancerComment)) {
  410.             $this->freelancerComments[] = $freelancerComment;
  411.             $freelancerComment->setAuthor($this);
  412.         }
  413.         return $this;
  414.     }
  415.     public function removeFreelancerComment(FreelancerComment $freelancerComment): self
  416.     {
  417.         if ($this->freelancerComments->contains($freelancerComment)) {
  418.             $this->freelancerComments->removeElement($freelancerComment);
  419.             // set the owning side to null (unless already changed)
  420.             if ($freelancerComment->getAuthor() === $this) {
  421.                 $freelancerComment->setAuthor(null);
  422.             }
  423.         }
  424.         return $this;
  425.     }
  426.     /**
  427.      * @return PaymentMethod|null
  428.      * @deprecated
  429.      */
  430.     public function getSepaPaymentMethod(): ?PaymentMethod
  431.     {
  432.         $methods $this->paymentMethods->filter(function (PaymentMethod $paymentMethod) {
  433.             return $paymentMethod->getType() == PaymentMethodTypeEnum::SEPA;
  434.         });
  435.         if (count($methods) > 0) {
  436.             return $methods[0];
  437.         } else {
  438.             return null;
  439.         }
  440.     }
  441.     /**
  442.      * @return bool
  443.      * @deprecated
  444.      */
  445.     public function hasSepaPaymentMethod(): bool
  446.     {
  447.         return !is_null($this->getSepaPaymentMethod());
  448.     }
  449.     /**
  450.      * @return Collection|PaymentMethod[]
  451.      */
  452.     public function getPaymentMethods(): Collection
  453.     {
  454.         return $this->paymentMethods;
  455.     }
  456.     public function addPaymentMethod(PaymentMethod $paymentMethod): self
  457.     {
  458.         if (!$this->paymentMethods->contains($paymentMethod)) {
  459.             $this->paymentMethods[] = $paymentMethod;
  460.             $paymentMethod->setUser($this);
  461.         }
  462.         return $this;
  463.     }
  464.     public function removePaymentMethod(PaymentMethod $paymentMethod): self
  465.     {
  466.         if ($this->paymentMethods->contains($paymentMethod)) {
  467.             $this->paymentMethods->removeElement($paymentMethod);
  468.             // set the owning side to null (unless already changed)
  469.             if ($paymentMethod->getUser() === $this) {
  470.                 $paymentMethod->setUser(null);
  471.             }
  472.         }
  473.         return $this;
  474.     }
  475.     public function getStoreToAssociateSepaWith(): ?Store
  476.     {
  477.         return $this->storeToAssociateSepaWith;
  478.     }
  479.     public function setStoreToAssociateSepaWith(?Store $storeToAssociateSepaWith): self
  480.     {
  481.         $this->storeToAssociateSepaWith $storeToAssociateSepaWith;
  482.         return $this;
  483.     }
  484.     public function clearStoreToAssociateSepaWith(): void
  485.     {
  486.         $this->setStoreToAssociateSepaWith(null);
  487.     }
  488.     /**
  489.      * @return mixed
  490.      */
  491.     public function getDefaultPaymentMethod()
  492.     {
  493.         return $this->defaultPaymentMethod;
  494.     }
  495.     /**
  496.      * @return boolean
  497.      */
  498.     public function hasDefaultPaymentMethod(): bool
  499.     {
  500.         return !is_null($this->defaultPaymentMethod);
  501.     }
  502.     /**
  503.      * @param mixed $defaultPaymentMethod
  504.      */
  505.     public function setDefaultPaymentMethod($defaultPaymentMethod): void
  506.     {
  507.         $this->defaultPaymentMethod $defaultPaymentMethod;
  508.     }
  509.     /**
  510.      * Can the user buy credit regarding al his possible status (freelancer, assistant, ...)
  511.      * @return bool
  512.      */
  513.     public function canBuyCredit(): bool
  514.     {
  515.         if ($this->isTypeFreelancerAssistant()) {
  516.             return $this->getFreelancerAssistant()->getFreelancer()->canBuyCredit();
  517.         } elseif ($this->isTypeFreelancer()) {
  518.             return $this->getAdministrator()->canBuyCredit();
  519.         } else {
  520.             return false;
  521.         }
  522.     }
  523.     public function setEmailAuthEnabled(bool $emailAuthEnabled): self
  524.     {
  525.         $this->emailAuthEnabled $emailAuthEnabled;
  526.         return $this;
  527.     }
  528.     public function isEmailAuthEnabled(): bool
  529.     {
  530.         return $this->emailAuthEnabled;
  531.     }
  532.     public function getEmailAuthRecipient(): string
  533.     {
  534.         return $this->email;
  535.     }
  536.     public function getEmailAuthCode(): string
  537.     {
  538.         if (null === $this->twoFactorAuthCode) {
  539.             throw new \LogicException('The email authentication code was not set');
  540.         }
  541.         return $this->twoFactorAuthCode;
  542.     }
  543.     public function setEmailAuthCode(string $authCode): void
  544.     {
  545.         $this->twoFactorAuthCode $authCode;
  546.     }
  547.     public function getTrustedTokenVersion(): int
  548.     {
  549.         return 48645;
  550.     }
  551.     public function getPrescriber(): ?Prescriber
  552.     {
  553.         return $this->prescriber;
  554.     }
  555.     public function isPrescripteur(): bool{
  556.         return !is_null($this->prescriber);
  557.     }
  558.     public function setPrescriber(Prescriber $prescriber): self
  559.     {
  560.         // set the owning side of the relation if necessary
  561.         if ($prescriber->getUser() !== $this) {
  562.             $prescriber->setUser($this);
  563.         }
  564.         $this->prescriber $prescriber;
  565.         return $this;
  566.     }
  567. }