<?php
namespace App\Entity\Auth;
use App\Entity\Activities\Booking;
use App\Entity\Activities\Feedback;
use App\Entity\Activities\Message;
use App\Entity\Activities\Report;
use App\Entity\Activities\WareHouse;
use App\Entity\Config\Day;
use App\Entity\Config\Profile;
use App\Entity\Activities\Likes;
use App\Entity\Config\Services;
use App\Infrastructure\Social\Entity\SocialLoggableTrait;
use App\Repository\Auth\UserRepository;
use App\Traits\DeletableTrait;
use App\Traits\FilesTrait;
use App\Traits\ProfileTrait;
use App\Traits\TimestampsTrait;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Serializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"email"}, message="Il y'a déjà un compte avec cette addresse email")
* @ORM\HasLifecycleCallbacks()
* @Vich\Uploadable()
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface, Serializable
{
use TimestampsTrait;
use DeletableTrait;
use SocialLoggableTrait;
use ProfileTrait;
use FilesTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private string $email;
/**
* @ORM\Column(type="json")
*/
private array $roles = ['ROLE_USER'];
/**
* @ORM\Column(type="json", nullable=true)
*/
private ?array $errors;
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private string $password;
/**
* @ORM\Column(type="string", length=255)
*/
private string $firstname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $lastname;
/**
* @ORM\Column(type="string", length=255)
*/
private string $phone = '';
/**
* @ORM\Column(type="string", length=255)
*/
private string $address = '';
/**
* @ORM\Column(type="integer", nullable=true)
* @Assert\Regex("/^\d{5}$/")
*/
private ?int $zipCode = null;
/**
* @ORM\Column(type="string", length=255)
*/
private string $town = '';
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
private ?string $username;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $confirmationToken;
/**
* @ORM\Column(type="boolean")
*/
private bool $isVerified = false;
/**
* @ORM\Column(type="boolean", options={"default": true})
*/
private bool $offerMailNotification = true;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $biography;
/**
* @Vich\UploadableField(mapping="avatars", fileNameProperty="avatarName")
* @var File|null
*/
private ?File $avatarFile = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $avatarName = null;
/**
* @ORM\OneToMany(targetEntity=Day::class, mappedBy="user")
*/
private $days;
/**
* @ORM\Column(type="float")
*/
private float $hourRate = 0;
/**
* @ORM\Column(type="smallint")
*/
private int $experience = 0;
/**
* @ORM\Column(type="date", nullable=true)
*/
private ?DateTime $birthDate;
/**
* @ORM\Column(type="boolean")
*/
private bool $vehicle = false;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private ?string $status;
/**
* @ORM\ManyToOne(targetEntity=Profile::class, inversedBy="users")
*/
private ?Profile $profile;
/**
* @ORM\OneToMany(targetEntity=Feedback::class, mappedBy="author")
*/
private $feedback;
/**
* @ORM\OneToMany(targetEntity=Feedback::class, mappedBy="candidate")
*/
private $candidateFeedback;
/**
* @ORM\OneToMany(targetEntity=Likes::class, mappedBy="liker")
*/
private $likerLikes;
/**
* @ORM\OneToMany(targetEntity=Likes::class, mappedBy="liked")
*/
private $likedLikes;
/**
* @ORM\OneToMany(targetEntity=Report::class, mappedBy="author")
*/
private $reports;
/**
* @ORM\OneToMany(targetEntity=Report::class, mappedBy="profile")
*/
private $profileReports;
/**
* @ORM\OneToMany(targetEntity=Booking::class, mappedBy="parent")
*/
private $parentBookings;
/**
* @ORM\OneToMany(targetEntity=Booking::class, mappedBy="nanny")
*/
private $bookings;
/**
* @ORM\OneToMany(targetEntity=Message::class, mappedBy="sender")
*/
private $senderMessages;
/**
* @ORM\OneToMany(targetEntity=Message::class, mappedBy="recipient")
*/
private $recipientMessages;
/**
* @ORM\ManyToMany(targetEntity=Services::class, inversedBy="users")
*/
private $services;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $validations = [];
/**
* @ORM\Column(type="date", nullable=true)
*/
private $hiredAt;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=true)
*/
private $matricule;
/**
* @ORM\ManyToOne(targetEntity=WareHouse::class, inversedBy="users")
*/
private $warehouse;
/**
* @ORM\Column(type="boolean")
*/
private $isPasswordUpdated = false;
public function __construct()
{
$this->days = new ArrayCollection();
$this->feedback = new ArrayCollection();
$this->candidateFeedback = new ArrayCollection();
$this->likerLikes = new ArrayCollection();
$this->likedLikes = new ArrayCollection();
$this->reports = new ArrayCollection();
$this->profileReports = new ArrayCollection();
$this->parentBookings = new ArrayCollection();
$this->bookings = new ArrayCollection();
$this->senderMessages = new ArrayCollection();
$this->recipientMessages = new ArrayCollection();
$this->services = new ArrayCollection();
}
/**
* @return string|null
*/
public function getFullName()
{
$name = '';
if (!empty($this->firstname)) {
$name .= $this->firstname;
}
if (!empty($this->lastname)) {
$name .= !empty($name) ? ' ' . $this->lastname : $this->lastname;
}
return !empty($name) ? $name : null;
}
/**
* @return string|null
*/
public function getFullAddress()
{
$address = '';
if (!empty($this->address)) {
$address .= $this->address;
}
if (!empty($this->zipCode)) {
$address .= (!empty($address)) ? ', ' . $this->zipCode : $this->zipCode;
}
if (!empty($this->zipCode)) {
$address .= (!empty($address)) ? ', ' . $this->town : $this->town;
}
return (!empty($address)) ? $address : null;
}
public function __toString()
{
return $this->getFullName();
}
/**
* Permet de savoir si cet article est "liké" par un utilisateur
* @param User|UserInterface $user
* @return bool
*/
public function isLikeByUser(User $user): bool
{
foreach ($this->likedLikes as $like) {
/* @var $like Likes */
if ($like->getLiker() === $user && $like->isIsActive() === true) return true;
}
return 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->getFullName();
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string)$this->email;
}
/**
* @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 getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(?string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getZipCode(): ?int
{
return $this->zipCode;
}
public function setZipCode(int $zipCode): self
{
$this->zipCode = $zipCode;
return $this;
}
public function getTown(): ?string
{
return $this->town;
}
public function setTown(string $town): self
{
$this->town = $town;
return $this;
}
public function setUsername(?string $username): self
{
$this->username = $username;
return $this;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function hasOfferMailNotification(): bool
{
return $this->offerMailNotification;
}
public function setOfferMailNotification(bool $offerMailNotification): User
{
$this->offerMailNotification = $offerMailNotification;
return $this;
}
public function getBiography(): ?string
{
return $this->biography;
}
public function setBiography(?string $biography): self
{
$this->biography = $biography;
return $this;
}
public function getAvatarFile(): ?File
{
return $this->avatarFile;
}
public function setAvatarFile(?File $avatarFile): User
{
$this->avatarFile = $avatarFile;
return $this;
}
public function getAvatarName(): ?string
{
return $this->avatarName;
}
public function setAvatarName(?string $avatarName): User
{
$this->avatarName = $avatarName;
return $this;
}
/**
* @return Collection<int, Day>
*/
public function getDays(): Collection
{
return $this->days;
}
/**
* @return Day[]
*/
public function getAvailableDays(): array
{
$days = $this->days->getValues();
$result = [];
/* @var $day Day */
foreach ($days as $day) {
if ($day->isIsOpen()) {
$result[] = $day;
}
}
return $result;
}
public function addDay(Day $day): self
{
if (!$this->days->contains($day)) {
$this->days[] = $day;
$day->setUser($this);
}
return $this;
}
public function removeDay(Day $day): self
{
if ($this->days->removeElement($day)) {
// set the owning side to null (unless already changed)
if ($day->getUser() === $this) {
$day->setUser(null);
}
}
return $this;
}
public function getHourRate(): ?float
{
return $this->hourRate;
}
public function setHourRate(float $hourRate): self
{
$this->hourRate = $hourRate;
return $this;
}
public function getExperience(): ?int
{
return $this->experience;
}
public function setExperience(int $experience): self
{
$this->experience = $experience;
return $this;
}
public function getBirthDate(): ?DateTimeInterface
{
return $this->birthDate;
}
public function setBirthDate(?DateTimeInterface $birthDate): self
{
$this->birthDate = $birthDate;
return $this;
}
public function isVehicle(): ?bool
{
return $this->vehicle;
}
public function setVehicle(bool $vehicle): self
{
$this->vehicle = $vehicle;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): self
{
$this->status = $status;
return $this;
}
public function getProfile(): ?Profile
{
return $this->profile;
}
public function setProfile(?Profile $profile): self
{
$this->profile = $profile;
return $this;
}
/**
* @return Collection<int, Feedback>
*/
public function getFeedback(): Collection
{
return $this->feedback;
}
public function addFeedback(Feedback $feedback): self
{
if (!$this->feedback->contains($feedback)) {
$this->feedback[] = $feedback;
$feedback->setAuthor($this);
}
return $this;
}
public function removeFeedback(Feedback $feedback): self
{
if ($this->feedback->removeElement($feedback)) {
// set the owning side to null (unless already changed)
if ($feedback->getAuthor() === $this) {
$feedback->setAuthor(null);
}
}
return $this;
}
/**
* @return Collection<int, Feedback>
*/
public function getCandidateFeedback(): Collection
{
return $this->candidateFeedback;
}
public function addCandidateFeedback(Feedback $candidateFeedback): self
{
if (!$this->candidateFeedback->contains($candidateFeedback)) {
$this->candidateFeedback[] = $candidateFeedback;
$candidateFeedback->setCandidate($this);
}
return $this;
}
public function removeCandidateFeedback(Feedback $candidateFeedback): self
{
if ($this->candidateFeedback->removeElement($candidateFeedback)) {
// set the owning side to null (unless already changed)
if ($candidateFeedback->getCandidate() === $this) {
$candidateFeedback->setCandidate(null);
}
}
return $this;
}
/**
* @return Collection<int, Likes>
*/
public function getLikerLikes(): Collection
{
return $this->likerLikes;
}
public function addLikerLike(Likes $likerLike): self
{
if (!$this->likerLikes->contains($likerLike)) {
$this->likerLikes[] = $likerLike;
$likerLike->setLiker($this);
}
return $this;
}
public function removeLikerLike(Likes $likerLike): self
{
if ($this->likerLikes->removeElement($likerLike)) {
// set the owning side to null (unless already changed)
if ($likerLike->getLiker() === $this) {
$likerLike->setLiker(null);
}
}
return $this;
}
/**
* @return Collection<int, Likes>
*/
public function getLikedLikes(): Collection
{
return $this->likedLikes;
}
public function addLikedLike(Likes $likedLike): self
{
if (!$this->likedLikes->contains($likedLike)) {
$this->likedLikes[] = $likedLike;
$likedLike->setLiked($this);
}
return $this;
}
public function removeLikedLike(Likes $likedLike): self
{
if ($this->likedLikes->removeElement($likedLike)) {
// set the owning side to null (unless already changed)
if ($likedLike->getLiked() === $this) {
$likedLike->setLiked(null);
}
}
return $this;
}
/**
* @return Collection<int, Report>
*/
public function getReports(): Collection
{
return $this->reports;
}
public function addReport(Report $report): self
{
if (!$this->reports->contains($report)) {
$this->reports[] = $report;
$report->setAuthor($this);
}
return $this;
}
public function removeReport(Report $report): self
{
if ($this->reports->removeElement($report)) {
// set the owning side to null (unless already changed)
if ($report->getAuthor() === $this) {
$report->setAuthor(null);
}
}
return $this;
}
/**
* @return Collection<int, Report>
*/
public function getProfileReports(): Collection
{
return $this->profileReports;
}
public function addProfileReport(Report $profileReport): self
{
if (!$this->profileReports->contains($profileReport)) {
$this->profileReports[] = $profileReport;
$profileReport->setProfile($this);
}
return $this;
}
public function removeProfileReport(Report $profileReport): self
{
if ($this->profileReports->removeElement($profileReport)) {
// set the owning side to null (unless already changed)
if ($profileReport->getProfile() === $this) {
$profileReport->setProfile(null);
}
}
return $this;
}
/**
* @return Collection<int, Booking>
*/
public function getParentBookings(): Collection
{
return $this->parentBookings;
}
public function addParentBooking(Booking $parentBooking): self
{
if (!$this->parentBookings->contains($parentBooking)) {
$this->parentBookings[] = $parentBooking;
$parentBooking->setParent($this);
}
return $this;
}
public function removeParentBooking(Booking $parentBooking): self
{
if ($this->parentBookings->removeElement($parentBooking)) {
// set the owning side to null (unless already changed)
if ($parentBooking->getParent() === $this) {
$parentBooking->setParent(null);
}
}
return $this;
}
/**
* @return Collection<int, Booking>
*/
public function getBookings(): Collection
{
return $this->bookings;
}
public function addBooking(Booking $booking): self
{
if (!$this->bookings->contains($booking)) {
$this->bookings[] = $booking;
$booking->setNanny($this);
}
return $this;
}
public function removeBooking(Booking $booking): self
{
if ($this->bookings->removeElement($booking)) {
// set the owning side to null (unless already changed)
if ($booking->getNanny() === $this) {
$booking->setNanny(null);
}
}
return $this;
}
/**
* String representation of object
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* @since 5.1.0
*/
public function serialize()
{
return serialize([$this->id, $this->firstname, $this->lastname, $this->email, $this->password, base64_encode($this->avatarFile)]);
}
/**
* Constructs the object
* @link http://php.net/manual/en/serializable.unserialize.php
*
* @param string $serialized <p>
* The string representation of the object.
* </p>
*
* @return void
* @since 5.1.0
*/
public function unserialize($serialized)
{
if ($this->avatarFile) {
[$this->id, $this->firstname, $this->lastname, $this->email, $this->password, $this->avatarFile] = unserialize($serialized, ['allowed_classes' => false]);
} else {
[$this->id, $this->firstname, $this->lastname, $this->email, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
}
}
/**
* @return Collection<int, Message>
*/
public function getSenderMessages(): Collection
{
return $this->senderMessages;
}
public function addSenderMessage(Message $senderMessage): self
{
if (!$this->senderMessages->contains($senderMessage)) {
$this->senderMessages[] = $senderMessage;
$senderMessage->setSender($this);
}
return $this;
}
public function removeSenderMessage(Message $senderMessage): self
{
if ($this->senderMessages->removeElement($senderMessage)) {
// set the owning side to null (unless already changed)
if ($senderMessage->getSender() === $this) {
$senderMessage->setSender(null);
}
}
return $this;
}
/**
* @return Collection<int, Message>
*/
public function getRecipientMessages(): Collection
{
return $this->recipientMessages;
}
public function addRecipientMessage(Message $recipientMessage): self
{
if (!$this->recipientMessages->contains($recipientMessage)) {
$this->recipientMessages[] = $recipientMessage;
$recipientMessage->setRecipient($this);
}
return $this;
}
public function removeRecipientMessage(Message $recipientMessage): self
{
if ($this->recipientMessages->removeElement($recipientMessage)) {
// set the owning side to null (unless already changed)
if ($recipientMessage->getRecipient() === $this) {
$recipientMessage->setRecipient(null);
}
}
return $this;
}
/**
* @return Collection<int, Services>
*/
public function getServices(): Collection
{
return $this->services;
}
public function addService(Services $service): self
{
if (!$this->services->contains($service)) {
$this->services[] = $service;
}
return $this;
}
public function removeService(Services $service): self
{
$this->services->removeElement($service);
return $this;
}
/**
* @see UserInterface|null
*/
public function getErrors(): ?array
{
return $this->errors;
}
/**
* @param array|null $errors
* @return $this
*/
public function setErrors(array $errors = null): self
{
$this->errors = $errors;
return $this;
}
public function getValidations(): ?array
{
return $this->validations;
}
public function setValidations(?array $validations): self
{
$this->validations = $validations;
return $this;
}
public function getHiredAt(): ?DateTimeInterface
{
return $this->hiredAt;
}
public function setHiredAt(?DateTimeInterface $hiredAt): self
{
$this->hiredAt = $hiredAt;
return $this;
}
public function getMatricule(): ?string
{
return $this->matricule;
}
public function setMatricule(?string $matricule): self
{
$this->matricule = $matricule;
return $this;
}
public function getWarehouse(): ?WareHouse
{
return $this->warehouse;
}
public function setWarehouse(?WareHouse $warehouse): self
{
$this->warehouse = $warehouse;
return $this;
}
public function isIsPasswordUpdated(): ?bool
{
return $this->isPasswordUpdated;
}
public function setIsPasswordUpdated(bool $isPasswordUpdated): self
{
$this->isPasswordUpdated = $isPasswordUpdated;
return $this;
}
}