src/Entity/User.php line 23

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