src/Entity/User/User.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\User;
  3. use App\Entity\Company;
  4. use App\Entity\Offer\Offer;
  5. use App\Entity\SharedOffer;
  6. use App\Entity\Traits\IdTrait;
  7. use App\Exception\UserAccountBlockedException;
  8. use App\Exception\UserAccountNotActiveException;
  9. use DateTime;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use App\Repository\User\UserRepository;
  16. /**
  17.  * @ORM\Entity(repositoryClass=UserRepository::class)
  18.  */
  19. class User implements UserInterface, PasswordAuthenticatedUserInterface
  20. {
  21.     use IdTrait;
  22.     /**
  23.      * @ORM\Column(type="string", length=180, unique=true)
  24.      */
  25.     private $email;
  26.     /**
  27.      * @ORM\Column(type="json")
  28.      */
  29.     private $roles = [];
  30.     /**
  31.      * @var string The hashed password
  32.      * @ORM\Column(type="string")
  33.      */
  34.     private $password;
  35.     /**
  36.      * @ORM\Column(type="datetime")
  37.      */
  38.     private $createdAt;
  39.     /**
  40.      * @ORM\Column(type="datetime")
  41.      */
  42.     private $updatedAt;
  43.     /**
  44.      * @ORM\Column(type="boolean")
  45.      */
  46.     private $activated;
  47.     /**
  48.      * @ORM\Column(type="boolean")
  49.      */
  50.     private $blocked;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity=UserPasswordResetToken::class, mappedBy="user",cascade={"persist", "remove"})
  53.      */
  54.     private $userPasswordResetTokens;
  55.     /**
  56.      * @ORM\OneToMany(targetEntity=UserActivationToken::class, mappedBy="user",cascade={"persist", "remove"})
  57.      */
  58.     private $userActivationTokens;
  59.     /**
  60.      * @ORM\OneToMany(targetEntity=Company::class, mappedBy="user",cascade={"persist", "remove"})
  61.      */
  62.     private $company;
  63.     /**
  64.      * @ORM\OneToMany(targetEntity=SharedOffer::class, mappedBy="user")
  65.      */
  66.     private $sharedOffers;
  67.     public function __construct()
  68.     {
  69.         $this->userPasswordResetTokens  = new ArrayCollection();
  70.         $this->userActivationTokens     = new ArrayCollection();
  71.         $this->company                  = new ArrayCollection();
  72.         $this->sharedOffers = new ArrayCollection();
  73.     }
  74.     public function getId(): ?int
  75.     {
  76.         return $this->id;
  77.     }
  78.     public function getEmail(): ?string
  79.     {
  80.         return $this->email;
  81.     }
  82.     public function setEmail(string $email): self
  83.     {
  84.         $this->email = $email;
  85.         return $this;
  86.     }
  87.     /**
  88.      * A visual identifier that represents this user.
  89.      *
  90.      * @see UserInterface
  91.      */
  92.     public function getUserIdentifier(): string
  93.     {
  94.         return (string) $this->email;
  95.     }
  96.     /**
  97.      * @see UserInterface
  98.      */
  99.     public function getRoles(): array
  100.     {
  101.         $roles = $this->roles;
  102.         return array_unique($roles);
  103.     }
  104.     public function setRoles(array $roles): self
  105.     {
  106.         $this->roles = $roles;
  107.         return $this;
  108.     }
  109.     /**
  110.      * @see PasswordAuthenticatedUserInterface
  111.      */
  112.     public function getPassword(): string
  113.     {
  114.         return $this->password;
  115.     }
  116.     public function setPassword(string $password): self
  117.     {
  118.         $this->password = $password;
  119.         return $this;
  120.     }
  121.     /**
  122.      * @see UserInterface
  123.      */
  124.     public function eraseCredentials()
  125.     {
  126.         // If you store any temporary, sensitive data on the user, clear it here
  127.         // $this->plainPassword = null;
  128.     }
  129.     public function getCreatedAt(): ?\DateTimeInterface
  130.     {
  131.         return $this->createdAt;
  132.     }
  133.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  134.     {
  135.         $this->createdAt = $createdAt;
  136.         return $this;
  137.     }
  138.     public function getUpdatedAt(): ?\DateTimeInterface
  139.     {
  140.         return $this->updatedAt;
  141.     }
  142.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  143.     {
  144.         $this->updatedAt = $updatedAt;
  145.         return $this;
  146.     }
  147.     public function getActivated(): ?bool
  148.     {
  149.         return $this->activated;
  150.     }
  151.     public function setActivated(bool $activated): self
  152.     {
  153.         $this->activated = $activated;
  154.         return $this;
  155.     }
  156.     public function getBlocked(): ?bool
  157.     {
  158.         return $this->blocked;
  159.     }
  160.     public function setBlocked(bool $blocked): self
  161.     {
  162.         $this->blocked = $blocked;
  163.         return $this;
  164.     }
  165.     /**
  166.      * @return Collection|UserPasswordResetToken[]
  167.      */
  168.     public function getUserPasswordResetTokens(): Collection
  169.     {
  170.         return $this->userPasswordResetTokens;
  171.     }
  172.     public function addUserPasswordResetToken(UserPasswordResetToken $userPasswordResetToken): self
  173.     {
  174.         if (!$this->userPasswordResetTokens->contains($userPasswordResetToken)) {
  175.             $this->userPasswordResetTokens[] = $userPasswordResetToken;
  176.             $userPasswordResetToken->setUser($this);
  177.         }
  178.         return $this;
  179.     }
  180.     public function removeUserPasswordResetToken(UserPasswordResetToken $userPasswordResetToken): self
  181.     {
  182.         if ($this->userPasswordResetTokens->removeElement($userPasswordResetToken)) {
  183.             // set the owning side to null (unless already changed)
  184.             if ($userPasswordResetToken->getUser() === $this) {
  185.                 $userPasswordResetToken->setUser(null);
  186.             }
  187.         }
  188.         return $this;
  189.     }
  190.     /**
  191.      * @return Collection|UserActivationToken[]
  192.      */
  193.     public function getUserActivationTokens(): Collection
  194.     {
  195.         return $this->userActivationTokens;
  196.     }
  197.     public function addUserActivationToken(UserActivationToken $userActivationToken): self
  198.     {
  199.         if (!$this->userActivationTokens->contains($userActivationToken)) {
  200.             $this->userActivationTokens[] = $userActivationToken;
  201.             $userActivationToken->setUser($this);
  202.         }
  203.         return $this;
  204.     }
  205.     public function removeUserActivationToken(UserActivationToken $userActivationToken): self
  206.     {
  207.         if ($this->userActivationTokens->removeElement($userActivationToken)) {
  208.             // set the owning side to null (unless already changed)
  209.             if ($userActivationToken->getUser() === $this) {
  210.                 $userActivationToken->setUser(null);
  211.             }
  212.         }
  213.         return $this;
  214.     }
  215.     /**
  216.      * @return Collection|Company[]
  217.      */
  218.     public function getCompany():Collection
  219.     {
  220.         return $this->company;
  221.     }
  222.     public function addCompany(?Company &$company):self
  223.     {
  224.        if(!$this->company->contains($company))
  225.        {
  226.         $this->company[] = $company;
  227.         $company->setUser($this);
  228.        }
  229.        return $this;
  230.     }
  231.     public function removeCompany(?Company &$company):self
  232.     {
  233.         if($this->company->removeElement($company))
  234.         {
  235.             if($company->getUser() === $this)
  236.             {
  237.                 $company->setUser(null);
  238.             }
  239.         }
  240.         return $this;
  241.     }
  242.     public function checkIfActive(): void
  243.     {
  244.         $checkIfActive = false === $this->activated ? throw new UserAccountNotActiveException() : '';
  245.     }
  246.     public function checkIfBlocked(): void
  247.     {
  248.         $checkIfBlocked = true === $this->blocked ? throw new UserAccountBlockedException() : '';
  249.     }
  250.     public static function createUser(array $data): User
  251.     {
  252.         $user = new User();
  253.         $user->setEmail($data['email']);
  254.         $user->setRoles(["ROLE_USER"]);
  255.         $user->setPassword($data['hashedPassword']);
  256.         $user->setCreatedAt(new DateTime());
  257.         $user->setUpdatedAt(new DateTime());
  258.         $user->setActivated(0);
  259.         $user->setBlocked(0);
  260.         return $user;
  261.     }
  262.     /**
  263.      * @return Collection<int, SharedOffer>
  264.      */
  265.     public function getSharedOffers(): Collection
  266.     {
  267.         return $this->sharedOffers;
  268.     }
  269.     public function addSharedOffer(SharedOffer $sharedOffer): self
  270.     {
  271.         if (!$this->sharedOffers->contains($sharedOffer)) {
  272.             $this->sharedOffers[] = $sharedOffer;
  273.             $sharedOffer->setUserId($this);
  274.         }
  275.         return $this;
  276.     }
  277.     public function checkPermissionToEditOfferByOfferAndUser(?Offer $offer, ?User $user): bool
  278.     {
  279.         if (is_null($offer) || is_null($user)) {
  280.             return false;
  281.         }
  282.         foreach ($user->getSharedOffers() as $sharedOffer){
  283.             if ($sharedOffer->getUser() === $user && $sharedOffer->getOffer() === $offer) {
  284.                 $editPermission = (bool)($sharedOffer->getPrivileges()['EDITING_OFFER'] ?? false);
  285.                 if ($editPermission === true) {
  286.                     return true;
  287.                 }
  288.                 return false;
  289.             }
  290.         }
  291.         return false;
  292.     }
  293.     public function removeSharedOffer(SharedOffer $sharedOffer): self
  294.     {
  295.         if ($this->sharedOffers->removeElement($sharedOffer)) {
  296.             if ($sharedOffer->getUserId() === $this) {
  297.                 $sharedOffer->setUserId(null);
  298.             }
  299.         }
  300.         return $this;
  301.     }
  302.     public function getCompanyCountry(): ?string
  303.     {
  304.         $company = $this->company[0] ?? null;
  305.         if (is_null($company)) {
  306.            return null;
  307.         }
  308.         $invoiceData = $company->getInvoiceData();
  309.         if ($company->getIsExtraInvoiceData()) {
  310.             $invoiceData = $company->getExtraInvoiceData();
  311.         }
  312.         if (is_null($invoiceData)) {
  313.             return null;
  314.         }
  315.         $address = $invoiceData->getAddress();
  316.         if (is_null($address)) {
  317.             return null;
  318.         }
  319.         $country = $address->getCountry();
  320.         return is_null($country) === false ? strtolower($country) : null;
  321.     }
  322.     public function haveCompany(): bool
  323.     {
  324.         $company = $this->company[0] ?? null;
  325.         if (is_null($company)) {
  326.             return false;
  327.         }
  328.         $invoiceData = $company->getInvoiceData();
  329.         if (empty($invoiceData->getVatId()) || empty($invoiceData->getName())) {
  330.             return false;
  331.         }
  332.         return true;
  333.     }
  334. }