<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(
* fields={"username"},
* errorPath="username",
* message="This username is already used"
* )
* @UniqueEntity(
* fields={"email"},
* errorPath="email",
* message="This email is already used"
* )
* @ORM\Table(name="`user`")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50, unique=true)
* @Assert\NotBlank()
* @Assert\Email(
* message = "The email '{{ value }}' is not valid."
* )
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
* @Assert\NotBlank(groups={"password"})
* @Assert\Length(
* min = 4,
* max = 15,
* minMessage = "Your password must be at least {{ limit }} characters",
* maxMessage = "Your password must be at most {{ limit }} characters",
* groups={"password"}
* )
*/
private $password;
/**
* @ORM\Column(type="string", length=20)
* @Assert\NotBlank()
* @Assert\Regex(
* pattern = "/^(?=.{3,15}$)(?![_.-])(?!.*[_.-]{2})[a-zA-Z0-9_-]+([^._-])$/",
* message = "Your username is invalid"
* )
* @Assert\Length(
* min = 3,
* max = 15,
* minMessage = "Your username must be at least {{ limit }} characters",
* maxMessage = "Your username must be at most {{ limit }} characters"
* )
*/
private $username;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $created_at;
public function __construct()
{
$this->last_login = new \DateTime();
$this->created_at = new \DateTimeImmutable();
$this->photos = new ArrayCollection();
$this->photoLikes = new ArrayCollection();
$this->influencerFollowers = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->videos = new ArrayCollection();
$this->profil_comments = new ArrayCollection();
$this->notifications = new ArrayCollection();
$this->setPicture(false);
$this->setNewsletter(true);
$this->setMailNotification(true);
$this->setImage(null);
$this->setRoles(['ROLE_USER']);
}
/**
* @ORM\Column(type="datetime")
*/
private $last_login;
/**
* @ORM\OneToMany(targetEntity=Photo::class, mappedBy="user", orphanRemoval=true)
*/
private $photos;
/**
* @Assert\NotBlank()
*/
private $agree_terms;
/**
* @ORM\OneToMany(targetEntity=PhotoLike::class, mappedBy="user", orphanRemoval=true)
*/
private $photoLikes;
/**
* @ORM\OneToMany(targetEntity=InfluencerFollower::class, mappedBy="user", orphanRemoval=true)
*/
private $influencerFollowers;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ip;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="user", orphanRemoval=true)
*/
private $comments;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $country;
/**
* @Assert\Length(
* min = 3,
* max = 250,
* minMessage = "Your bio must be at least {{ limit }} characters long",
* maxMessage = "Your bio cannot be longer than {{ limit }} characters"
* )
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $bio;
/**
* @Assert\Image(
* minWidth = 70,
* minHeight = 70,
* minWidthMessage = "Your photo is too small",
* minHeightMessage = "Your photo is too small",
* maxSize = "5M",
* maxSizeMessage = "The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}"
* )
* @Assert\Valid()
*/
private $image;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $picture;
/**
* @ORM\OneToMany(targetEntity=Video::class, mappedBy="user", orphanRemoval=true)
*/
private $videos;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="to_user")
*/
private $profil_comments;
/**
* @ORM\OneToMany(targetEntity=Notification::class, mappedBy="user", orphanRemoval=true)
*/
private $notifications;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $newsletter;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $mail_notification;
/**
* @ORM\Column(type="smallint", options={"default": 0})
*/
private $trust_level = 0;
/**
* @ORM\Column(type="integer", options={"default": 0})
*/
private $upload_count = 0;
/**
* @ORM\Column(type="integer", options={"default": 0})
*/
private $approved_count = 0;
/**
* @ORM\Column(type="integer", options={"default": 0})
*/
private $rejected_count = 0;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $last_upload_date;
/**
* @ORM\Column(type="integer", options={"default": 0})
*/
private $daily_upload_count = 0;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private $banned = false;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeImmutable $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getLastLogin(): ?\DateTime
{
return $this->last_login;
}
public function setLastLogin(\DateTime $last_login): self
{
$this->last_login = $last_login;
return $this;
}
/**
* @return Collection<int, Photo>
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): self
{
if (!$this->photos->contains($photo)) {
$this->photos[] = $photo;
$photo->setUser($this);
}
return $this;
}
public function removePhoto(Photo $photo): self
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getUser() === $this) {
$photo->setUser(null);
}
}
return $this;
}
public function setAgreeTerms(bool $agree_terms): self
{
$this->agree_terms = $agree_terms;
return $this;
}
/**
* @return Collection<int, PhotoLike>
*/
public function getPhotoLikes(): Collection
{
return $this->photoLikes;
}
public function addPhotoLike(PhotoLike $photoLike): self
{
if (!$this->photoLikes->contains($photoLike)) {
$this->photoLikes[] = $photoLike;
$photoLike->setUser($this);
}
return $this;
}
public function removePhotoLike(PhotoLike $photoLike): self
{
if ($this->photoLikes->removeElement($photoLike)) {
// set the owning side to null (unless already changed)
if ($photoLike->getUser() === $this) {
$photoLike->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, InfluencerFollower>
*/
public function getInfluencerFollowers(): Collection
{
return $this->influencerFollowers;
}
public function addInfluencerFollower(InfluencerFollower $influencerFollower): self
{
if (!$this->influencerFollowers->contains($influencerFollower)) {
$this->influencerFollowers[] = $influencerFollower;
$influencerFollower->setUser($this);
}
return $this;
}
public function removeInfluencerFollower(InfluencerFollower $influencerFollower): self
{
if ($this->influencerFollowers->removeElement($influencerFollower)) {
// set the owning side to null (unless already changed)
if ($influencerFollower->getUser() === $this) {
$influencerFollower->setUser(null);
}
}
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(?string $ip): self
{
$this->ip = $ip;
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setUser($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getUser() === $this) {
$comment->setUser(null);
}
}
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
public function getBio(): ?string
{
return $this->bio;
}
public function setBio(?string $bio): self
{
$this->bio = $bio;
return $this;
}
public function getPicture(): ?bool
{
return $this->picture;
}
public function setPicture(?bool $picture): self
{
$this->picture = $picture;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(File $file = null)
{
$this->image = $file;
return $this;
}
/**
* @return Collection<int, Video>
*/
public function getVideos(): Collection
{
return $this->videos;
}
public function addVideo(Video $video): self
{
if (!$this->videos->contains($video)) {
$this->videos[] = $video;
$video->setUser($this);
}
return $this;
}
public function removeVideo(Video $video): self
{
if ($this->videos->removeElement($video)) {
// set the owning side to null (unless already changed)
if ($video->getUser() === $this) {
$video->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getProfilComments(): Collection
{
return $this->profil_comments;
}
public function addProfilComment(Comment $profilComment): self
{
if (!$this->profil_comments->contains($profilComment)) {
$this->profil_comments[] = $profilComment;
$profilComment->setToUser($this);
}
return $this;
}
public function removeProfilComment(Comment $profilComment): self
{
if ($this->profil_comments->removeElement($profilComment)) {
// set the owning side to null (unless already changed)
if ($profilComment->getToUser() === $this) {
$profilComment->setToUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Notification>
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Notification $notification): self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
$notification->setUser($this);
}
return $this;
}
public function removeNotification(Notification $notification): self
{
if ($this->notifications->removeElement($notification)) {
// set the owning side to null (unless already changed)
if ($notification->getUser() === $this) {
$notification->setUser(null);
}
}
return $this;
}
public function getNewsletter(): ?bool
{
return $this->newsletter;
}
public function setNewsletter(?bool $newsletter): self
{
$this->newsletter = $newsletter;
return $this;
}
public function getMailNotification(): ?bool
{
return $this->mail_notification;
}
public function setMailNotification(bool $mail_notification): self
{
$this->mail_notification = $mail_notification;
return $this;
}
public function getTrustLevel(): int
{
return $this->trust_level ?? 0;
}
public function setTrustLevel(int $trust_level): self
{
$this->trust_level = $trust_level;
return $this;
}
public function getUploadCount(): int
{
return $this->upload_count ?? 0;
}
public function setUploadCount(int $upload_count): self
{
$this->upload_count = $upload_count;
return $this;
}
public function getApprovedCount(): int
{
return $this->approved_count ?? 0;
}
public function setApprovedCount(int $approved_count): self
{
$this->approved_count = $approved_count;
return $this;
}
public function getRejectedCount(): int
{
return $this->rejected_count ?? 0;
}
public function setRejectedCount(int $rejected_count): self
{
$this->rejected_count = $rejected_count;
return $this;
}
public function getLastUploadDate(): ?\DateTimeInterface
{
return $this->last_upload_date;
}
public function setLastUploadDate(?\DateTimeInterface $last_upload_date): self
{
$this->last_upload_date = $last_upload_date;
return $this;
}
public function getDailyUploadCount(): int
{
return $this->daily_upload_count ?? 0;
}
public function setDailyUploadCount(int $daily_upload_count): self
{
$this->daily_upload_count = $daily_upload_count;
return $this;
}
public function getDailyUploadLimit(): int
{
switch ($this->getTrustLevel()) {
case 2: return 50;
case 1: return 20;
default: return 5;
}
}
public function canAutoPublish(): bool
{
return $this->getTrustLevel() >= 1;
}
public function isBanned(): bool
{
return (bool) $this->banned;
}
public function setBanned(bool $banned): self
{
$this->banned = $banned;
return $this;
}
}