src/Entity/User.php line 29

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\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. /**
  13.  * @ORM\Entity(repositoryClass=UserRepository::class)
  14.  * @UniqueEntity(
  15.  *     fields={"username"},
  16.  *     errorPath="username",
  17.  *     message="This username is already used"
  18.  * )
  19.  *  @UniqueEntity(
  20.  *     fields={"email"},
  21.  *     errorPath="email",
  22.  *     message="This email is already used"
  23.  * )
  24.  * @ORM\Table(name="`user`")
  25.  */
  26. class User implements UserInterfacePasswordAuthenticatedUserInterface
  27. {
  28.     /**
  29.      * @ORM\Id
  30.      * @ORM\GeneratedValue
  31.      * @ORM\Column(type="integer")
  32.      */
  33.     private $id;
  34.     /**
  35.      * @ORM\Column(type="string", length=50, unique=true)
  36.      * @Assert\NotBlank()
  37.      * @Assert\Email(
  38.      *     message = "The email '{{ value }}' is not valid."
  39.      * )
  40.      */    
  41.     private $email;
  42.     /**
  43.      * @ORM\Column(type="json")
  44.      */
  45.     private $roles = [];
  46.     /**
  47.      * @var string The hashed password
  48.      * @ORM\Column(type="string")
  49.      * @Assert\NotBlank(groups={"password"})
  50.      * @Assert\Length(
  51.      *      min = 4,
  52.      *      max = 15,
  53.      *      minMessage = "Your password must be at least {{ limit }} characters",
  54.      *      maxMessage = "Your password must be at most {{ limit }} characters",
  55.      *      groups={"password"}
  56.      * )
  57.      */
  58.     private $password;
  59.     /**
  60.      * @ORM\Column(type="string", length=20)
  61.      * @Assert\NotBlank()
  62.      * @Assert\Regex(
  63.      *     pattern = "/^(?=.{3,15}$)(?![_.-])(?!.*[_.-]{2})[a-zA-Z0-9_-]+([^._-])$/",
  64.      *     message = "Your username is invalid"
  65.      * )
  66.      * @Assert\Length(
  67.      *      min = 3,
  68.      *      max = 15,
  69.      *      minMessage = "Your username must be at least {{ limit }} characters",
  70.      *      maxMessage = "Your username must be at most {{ limit }} characters"
  71.      * )
  72.      */
  73.     private $username;
  74.     /**
  75.      * @ORM\Column(type="datetime_immutable")
  76.      */
  77.     private $created_at;
  78.     public function __construct()
  79.     {
  80.         $this->last_login = new \DateTime();
  81.         $this->created_at = new \DateTimeImmutable();
  82.         $this->photos = new ArrayCollection();
  83.         $this->photoLikes = new ArrayCollection();
  84.         $this->influencerFollowers = new ArrayCollection();
  85.         $this->comments = new ArrayCollection();
  86.         $this->videos = new ArrayCollection();
  87.         $this->profil_comments = new ArrayCollection();
  88.         $this->notifications = new ArrayCollection();
  89.         $this->setPicture(false);
  90.         $this->setNewsletter(true);
  91.         $this->setMailNotification(true);
  92.         $this->setImage(null);
  93.         $this->setRoles(['ROLE_USER']);        
  94.     }
  95.     /**
  96.      * @ORM\Column(type="datetime")
  97.      */
  98.     private $last_login;
  99.     /**
  100.      * @ORM\OneToMany(targetEntity=Photo::class, mappedBy="user", orphanRemoval=true)
  101.      */
  102.     private $photos;
  103.     /**
  104.      * @Assert\NotBlank()
  105.      */
  106.     private $agree_terms;
  107.     /**
  108.      * @ORM\OneToMany(targetEntity=PhotoLike::class, mappedBy="user", orphanRemoval=true)
  109.      */
  110.     private $photoLikes;
  111.     /**
  112.      * @ORM\OneToMany(targetEntity=InfluencerFollower::class, mappedBy="user", orphanRemoval=true)
  113.      */
  114.     private $influencerFollowers;
  115.     /**
  116.      * @ORM\Column(type="string", length=255, nullable=true)
  117.      */
  118.     private $ip;
  119.     /**
  120.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="user", orphanRemoval=true)
  121.      */
  122.     private $comments;
  123.     /**
  124.      * @ORM\Column(type="string", length=255, nullable=true)
  125.      */
  126.     private $country;
  127.     /**
  128.      * @Assert\Length(
  129.      *      min = 3,
  130.      *      max = 250,
  131.      *      minMessage = "Your bio must be at least {{ limit }} characters long",
  132.      *      maxMessage = "Your bio cannot be longer than {{ limit }} characters"
  133.      * )    
  134.      * @ORM\Column(type="string", length=255, nullable=true)
  135.      */
  136.     private $bio;
  137.     /**
  138.      * @Assert\Image(
  139.      *     minWidth = 70,
  140.      *     minHeight = 70,
  141.      *     minWidthMessage = "Your photo is too small",
  142.      *     minHeightMessage = "Your photo is too small",
  143.      *     maxSize = "5M",
  144.      *     maxSizeMessage = "The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}"
  145.      * )     
  146.      * @Assert\Valid()
  147.      */
  148.     private $image;
  149.     /**
  150.      * @ORM\Column(type="boolean", nullable=true)
  151.      */
  152.     private $picture;
  153.     /**
  154.      * @ORM\OneToMany(targetEntity=Video::class, mappedBy="user", orphanRemoval=true)
  155.      */
  156.     private $videos;
  157.     /**
  158.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="to_user")
  159.      */
  160.     private $profil_comments;
  161.     /**
  162.      * @ORM\OneToMany(targetEntity=Notification::class, mappedBy="user", orphanRemoval=true)
  163.      */
  164.     private $notifications;
  165.     /**
  166.      * @ORM\Column(type="boolean", nullable=true)
  167.      */
  168.     private $newsletter;
  169.     /**
  170.      * @ORM\Column(type="boolean", nullable=true)
  171.      */
  172.     private $mail_notification;
  173.     /**
  174.      * @ORM\Column(type="smallint", options={"default": 0})
  175.      */
  176.     private $trust_level 0;
  177.     /**
  178.      * @ORM\Column(type="integer", options={"default": 0})
  179.      */
  180.     private $upload_count 0;
  181.     /**
  182.      * @ORM\Column(type="integer", options={"default": 0})
  183.      */
  184.     private $approved_count 0;
  185.     /**
  186.      * @ORM\Column(type="integer", options={"default": 0})
  187.      */
  188.     private $rejected_count 0;
  189.     /**
  190.      * @ORM\Column(type="date", nullable=true)
  191.      */
  192.     private $last_upload_date;
  193.     /**
  194.      * @ORM\Column(type="integer", options={"default": 0})
  195.      */
  196.     private $daily_upload_count 0;
  197.     /**
  198.      * @ORM\Column(type="boolean", options={"default": false})
  199.      */
  200.     private $banned false;
  201.     public function getId(): ?int
  202.     {
  203.         return $this->id;
  204.     }
  205.     public function getEmail(): ?string
  206.     {
  207.         return $this->email;
  208.     }
  209.     public function setEmail(string $email): self
  210.     {
  211.         $this->email $email;
  212.         return $this;
  213.     }
  214.     /**
  215.      * A visual identifier that represents this user.
  216.      *
  217.      * @see UserInterface
  218.      */
  219.     public function getUserIdentifier(): string
  220.     {
  221.         return (string) $this->email;
  222.     }
  223.     /**
  224.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  225.      */
  226.     public function getUsername(): string
  227.     {
  228.         return (string) $this->username;
  229.     }
  230.     /**
  231.      * @see UserInterface
  232.      */
  233.     public function getRoles(): array
  234.     {
  235.         $roles $this->roles;
  236.         // guarantee every user at least has ROLE_USER
  237.         $roles[] = 'ROLE_USER';
  238.         return array_unique($roles);
  239.     }
  240.     public function setRoles(array $roles): self
  241.     {
  242.         $this->roles $roles;
  243.         return $this;
  244.     }
  245.     /**
  246.      * @see PasswordAuthenticatedUserInterface
  247.      */
  248.     public function getPassword(): string
  249.     {
  250.         return $this->password;
  251.     }
  252.     public function setPassword(string $password): self
  253.     {
  254.         $this->password $password;
  255.         return $this;
  256.     }
  257.     /**
  258.      * Returning a salt is only needed, if you are not using a modern
  259.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  260.      *
  261.      * @see UserInterface
  262.      */
  263.     public function getSalt(): ?string
  264.     {
  265.         return null;
  266.     }
  267.     /**
  268.      * @see UserInterface
  269.      */
  270.     public function eraseCredentials()
  271.     {
  272.         // If you store any temporary, sensitive data on the user, clear it here
  273.         // $this->plainPassword = null;
  274.     }
  275.     public function setUsername(string $username): self
  276.     {
  277.         $this->username $username;
  278.         return $this;
  279.     }
  280.     public function getCreatedAt(): ?\DateTimeImmutable
  281.     {
  282.         return $this->created_at;
  283.     }
  284.     public function setCreatedAt(\DateTimeImmutable $created_at): self
  285.     {
  286.         $this->created_at $created_at;
  287.         return $this;
  288.     }
  289.     public function getLastLogin(): ?\DateTime
  290.     {
  291.         return $this->last_login;
  292.     }
  293.     public function setLastLogin(\DateTime $last_login): self
  294.     {
  295.         $this->last_login $last_login;
  296.         return $this;
  297.     }
  298.     /**
  299.      * @return Collection<int, Photo>
  300.      */
  301.     public function getPhotos(): Collection
  302.     {
  303.         return $this->photos;
  304.     }
  305.     public function addPhoto(Photo $photo): self
  306.     {
  307.         if (!$this->photos->contains($photo)) {
  308.             $this->photos[] = $photo;
  309.             $photo->setUser($this);
  310.         }
  311.         return $this;
  312.     }
  313.     public function removePhoto(Photo $photo): self
  314.     {
  315.         if ($this->photos->removeElement($photo)) {
  316.             // set the owning side to null (unless already changed)
  317.             if ($photo->getUser() === $this) {
  318.                 $photo->setUser(null);
  319.             }
  320.         }
  321.         return $this;
  322.     }
  323.     public function setAgreeTerms(bool $agree_terms): self
  324.     {
  325.         $this->agree_terms $agree_terms;
  326.         return $this;
  327.     }
  328.     /**
  329.      * @return Collection<int, PhotoLike>
  330.      */
  331.     public function getPhotoLikes(): Collection
  332.     {
  333.         return $this->photoLikes;
  334.     }
  335.     public function addPhotoLike(PhotoLike $photoLike): self
  336.     {
  337.         if (!$this->photoLikes->contains($photoLike)) {
  338.             $this->photoLikes[] = $photoLike;
  339.             $photoLike->setUser($this);
  340.         }
  341.         return $this;
  342.     }
  343.     public function removePhotoLike(PhotoLike $photoLike): self
  344.     {
  345.         if ($this->photoLikes->removeElement($photoLike)) {
  346.             // set the owning side to null (unless already changed)
  347.             if ($photoLike->getUser() === $this) {
  348.                 $photoLike->setUser(null);
  349.             }
  350.         }
  351.         return $this;
  352.     }
  353.     /**
  354.      * @return Collection<int, InfluencerFollower>
  355.      */
  356.     public function getInfluencerFollowers(): Collection
  357.     {
  358.         return $this->influencerFollowers;
  359.     }
  360.     public function addInfluencerFollower(InfluencerFollower $influencerFollower): self
  361.     {
  362.         if (!$this->influencerFollowers->contains($influencerFollower)) {
  363.             $this->influencerFollowers[] = $influencerFollower;
  364.             $influencerFollower->setUser($this);
  365.         }
  366.         return $this;
  367.     }
  368.     public function removeInfluencerFollower(InfluencerFollower $influencerFollower): self
  369.     {
  370.         if ($this->influencerFollowers->removeElement($influencerFollower)) {
  371.             // set the owning side to null (unless already changed)
  372.             if ($influencerFollower->getUser() === $this) {
  373.                 $influencerFollower->setUser(null);
  374.             }
  375.         }
  376.         return $this;
  377.     }
  378.     public function getIp(): ?string
  379.     {
  380.         return $this->ip;
  381.     }
  382.     public function setIp(?string $ip): self
  383.     {
  384.         $this->ip $ip;
  385.         return $this;
  386.     }
  387.     /**
  388.      * @return Collection<int, Comment>
  389.      */
  390.     public function getComments(): Collection
  391.     {
  392.         return $this->comments;
  393.     }
  394.     public function addComment(Comment $comment): self
  395.     {
  396.         if (!$this->comments->contains($comment)) {
  397.             $this->comments[] = $comment;
  398.             $comment->setUser($this);
  399.         }
  400.         return $this;
  401.     }
  402.     public function removeComment(Comment $comment): self
  403.     {
  404.         if ($this->comments->removeElement($comment)) {
  405.             // set the owning side to null (unless already changed)
  406.             if ($comment->getUser() === $this) {
  407.                 $comment->setUser(null);
  408.             }
  409.         }
  410.         return $this;
  411.     }
  412.     public function getCountry(): ?string
  413.     {
  414.         return $this->country;
  415.     }
  416.     public function setCountry(?string $country): self
  417.     {
  418.         $this->country $country;
  419.         return $this;
  420.     }
  421.     public function getBio(): ?string
  422.     {
  423.         return $this->bio;
  424.     }
  425.     public function setBio(?string $bio): self
  426.     {
  427.         $this->bio $bio;
  428.         return $this;
  429.     }
  430.     public function getPicture(): ?bool
  431.     {
  432.         return $this->picture;
  433.     }
  434.     public function setPicture(?bool $picture): self
  435.     {
  436.         $this->picture $picture;
  437.         return $this;
  438.     }
  439.     public function getImage(): ?string
  440.     {
  441.         return $this->image;
  442.     }
  443.     public function setImage(File $file null)
  444.     {
  445.         $this->image $file;
  446.         return $this;
  447.     }
  448.     /**
  449.      * @return Collection<int, Video>
  450.      */
  451.     public function getVideos(): Collection
  452.     {
  453.         return $this->videos;
  454.     }
  455.     public function addVideo(Video $video): self
  456.     {
  457.         if (!$this->videos->contains($video)) {
  458.             $this->videos[] = $video;
  459.             $video->setUser($this);
  460.         }
  461.         return $this;
  462.     }
  463.     public function removeVideo(Video $video): self
  464.     {
  465.         if ($this->videos->removeElement($video)) {
  466.             // set the owning side to null (unless already changed)
  467.             if ($video->getUser() === $this) {
  468.                 $video->setUser(null);
  469.             }
  470.         }
  471.         return $this;
  472.     }
  473.     /**
  474.      * @return Collection<int, Comment>
  475.      */
  476.     public function getProfilComments(): Collection
  477.     {
  478.         return $this->profil_comments;
  479.     }
  480.     public function addProfilComment(Comment $profilComment): self
  481.     {
  482.         if (!$this->profil_comments->contains($profilComment)) {
  483.             $this->profil_comments[] = $profilComment;
  484.             $profilComment->setToUser($this);
  485.         }
  486.         return $this;
  487.     }
  488.     public function removeProfilComment(Comment $profilComment): self
  489.     {
  490.         if ($this->profil_comments->removeElement($profilComment)) {
  491.             // set the owning side to null (unless already changed)
  492.             if ($profilComment->getToUser() === $this) {
  493.                 $profilComment->setToUser(null);
  494.             }
  495.         }
  496.         return $this;
  497.     }
  498.     /**
  499.      * @return Collection<int, Notification>
  500.      */
  501.     public function getNotifications(): Collection
  502.     {
  503.         return $this->notifications;
  504.     }
  505.     public function addNotification(Notification $notification): self
  506.     {
  507.         if (!$this->notifications->contains($notification)) {
  508.             $this->notifications[] = $notification;
  509.             $notification->setUser($this);
  510.         }
  511.         return $this;
  512.     }
  513.     public function removeNotification(Notification $notification): self
  514.     {
  515.         if ($this->notifications->removeElement($notification)) {
  516.             // set the owning side to null (unless already changed)
  517.             if ($notification->getUser() === $this) {
  518.                 $notification->setUser(null);
  519.             }
  520.         }
  521.         return $this;
  522.     }
  523.     public function getNewsletter(): ?bool
  524.     {
  525.         return $this->newsletter;
  526.     }
  527.     public function setNewsletter(?bool $newsletter): self
  528.     {
  529.         $this->newsletter $newsletter;
  530.         return $this;
  531.     }
  532.     public function getMailNotification(): ?bool
  533.     {
  534.         return $this->mail_notification;
  535.     }
  536.     public function setMailNotification(bool $mail_notification): self
  537.     {
  538.         $this->mail_notification $mail_notification;
  539.         return $this;
  540.     }
  541.     public function getTrustLevel(): int
  542.     {
  543.         return $this->trust_level ?? 0;
  544.     }
  545.     public function setTrustLevel(int $trust_level): self
  546.     {
  547.         $this->trust_level $trust_level;
  548.         return $this;
  549.     }
  550.     public function getUploadCount(): int
  551.     {
  552.         return $this->upload_count ?? 0;
  553.     }
  554.     public function setUploadCount(int $upload_count): self
  555.     {
  556.         $this->upload_count $upload_count;
  557.         return $this;
  558.     }
  559.     public function getApprovedCount(): int
  560.     {
  561.         return $this->approved_count ?? 0;
  562.     }
  563.     public function setApprovedCount(int $approved_count): self
  564.     {
  565.         $this->approved_count $approved_count;
  566.         return $this;
  567.     }
  568.     public function getRejectedCount(): int
  569.     {
  570.         return $this->rejected_count ?? 0;
  571.     }
  572.     public function setRejectedCount(int $rejected_count): self
  573.     {
  574.         $this->rejected_count $rejected_count;
  575.         return $this;
  576.     }
  577.     public function getLastUploadDate(): ?\DateTimeInterface
  578.     {
  579.         return $this->last_upload_date;
  580.     }
  581.     public function setLastUploadDate(?\DateTimeInterface $last_upload_date): self
  582.     {
  583.         $this->last_upload_date $last_upload_date;
  584.         return $this;
  585.     }
  586.     public function getDailyUploadCount(): int
  587.     {
  588.         return $this->daily_upload_count ?? 0;
  589.     }
  590.     public function setDailyUploadCount(int $daily_upload_count): self
  591.     {
  592.         $this->daily_upload_count $daily_upload_count;
  593.         return $this;
  594.     }
  595.     public function getDailyUploadLimit(): int
  596.     {
  597.         switch ($this->getTrustLevel()) {
  598.             case 2: return 50;
  599.             case 1: return 20;
  600.             default: return 5;
  601.         }
  602.     }
  603.     public function canAutoPublish(): bool
  604.     {
  605.         return $this->getTrustLevel() >= 1;
  606.     }
  607.     public function isBanned(): bool
  608.     {
  609.         return (bool) $this->banned;
  610.     }
  611.     public function setBanned(bool $banned): self
  612.     {
  613.         $this->banned $banned;
  614.         return $this;
  615.     }
  616. }