src/Entity/User.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Garages\Garage;
  4. use App\Entity\PointsCatalog\CatalogPointMovement;
  5. use App\Enum\UserRolesEnum;
  6. use App\Repository\UserRepository;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  13. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use Symfony\Component\Validator\Constraints\Email;
  17. /**
  18.  * @ORM\Table(name="vulco_user")
  19.  *
  20.  * @ORM\Entity(repositoryClass=UserRepository::class)
  21.  *
  22.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  23.  *
  24.  * https://symfony.com/doc/5.x/reference/constraints/UniqueEntity.html:
  25.  * (no he conseguit que funcioni)
  26.  * @UniqueEntity("email")
  27.  * @UniqueEntity("username")
  28.  */
  29. class User extends AbstractBase implements PasswordAuthenticatedUserInterfaceUserInterface
  30. {
  31.     /**
  32.      * @ORM\ManyToOne(targetEntity="App\Entity\UserGroup", inversedBy="users")
  33.      *
  34.      * @ORM\JoinColumn(name="user_group_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  35.      */
  36.     private ?UserGroup $userGroup null;
  37.     /**
  38.      * @ORM\OneToMany(targetEntity="App\Entity\Garages\Garage", mappedBy="owner")
  39.      */
  40.     private ?Collection $garages;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\Supplier", mappedBy="user")
  43.      */
  44.     private ?Collection $suppliers;
  45.     /**
  46.      * @ORM\ManyToMany(targetEntity="App\Entity\Province")
  47.      *
  48.      * @ORM\JoinTable(name="vulco_user_province",
  49.      *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  50.      *     inverseJoinColumns={@ORM\JoinColumn(name="province_id", referencedColumnName="id")}
  51.      * )
  52.      *
  53.      * @ORM\OrderBy({"name": "ASC"})
  54.      */
  55.     private ?Collection $workProvinces;
  56.     /**
  57.      * @ORM\OneToMany(targetEntity="App\Entity\Documents\DocumentDownload", mappedBy="user", orphanRemoval=true)
  58.      */
  59.     private ?Collection $documentDownloads;
  60.     /**
  61.      * @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\SupplierDocumentDownload", mappedBy="user", orphanRemoval=true)
  62.      */
  63.     private ?Collection $supplierDocumentDownloads;
  64.     /**
  65.      * @ORM\OneToMany(targetEntity="App\Entity\Tracking\UserTracking", mappedBy="user", orphanRemoval=true)
  66.      */
  67.     private ?Collection $userTrackings;
  68.     /**
  69.      * @ORM\OneToMany(targetEntity="App\Entity\PointsCatalog\CatalogPointMovement", mappedBy="user", orphanRemoval=true)
  70.      */
  71.     private ?Collection $pointMovements;
  72.     /**
  73.      * @ORM\Column(type="string", length=180, unique=true)
  74.      * @Assert\Email
  75.      */
  76.     private string $email;
  77.     /**
  78.      * @ORM\Column(type="string", length=180, nullable=true)
  79.      *
  80.      * inhereted from legacy vulco FOSUserBundle
  81.      */
  82.     private ?string $emailCanonical null;
  83.     /**
  84.      * @ORM\Column(type="string", length=180, unique=true)
  85.      */
  86.     private string $username;
  87.     /**
  88.      * @ORM\Column(type="string", length=180, nullable=true)
  89.      *
  90.      * inhereted from legacy vulco FOSUserBundle
  91.      */
  92.     private ?string $usernameCanonical null;
  93.     /**
  94.      * @ORM\Column(type="string", length=255, nullable=true)
  95.      */
  96.     private ?string $name;
  97.     /**
  98.      * @ORM\Column(type="string", length=255, nullable=true)
  99.      */
  100.     private ?string $zone;
  101.     /**
  102.      * @ORM\Column(type="array")
  103.      */
  104.     private array $roles = [];
  105.     /**
  106.      * @ORM\Column(type="string")
  107.      */
  108.     private string $password;
  109.     /**
  110.      * @ORM\Column(type="string", nullable=true)
  111.      */
  112.     private ?string $salt null;
  113.     /**
  114.      * @ORM\Column(type="datetime", name="last_login", nullable=true)
  115.      */
  116.     private ?\DateTimeInterface $lastLogin;
  117.     /**
  118.      * @ORM\Column(type="datetime", name="last_old_login", nullable=true)
  119.      */
  120.     private ?\DateTimeInterface $lastOldLogin;
  121.     /**
  122.      * @ORM\Column(type="datetime", name="last_visit_on", nullable=true)
  123.      */
  124.     private ?\DateTimeInterface $lastVisitOn;
  125.     /**
  126.      * @ORM\Column(type="datetime", name="password_requested_at", nullable=true)
  127.      */
  128.     private ?\DateTimeInterface $passwordRequestedAt;
  129.     /**
  130.      * @ORM\Column(type="string", name="internal_description", nullable=true)
  131.      */
  132.     private ?string $internalDescription;
  133.     /**
  134.      * @ORM\Column(type="boolean", name="enabled", nullable=false)
  135.      */
  136.     private bool $enabled true;
  137.     /**
  138.      * @ORM\Column(type="boolean", nullable=true, options={"default": 0})
  139.      */
  140.     private bool $enableStatisticsManagement false;
  141.     /**
  142.      * @ORM\Column(type="boolean", name="readed_confidentiality", nullable=false)
  143.      */
  144.     private bool $readedConfidentiality false;
  145.     /**
  146.      * @ORM\Column(type="datetime", name="readed_confidentiality_at", nullable=true)
  147.      */
  148.     private ?\DateTimeInterface $readedConfidentialityAt;
  149.     /**
  150.      * @ORM\Column(type="string", name="login_token", nullable=true)
  151.      */
  152.     private ?string $token null;
  153.     /**
  154.      * @ORM\Column(type="string", name="confirmation_token", length=180, nullable=true, unique=true)
  155.      */
  156.     private string $confirmationToken;
  157.     public function __construct()
  158.     {
  159.         $this->createdAt = new \DateTime();
  160.         $this->garages = new ArrayCollection();
  161.         $this->suppliers = new ArrayCollection();
  162.         $this->workProvinces = new ArrayCollection();
  163.         $this->documentDownloads = new ArrayCollection();
  164.         $this->supplierDocumentDownloads = new ArrayCollection();
  165.         $this->userTrackings = new ArrayCollection();
  166.         $this->pointMovements = new ArrayCollection();
  167.     }
  168.     public function getUserIdentifier(): string
  169.     {
  170.         return $this->getUsername();
  171.     }
  172.     public function getUserGroup(): ?UserGroup
  173.     {
  174.         return $this->userGroup;
  175.     }
  176.     public function setUserGroup(?UserGroup $userGroup): self
  177.     {
  178.         $this->userGroup $userGroup;
  179.         return $this;
  180.     }
  181.     public function getGarages(): ?Collection
  182.     {
  183.         return $this->garages;
  184.     }
  185.     public function setGarages(?Collection $garages): self
  186.     {
  187.         $this->garages $garages;
  188.         return $this;
  189.     }
  190.     public function addGarage(Garage $garage): self
  191.     {
  192.         if (!$this->garages->contains($garage)) {
  193.             $this->garages->add($garage);
  194.         }
  195.         return $this;
  196.     }
  197.     public function getSuppliers(): ?Collection
  198.     {
  199.         return $this->suppliers;
  200.     }
  201.     public function setSuppliers($suppliers): self
  202.     {
  203.         $this->suppliers $suppliers;
  204.         return $this;
  205.     }
  206.     public function getWorkProvinces(): ?Collection
  207.     {
  208.         return $this->workProvinces;
  209.     }
  210.     public function setWorkProvinces(?Collection $workProvinces): self
  211.     {
  212.         $this->workProvinces $workProvinces;
  213.         return $this;
  214.     }
  215.     public function getDocumentDownloads(): ?Collection
  216.     {
  217.         return $this->documentDownloads;
  218.     }
  219.     public function setDocumentDownloads(?Collection $documentDownloads): self
  220.     {
  221.         $this->documentDownloads $documentDownloads;
  222.         return $this;
  223.     }
  224.     public function getSupplierDocumentDownloads(): ?Collection
  225.     {
  226.         return $this->supplierDocumentDownloads;
  227.     }
  228.     public function setSupplierDocumentDownloads(?Collection $supplierDocumentDownloads): self
  229.     {
  230.         $this->supplierDocumentDownloads $supplierDocumentDownloads;
  231.         return $this;
  232.     }
  233.     public function getUserTrackings(): ?Collection
  234.     {
  235.         return $this->userTrackings;
  236.     }
  237.     public function setUserTrackings(?Collection $userTrackings): self
  238.     {
  239.         $this->userTrackings $userTrackings;
  240.         return $this;
  241.     }
  242.     public function getPointMovements(): ?Collection
  243.     {
  244.         return $this->pointMovements;
  245.     }
  246.     public function setPointMovements(?Collection $pointMovements): self
  247.     {
  248.         $this->pointMovements $pointMovements;
  249.         return $this;
  250.     }
  251.     public function getNameOrEmail(): string
  252.     {
  253.         return $this->getName() ?: $this->getEmail();
  254.     }
  255.     public function getNameAndEmailString(): string
  256.     {
  257.         return $this->getName() ? $this->getName().' ('.$this->getEmail().')' $this->getEmail();
  258.     }
  259.     public function getUsername(): string
  260.     {
  261.         return $this->username;
  262.     }
  263.     public function setUsername(string $username): self
  264.     {
  265.         $this->username $username;
  266.         return $this;
  267.     }
  268.     public function getRoles(): array
  269.     {
  270.         $roles $this->roles;
  271.         $roles[] = UserRolesEnum::ROLE_USER_LONG// guarantee every user at least has ROLE_USER
  272.         return array_unique($roles);
  273.     }
  274.     public function setRoles(array $roles): self
  275.     {
  276.         $this->roles $roles;
  277.         return $this;
  278.     }
  279.     public function hasRole(string $role): bool
  280.     {
  281.         return in_array(strtoupper($role), $this->rolestrue);
  282.     }
  283.     public function addRole(string $role): self
  284.     {
  285.         if (!$this->hasRole($role)) {
  286.             $this->roles[] = strtoupper($role);
  287.         }
  288.         return $this;
  289.     }
  290.     public function removeRole(string $role): self
  291.     {
  292.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  293.             unset($this->roles[$key]);
  294.             $this->roles array_values($this->roles);
  295.         }
  296.         return $this;
  297.     }
  298.     public function getUserRole()
  299.     {
  300.         $role '';
  301.         if ($this->hasRole(UserRolesEnum::ROLE_ADMIN_LONG) || $this->hasRole(UserRolesEnum::ROLE_SUPER_ADMIN_LONG)) {
  302.             $role UserRolesEnum::ROLE_ADMIN;
  303.         } elseif ($this->hasRole(UserRolesEnum::ROLE_MANAGER_LONG)) {
  304.             $role UserRolesEnum::ROLE_MANAGER;
  305.         } elseif ($this->hasRole(UserRolesEnum::ROLE_ASSOCIATED_LONG)) {
  306.             $role UserRolesEnum::ROLE_ASSOCIATED;
  307.         } elseif ($this->hasRole(UserRolesEnum::ROLE_ASSOCIATED_MANAGER_LONG)) {
  308.             $role UserRolesEnum::ROLE_ASSOCIATED_MANAGER;
  309.         } elseif ($this->hasRole(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
  310.             $role UserRolesEnum::ROLE_COORDINATOR;
  311.         } elseif ($this->hasRole(UserRolesEnum::ROLE_PROVIDER_LONG)) {
  312.             $role UserRolesEnum::ROLE_PROVIDER;
  313.         } elseif ($this->hasRole(UserRolesEnum::ROLE_QUALITY_ADVISOR_LONG)) {
  314.             $role UserRolesEnum::ROLE_QUALITY_ADVISOR;
  315.         }
  316.         return $role;
  317.     }
  318.     public function getPassword(): string
  319.     {
  320.         return $this->password;
  321.     }
  322.     public function setPassword(string $password): self
  323.     {
  324.         $this->password $password;
  325.         return $this;
  326.     }
  327.     public function getToken(): ?string
  328.     {
  329.         return $this->token;
  330.     }
  331.     public function setToken(string $token): self
  332.     {
  333.         $this->token $token;
  334.         return $this;
  335.     }
  336.     public function getSalt(): ?string
  337.     {
  338.         return $this->salt;
  339.     }
  340.     public function setSalt(?string $salt): self
  341.     {
  342.         $this->salt $salt;
  343.         return $this;
  344.     }
  345.     public function eraseCredentials(): void
  346.     {
  347.         // If you store any temporary, sensitive data on the user, clear it here
  348.         // $this->plainPassword = null;
  349.     }
  350.     public function getUsernameCanonical(): ?string
  351.     {
  352.         return $this->usernameCanonical;
  353.     }
  354.     public function setUsernameCanonical(?string $usernameCanonical): self
  355.     {
  356.         $this->usernameCanonical $usernameCanonical;
  357.         return $this;
  358.     }
  359.     public function getEmail(): string
  360.     {
  361.         return $this->email;
  362.     }
  363.     public function setEmail(string $email): self
  364.     {
  365.         $this->email strtolower($email);
  366.         return $this;
  367.     }
  368.     public function getEmailCanonical(): ?string
  369.     {
  370.         return $this->emailCanonical;
  371.     }
  372.     public function setEmailCanonical(?string $emailCanonical): self
  373.     {
  374.         $this->emailCanonical $emailCanonical;
  375.         return $this;
  376.     }
  377.     public function getName(): ?string
  378.     {
  379.         return $this->name;
  380.     }
  381.     public function setName(?string $name): self
  382.     {
  383.         $this->name $name;
  384.         return $this;
  385.     }
  386.     public function getZone(): ?string
  387.     {
  388.         return $this->zone;
  389.     }
  390.     public function setZone(?string $zone): self
  391.     {
  392.         $this->zone $zone;
  393.         return $this;
  394.     }
  395.     public function getLastLogin(): ?\DateTimeInterface
  396.     {
  397.         return $this->lastLogin;
  398.     }
  399.     public function setLastLogin(?\DateTimeInterface $lastLogin): self
  400.     {
  401.         $this->lastLogin $lastLogin;
  402.         return $this;
  403.     }
  404.     public function getLastOldLogin(): ?\DateTimeInterface
  405.     {
  406.         return $this->lastOldLogin;
  407.     }
  408.     public function setLastOldLogin(?\DateTimeInterface $lastOldLogin): self
  409.     {
  410.         $this->lastOldLogin $lastOldLogin;
  411.         return $this;
  412.     }
  413.     public function getLastVisitOn(): ?\DateTimeInterface
  414.     {
  415.         return $this->lastVisitOn;
  416.     }
  417.     public function setLastVisitOn(?\DateTimeInterface $lastVisitOn): self
  418.     {
  419.         $this->lastVisitOn $lastVisitOn;
  420.         return $this;
  421.     }
  422.     public function getPasswordRequestedAt(): ?\DateTimeInterface
  423.     {
  424.         return $this->passwordRequestedAt;
  425.     }
  426.     public function setPasswordRequestedAt(?\DateTimeInterface $passwordRequestedAt): self
  427.     {
  428.         $this->passwordRequestedAt $passwordRequestedAt;
  429.         return $this;
  430.     }
  431.     public function getInternalDescription(): ?string
  432.     {
  433.         return $this->internalDescription;
  434.     }
  435.     public function setInternalDescription(?string $internalDescription): self
  436.     {
  437.         $this->internalDescription $internalDescription;
  438.         return $this;
  439.     }
  440.     public function isEnabled(): bool
  441.     {
  442.         return $this->enabled;
  443.     }
  444.     public function getEnabled(): bool
  445.     {
  446.         return $this->isEnabled();
  447.     }
  448.     public function setEnabled(bool $enabled): self
  449.     {
  450.         $this->enabled $enabled;
  451.         return $this;
  452.     }
  453.     public function isEnableStatisticsManagement(): bool
  454.     {
  455.         return $this->enableStatisticsManagement;
  456.     }
  457.     public function getEnableStatisticsManagement(): bool
  458.     {
  459.         return $this->isEnableStatisticsManagement();
  460.     }
  461.     public function setEnableStatisticsManagement(bool $enableStatisticsManagement): self
  462.     {
  463.         $this->enableStatisticsManagement $enableStatisticsManagement;
  464.         return $this;
  465.     }
  466.     public function isReadedConfidentiality(): bool
  467.     {
  468.         return $this->readedConfidentiality;
  469.     }
  470.     public function setReadedConfidentiality(bool $readedConfidentiality): self
  471.     {
  472.         $this->readedConfidentiality $readedConfidentiality;
  473.         return $this;
  474.     }
  475.     public function getReadedConfidentialityAt(): ?\DateTimeInterface
  476.     {
  477.         return $this->readedConfidentialityAt;
  478.     }
  479.     public function setReadedConfidentialityAt(?\DateTimeInterface $readedConfidentialityAt): self
  480.     {
  481.         $this->readedConfidentialityAt $readedConfidentialityAt;
  482.         return $this;
  483.     }
  484.     public function getConfirmationToken(): string
  485.     {
  486.         return $this->confirmationToken;
  487.     }
  488.     public function setConfirmationToken(string $confirmationToken): self
  489.     {
  490.         $this->confirmationToken $confirmationToken;
  491.         return $this;
  492.     }
  493.     public function belongsToUserGroup($userGroups): bool
  494.     {
  495.         $result false;
  496.         if (null !== $userGroups && null !== $this->getUserGroup()) {
  497.             /** @var UserGroup $userGroup */
  498.             foreach ($userGroups as $userGroup) {
  499.                 if ($userGroup->getId() === $this->getUserGroup()->getId()) {
  500.                     $result true;
  501.                     break;
  502.                 }
  503.             }
  504.         }
  505.         return $result;
  506.     }
  507.     public function belongsToUsers($users): bool
  508.     {
  509.         $result false;
  510.         if (null !== $users) {
  511.             /** @var User $user */
  512.             foreach ($users as $user) {
  513.                 if ($user->getId() === $this->getId()) {
  514.                     $result true;
  515.                     break;
  516.                 }
  517.             }
  518.         }
  519.         return $result;
  520.     }
  521.     public function getCatalogPointsAmount(): float
  522.     {
  523.         $catalogPointsAmount 0.0;
  524.         /** @var CatalogPointMovement $pointMovement */
  525.         foreach ($this->pointMovements as $pointMovement) {
  526.             $catalogPointsAmount += $pointMovement->getPoints();
  527.         }
  528.         return $catalogPointsAmount;
  529.     }
  530.     public function getCatalogPointsAmountUntilDate(DateTime $date): float
  531.     {
  532.         $catalogPointsAmount 0.0;
  533.         $dateStr $date->format('Y-m-d');
  534.         /** @var CatalogPointMovement $pointMovement */
  535.         foreach ($this->pointMovements as $pointMovement) {
  536.             $pointMovementDateStr $pointMovement->getDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  537.             if (strtotime($pointMovementDateStr) <= strtotime($dateStr)) {
  538.                 $catalogPointsAmount += $pointMovement->getPoints();
  539.             }
  540.         }
  541.         return $catalogPointsAmount;
  542.     }
  543.     public function getOwnerEarnedPointsWithinCalendarYear(\DateTimeInterface $date): float
  544.     {
  545.         $catalogPointsAmount 0.0;
  546.         $firstDate \DateTimeImmutable::createFromFormat(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT$date->format('Y').'-01-01');
  547.         $lastDate \DateTimeImmutable::createFromFormat(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT$date->format('Y').'-12-31');
  548.         $firstDateYearStr $firstDate->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  549.         $lastDateYearStr $lastDate->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  550.         /** @var CatalogPointMovement $pointMovement */
  551.         foreach ($this->pointMovements as $pointMovement) {
  552.             // if the pointMovement date is within the given calendar year
  553.             $pointMovementDateStr $pointMovement->getDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  554.             // si son puntos positivos (los que gasta son negativos) y no es una devolucion
  555.             if ($pointMovementDateStr >= $firstDateYearStr && $pointMovementDateStr <= $lastDateYearStr && !$pointMovement->isReturnMovement() && ($pointMovement->getPoints() > 0.00001)) {
  556.                 $catalogPointsAmount += $pointMovement->getPoints();
  557.             }
  558.         }
  559.         return $catalogPointsAmount;
  560.     }
  561.     public function getGarageEarnedPointsWithinCalendarYear(Garage $garage\DateTimeInterface $date): float
  562.     {
  563.         $catalogPointsAmount 0.0;
  564.         $firstDate \DateTimeImmutable::createFromFormat(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT$date->format('Y').'-01-01');
  565.         $lastDate \DateTimeImmutable::createFromFormat(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT$date->format('Y').'-12-31');
  566.         $firstDateYearStr $firstDate->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  567.         $lastDateYearStr $lastDate->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  568.         /** @var CatalogPointMovement $pointMovement */
  569.         foreach ($this->pointMovements as $pointMovement) {
  570.             // if the pointMovement belongs to the given garage
  571.             if ($pointMovement->getGarage() && $pointMovement->getGarage()->getId() === $garage->getId()) {
  572.                 // if the pointMovement date is within the given calendar year
  573.                 $pointMovementDateStr $pointMovement->getDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT);
  574.                 // si son puntos positivos (los que gasta son negativos) y no es una devolucion
  575.                 if ($pointMovementDateStr >= $firstDateYearStr && $pointMovementDateStr <= $lastDateYearStr && !$pointMovement->isReturnMovement() && ($pointMovement->getPoints() > 0.00001)) {
  576.                     $catalogPointsAmount += $pointMovement->getPoints();
  577.                 }
  578.             }
  579.         }
  580.         return $catalogPointsAmount;
  581.     }
  582.     public function __toString(): string
  583.     {
  584.         return $this->id $this->getUsername() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
  585.     }
  586. }