src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Serializer\Annotation\Groups// Add this use statement
  11. #[ORM\Entity(repositoryClassUserRepository::class)]
  12. #[ORM\Table(name'`user`')]
  13. #[UniqueEntity(fields: ['email'], message'There is already an account with this email')]
  14. class User implements UserInterfacePasswordAuthenticatedUserInterface
  15. {
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     #[Groups(["chat:channel:list""chat_message"])] // Add group
  20.     private ?int $id null;
  21.     #[ORM\Column(length180uniquetrue)]
  22.     private ?string $email null;
  23.     #[ORM\Column]
  24.     private array $roles = [];
  25.     /**
  26.      * @var string The hashed password
  27.      */
  28.     #[ORM\Column]
  29.     private ?string $password null;
  30.     #[ORM\Column(length255)]
  31.     #[Groups(["chat:channel:list""chat_message"])] // Add group
  32.     private ?string $firstName null;
  33.     #[ORM\Column(length255)]
  34.     #[Groups(["chat:channel:list""chat_message"])] // Add group
  35.     private ?string $lastName null;
  36.     #[ORM\Column]
  37.     private ?\DateTimeImmutable $createdAt null;
  38.     #[ORM\ManyToOne(inversedBy'users')]
  39.     private ?Company $company null;
  40.     #[ORM\OneToOne(mappedBy'user'cascade: ['persist''remove'])]
  41.     #[Groups(["chat_message"])] // Add serialization group to expose profile
  42.     private ?UserProfile $profile null;
  43.     #[ORM\OneToMany(mappedBy'user'targetEntityUserTeam::class, orphanRemovaltrue)]
  44.     private Collection $userTeams;
  45.     #[ORM\OneToMany(mappedBy'user'targetEntitySubmission::class)]
  46.     private Collection $submissions;
  47.     #[ORM\OneToMany(mappedBy'user'targetEntityChatMessage::class)]
  48.     private Collection $chatMessages;
  49.     #[ORM\OneToMany(mappedBy'user'targetEntityNotification::class)]
  50.     private Collection $notifications;
  51.     public function __toString(): string
  52.     {
  53.         return $this->getFullName();
  54.     }
  55.     public function __construct()
  56.     {
  57.         $this->createdAt = new \DateTimeImmutable();
  58.         $this->userTeams = new ArrayCollection();
  59.         $this->submissions = new ArrayCollection();
  60.         $this->chatMessages = new ArrayCollection();
  61.         $this->notifications = new ArrayCollection();
  62.     }
  63.     public function getId(): ?int
  64.     {
  65.         return $this->id;
  66.     }
  67.     public function getEmail(): ?string
  68.     {
  69.         return $this->email;
  70.     }
  71.     public function setEmail(string $email): static
  72.     {
  73.         $this->email $email;
  74.         return $this;
  75.     }
  76.     /**
  77.      * A visual identifier that represents this user.
  78.      *
  79.      * @see UserInterface
  80.      */
  81.     public function getUserIdentifier(): string
  82.     {
  83.         return (string) $this->email;
  84.     }
  85.     /**
  86.      * @see UserInterface
  87.      */
  88.     public function getRoles(): array
  89.     {
  90.         $roles $this->roles;
  91.         // guarantee every user at least has ROLE_USER
  92.         $roles[] = 'ROLE_USER';
  93.         return array_unique($roles);
  94.     }
  95.     public function setRoles(array $roles): static
  96.     {
  97.         $this->roles $roles;
  98.         return $this;
  99.     }
  100.     /**
  101.      * @see PasswordAuthenticatedUserInterface
  102.      */
  103.     public function getPassword(): string
  104.     {
  105.         return $this->password;
  106.     }
  107.     public function setPassword(string $password): static
  108.     {
  109.         $this->password $password;
  110.         return $this;
  111.     }
  112.     /**
  113.      * @see UserInterface
  114.      */
  115.     public function eraseCredentials(): void
  116.     {
  117.         // If you store any temporary, sensitive data on the user, clear it here
  118.         // $this->plainPassword = null;
  119.     }
  120.     public function getFirstName(): ?string
  121.     {
  122.         return $this->firstName;
  123.     }
  124.     public function setFirstName(string $firstName): static
  125.     {
  126.         $this->firstName $firstName;
  127.         return $this;
  128.     }
  129.     public function getLastName(): ?string
  130.     {
  131.         return $this->lastName;
  132.     }
  133.     public function setLastName(string $lastName): static
  134.     {
  135.         $this->lastName $lastName;
  136.         return $this;
  137.     }
  138.     public function getCreatedAt(): ?\DateTimeImmutable
  139.     {
  140.         return $this->createdAt;
  141.     }
  142.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  143.     {
  144.         $this->createdAt $createdAt;
  145.         return $this;
  146.     }
  147.     #[Groups(["chat:channel:list""chat_message"])] // Add group to the getter for fullName
  148.     public function getFullName(): string
  149.     {
  150.         return $this->firstName ' ' $this->lastName;
  151.     }
  152.     public function getCompany(): ?Company
  153.     {
  154.         return $this->company;
  155.     }
  156.     public function setCompany(?Company $company): static
  157.     {
  158.         $this->company $company;
  159.         return $this;
  160.     }
  161.     public function getProfile(): ?UserProfile
  162.     {
  163.         return $this->profile;
  164.     }
  165.     public function setProfile(UserProfile $profile): static
  166.     {
  167.         // set the owning side of the relation if necessary
  168.         if ($profile->getUser() !== $this) {
  169.             $profile->setUser($this);
  170.         }
  171.         $this->profile $profile;
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return Collection<int, UserTeam>
  176.      */
  177.     public function getUserTeams(): Collection
  178.     {
  179.         return $this->userTeams;
  180.     }
  181.     public function addUserTeam(UserTeam $userTeam): static
  182.     {
  183.         if (!$this->userTeams->contains($userTeam)) {
  184.             $this->userTeams->add($userTeam);
  185.             $userTeam->setUser($this);
  186.         }
  187.         return $this;
  188.     }
  189.     public function removeUserTeam(UserTeam $userTeam): static
  190.     {
  191.         if ($this->userTeams->removeElement($userTeam)) {
  192.             // set the owning side to null (unless already changed)
  193.             if ($userTeam->getUser() === $this) {
  194.                 $userTeam->setUser(null);
  195.             }
  196.         }
  197.         return $this;
  198.     }
  199.     /**
  200.      * @return Collection<int, Submission>
  201.      */
  202.     public function getSubmissions(): Collection
  203.     {
  204.         return $this->submissions;
  205.     }
  206.     public function addSubmission(Submission $submission): static
  207.     {
  208.         if (!$this->submissions->contains($submission)) {
  209.             $this->submissions->add($submission);
  210.             $submission->setUser($this);
  211.         }
  212.         return $this;
  213.     }
  214.     public function removeSubmission(Submission $submission): static
  215.     {
  216.         if ($this->submissions->removeElement($submission)) {
  217.             // set the owning side to null (unless already changed)
  218.             if ($submission->getUser() === $this) {
  219.                 $submission->setUser(null);
  220.             }
  221.         }
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return Collection<int, ChatMessage>
  226.      */
  227.     public function getChatMessages(): Collection
  228.     {
  229.         return $this->chatMessages;
  230.     }
  231.     public function addChatMessage(ChatMessage $chatMessage): static
  232.     {
  233.         if (!$this->chatMessages->contains($chatMessage)) {
  234.             $this->chatMessages->add($chatMessage);
  235.             $chatMessage->setUser($this);
  236.         }
  237.         return $this;
  238.     }
  239.     public function removeChatMessage(ChatMessage $chatMessage): static
  240.     {
  241.         if ($this->chatMessages->removeElement($chatMessage)) {
  242.             // set the owning side to null (unless already changed)
  243.             if ($chatMessage->getUser() === $this) {
  244.                 $chatMessage->setUser(null);
  245.             }
  246.         }
  247.         return $this;
  248.     }
  249.     /**
  250.      * @return Collection<int, Notification>
  251.      */
  252.     public function getNotifications(): Collection
  253.     {
  254.         return $this->notifications;
  255.     }
  256.     public function addNotification(Notification $notification): static
  257.     {
  258.         if (!$this->notifications->contains($notification)) {
  259.             $this->notifications->add($notification);
  260.             $notification->setUser($this);
  261.         }
  262.         return $this;
  263.     }
  264.     public function removeNotification(Notification $notification): static
  265.     {
  266.         if ($this->notifications->removeElement($notification)) {
  267.             // set the owning side to null (unless already changed)
  268.             if ($notification->getUser() === $this) {
  269.                 $notification->setUser(null);
  270.             }
  271.         }
  272.         return $this;
  273.     }
  274.     /**
  275.      * Get all teams the user is a member of
  276.      *
  277.      * @return Collection<int, Team>
  278.      */
  279.     public function getTeams(): Collection
  280.     {
  281.         $teams = new ArrayCollection();
  282.         
  283.         foreach ($this->userTeams as $userTeam) {
  284.             $teams->add($userTeam->getTeam());
  285.         }
  286.         
  287.         return $teams;
  288.     }
  289.     /**
  290.      * Check if user is a member of a specific team
  291.      */
  292.     public function isMemberOfTeam(Team $team): bool
  293.     {
  294.         foreach ($this->userTeams as $userTeam) {
  295.             if ($userTeam->getTeam() === $team) {
  296.                 return true;
  297.             }
  298.         }
  299.         
  300.         return false;
  301.     }
  302.     /**
  303.      * Check if user is a leader of a specific team
  304.      */
  305.     public function isLeaderOfTeam(Team $team): bool
  306.     {
  307.         foreach ($this->userTeams as $userTeam) {
  308.             if ($userTeam->getTeam() === $team && $userTeam->getRole() === 'leader') {
  309.                 return true;
  310.             }
  311.         }
  312.         
  313.         return false;
  314.     }
  315.     
  316.     /**
  317.      * Get the user's primary team (first team found)
  318.      */
  319.     public function getUserTeam(): ?UserTeam
  320.     {
  321.         if ($this->userTeams->isEmpty()) {
  322.             return null;
  323.         }
  324.         
  325.         return $this->userTeams->first();
  326.     }
  327.     
  328.     /**
  329.      * Get the user's primary team entity
  330.      */
  331.     public function getTeam(): ?Team
  332.     {
  333.         $userTeam $this->getUserTeam();
  334.         return $userTeam $userTeam->getTeam() : null;
  335.     }
  336.     /**
  337.      * Get total steps for a specific season
  338.      */
  339.     public function getTotalStepsForSeason(Season $season): int
  340.     {
  341.         $totalSteps 0;
  342.         foreach ($this->submissions as $submission) {
  343.             // Only count approved submissions for this season
  344.             if ($submission->getSeason() === $season &&
  345.                 $submission->getStatus() === 'approved' &&
  346.                 $submission->getActivityData() !== null) {
  347.                 $activityData $submission->getActivityData();
  348.                 if (isset($activityData['steps'])) {
  349.                     $totalSteps += (int) $activityData['steps'];
  350.                 }
  351.             }
  352.         }
  353.         return $totalSteps;
  354.     }
  355.     /**
  356.      * Get lifetime total steps across all seasons
  357.      */
  358.     public function getLifetimeTotalSteps(): int
  359.     {
  360.         $totalSteps 0;
  361.         foreach ($this->submissions as $submission) {
  362.             // Only count approved submissions
  363.             if ($submission->getStatus() === 'approved' &&
  364.                 $submission->getActivityData() !== null) {
  365.                 $activityData $submission->getActivityData();
  366.                 if (isset($activityData['steps'])) {
  367.                     $totalSteps += (int) $activityData['steps'];
  368.                 }
  369.             }
  370.         }
  371.         return $totalSteps;
  372.     }
  373. }