<?php
namespace Evo\Infrastructure\MappingORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User entity.
*
* @ApiResource(attributes={
* "normalization_context"={"groups"={"read_user"}},
* "denormalization_context"={"groups"={"write_user"}}
* },
* collectionOperations={
* "me"= {
* "method"="GET",
* "path"="/admin/user/me"
* },
* "get",
* "post"
* },
* itemOperations={"get","put","change_password"={"method"="POST", "route_name"="app_user_change_password"}}
*)
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "email": "partial", "phoneNumber": "partial", "resetToken" : "exact", "roles": "partial", "organizations":"exact", "lastname":"partial", "firstname":"partial"})
* @ApiFilter(GroupFilter::class, arguments={"parameterName": "groups", "overrideDefaultGroups": true})
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity("email")
*/
class User implements UserInterface
{
/**
* @Groups({"read_user", "read_lead", "admin:user:read", "admin_orga_list", "admin:invoice:read"})
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue()
*/
private $id;
/**
* @Groups({"admin_orga_list", "read_user", "write_user", "admin:user:read"})
* @ORM\ManyToMany(targetEntity="Evo\Infrastructure\MappingORM\Organization", mappedBy="users", cascade={"persist"})
* @ORM\JoinTable(name="user_organization",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id")}
* )
*/
private Collection $organizations;
/**
* @Groups({"read_user", "write_user"})
* @ORM\ManyToMany(targetEntity="Evo\Infrastructure\MappingORM\Store")
* @Assert\Valid
*/
private Collection $stores;
/**
* @Groups({"admin_orga_list", "read_user", "write_user", "admin:user:read"})
* @ORM\Column(type="string", length=150, unique=true)
* @Assert\NotBlank
* @Assert\Email
*/
private ?string $email = null;
/**
* @Groups({"admin_orga_list", "read_user", "write_user", "admin:user:read"})
* @ORM\Column(type="string", length=30, nullable=true)
*/
private ?string $phoneNumber = null;
/**
* @Groups({"read_user", "write_user"})
*
* @var string The hashed password
*
* @ORM\Column(type="text")
* @Assert\NotBlank
* @Assert\Regex(pattern="/^(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/")
*/
private ?string $password = null;
/**
* @var array
*
* @Groups({"read_user", "write_user", "admin:user:read", "read:activity"})
* @ORM\Column(type="json")
* @Assert\NotBlank
*/
private $roles = [];
/**
* @Groups({"admin_orga_list", "read_user", "write_user", "read_lead", "admin:user:read", "read_potential_case"})
* @ORM\Column(type="string", length=150, nullable=true)
*/
private ?string $lastname = null;
/**
* @Groups({"admin_orga_list", "read_user", "write_user", "read_lead", "read:activity", "admin:user:read", "read_potential_case"})
* @ORM\Column(type="string", length=150, nullable=true)
*/
private ?string $firstname = null;
/**
* @Groups({"read_user", "admin:user:read"})
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $createdAt;
/**
* @Groups({"read_user"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $registrationSource = null;
/**
* @Groups({"read_user", "write_user"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $honorificPrefix = null;
/**
* @ApiProperty(writable=false)
* @Groups({"read_user", "write_user"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $resetToken = null;
/**
* @ApiProperty(writable=false)
* @Groups({"read_user"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $resetTokenExpirationDate = null;
/**
* @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\Invoice", mappedBy="user")
*/
private ?Collection $invoices;
/**
* @Groups({"read_user", "write_user"})
* @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\UserUTM", mappedBy="user", cascade={"persist"})
*/
private ?Collection $UTMs;
/**
* @Groups({"read_user", "write_user"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $uniqId = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $codeConfirmation = null;
/**
* @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\Lead", mappedBy="manager")
*/
private ?Collection $leads;
/**
* @ORM\OneToMany(targetEntity=ActivityLog::class, mappedBy="user")
*/
private ?Collection $activityLogs;
/**
* @Groups({"write_user", "admin:user:read"})
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $updatedAt = null;
/**
* @Groups({"read_user", "write_user", "admin:user:read"})
* @ORM\Column(type="boolean", options={"default" : true})
*/
private bool $isActive = true;
/**
* @ORM\OneToMany(targetEntity=CommercialGesture::class, mappedBy="user")
*/
private ?Collection $commercialGestures;
/**
* @ORM\OneToMany(targetEntity=PappersActivityLogWrapper::class, mappedBy="user")
*/
private Collection $pappersActivityLogWrappers;
public function __construct()
{
$this->setRoles(['ROLE_USER']);
$this->organizations = new ArrayCollection();
$this->stores = new ArrayCollection();
$this->invoices = new ArrayCollection();
$this->UTMs = new ArrayCollection();
$this->leads = new ArrayCollection();
$this->activityLogs = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->commercialGestures = new ArrayCollection();
$this->pappersActivityLogWrappers = new ArrayCollection();
}
public function __toString(): string
{
if (' ' !== $this->getFullName()) {
return $this->getFullName();
}
return $this->getEmail() ?? 'N/A';
}
/**
* @Groups({"admin:invoice:read"})
*/
public function getFullName(): string
{
if (!($this->firstname && $this->lastname)) {
return $this->email;
}
return trim($this->firstname.' '.$this->lastname);
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return Organization[]
*/
public function getOrganizations()
{
return $this->organizations->getValues();
}
/**
* @return User
*/
public function addOrganization(Organization $organization)
{
if (!$this->organizations->contains($organization)) {
$this->organizations->add($organization);
}
return $this;
}
/**
* @return User
*/
public function removeOrganization(Organization $organization)
{
$this->organizations->removeElement($organization);
return $this;
}
/**
* @return string
*/
public function getPhoneNumber()
{
return $this->phoneNumber;
}
/**
* @param string $phoneNumber
*
* @return User
*/
public function setPhoneNumber($phoneNumber)
{
$this->phoneNumber = str_replace(' ', '', $phoneNumber);
return $this;
}
/**
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @param string $lastname
*
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* @param string $firstname
*
* @return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername()
{
return $this->email;
}
/**
* @see UserInterface
*/
public function getRoles()
{
return $this->roles;
}
/**
* @return User
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
/**
* @return User
*/
public function addRole(string $role)
{
$this->roles[] = $role;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword()
{
return $this->password;
}
/**
* @return User
*/
public function setPassword(string $password)
{
$this->password = $password;
return $this;
}
public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}
/**
* @param \DateTime|\DateTimeImmutable $createdAt
*
* @return User
*/
public function setCreatedAt(?\DateTimeInterface $createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
}
public function getRegistrationSource(): ?string
{
return $this->registrationSource;
}
public function setRegistrationSource(?string $registrationSource): self
{
$this->registrationSource = $registrationSource;
return $this;
}
public function getHonorificPrefix(): ?string
{
return $this->honorificPrefix;
}
public function setHonorificPrefix(?string $honorificPrefix): self
{
$this->honorificPrefix = $honorificPrefix;
return $this;
}
public function getResetToken(): ?string
{
return $this->resetToken;
}
public function setResetToken(?string $resetToken): self
{
$this->resetToken = $resetToken;
return $this;
}
public function getResetTokenExpirationDate(): ?string
{
return $this->resetTokenExpirationDate;
}
public function setResetTokenExpirationDate(?string $resetTokenExpirationDate): self
{
$this->resetTokenExpirationDate = $resetTokenExpirationDate;
return $this;
}
/**
* @return Store[]
*/
public function getStores()
{
return $this->stores->getValues();
}
public function setStores(?Collection $stores): User
{
$this->stores = $stores;
return $this;
}
/**
* @return User
*/
public function addStore(Store $store)
{
if (!$this->stores->contains($store)) {
$this->stores->add($store);
}
return $this;
}
/**
* @return User
*/
public function removeStore(Store $store)
{
$this->stores->removeElement($store);
return $this;
}
/**
* @return Collection|Invoice[]
*/
public function getInvoices(): Collection
{
return $this->invoices;
}
public function addInvoice(Invoice $invoice): self
{
if (!$this->invoices->contains($invoice)) {
$this->invoices[] = $invoice;
$invoice->setUser($this);
}
return $this;
}
public function removeInvoice(Invoice $invoice): self
{
if ($this->invoices->contains($invoice)) {
$this->invoices->removeElement($invoice);
// set the owning side to null (unless already changed)
if ($invoice->getUser() === $this) {
$invoice->setUser(null);
}
}
return $this;
}
/**
* @return Collection|UserUTM[]
*/
public function getUTMs(): Collection
{
return $this->UTMs;
}
public function addUTM(UserUTM $uTM): self
{
if (!$this->UTMs->contains($uTM)) {
$this->UTMs[] = $uTM;
$uTM->setUser($this);
}
return $this;
}
public function removeUTM(UserUTM $uTM): self
{
if ($this->UTMs->contains($uTM)) {
$this->UTMs->removeElement($uTM);
// set the owning side to null (unless already changed)
if ($uTM->getUser() === $this) {
$uTM->setUser(null);
}
}
return $this;
}
public function getUniqId(): ?string
{
if ($this->uniqId) {
return $this->uniqId;
}
return $this->id;
}
public function setUniqId(?string $uniqId): self
{
$this->uniqId = $uniqId;
return $this;
}
public function getCodeConfirmation(): ?int
{
return $this->codeConfirmation;
}
public function setCodeConfirmation(?int $codeConfirmation): self
{
$this->codeConfirmation = $codeConfirmation;
return $this;
}
/**
* @return Collection|Lead[]
*/
public function getLeads(): Collection
{
return $this->leads;
}
public function addLead(Lead $lead): self
{
if (!$this->leads->contains($lead)) {
$this->leads[] = $lead;
$lead->setManager($this);
}
return $this;
}
public function removeLead(Lead $lead): self
{
if ($this->leads->contains($lead)) {
$this->leads->removeElement($lead);
// set the owning side to null (unless already changed)
if ($lead->getManager() === $this) {
$lead->setManager(null);
}
}
return $this;
}
/**
* @return Collection|ActivityLog[]
*/
public function getActivityLogs(): Collection
{
return $this->activityLogs;
}
public function addActivityLog(ActivityLog $activityLog): self
{
if (!$this->activityLogs->contains($activityLog)) {
$this->activityLogs[] = $activityLog;
$activityLog->setUser($this);
}
return $this;
}
public function removeActivityLog(ActivityLog $activityLog): self
{
// set the owning side to null (unless already changed)
if ($this->activityLogs->removeElement($activityLog) && $activityLog->getUser() === $this) {
$activityLog->setUser(null);
}
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
/**
* @return Collection|CommercialGesture[]
*/
public function getCommercialGestures(): Collection
{
return $this->commercialGestures;
}
public function addCommercialGesture(CommercialGesture $commercialGesture): self
{
if (!$this->commercialGestures->contains($commercialGesture)) {
$this->commercialGestures[] = $commercialGesture;
$commercialGesture->setUser($this);
}
return $this;
}
public function removeCommercialGesture(CommercialGesture $commercialGesture): self
{
// set the owning side to null (unless already changed)
if ($this->commercialGestures->removeElement($commercialGesture) && $commercialGesture->getUser() === $this) {
$commercialGesture->setUser(null);
}
return $this;
}
public static function createFixture(array $data): self
{
$self = (new self());
$self->email = $data['email'];
$self->firstname = $data['firstname'] ?: null;
$self->lastname = $data['lastname'] ?: null;
$self->phoneNumber = $data['phoneNumber'];
$self->password = $data['password'] ?? '123soleil';
$self->roles = $data['roles'] ?? ['ROLE_USER'];
$self->uniqId = $data['uniqId'] ?? uniqid('test', true);
return $self;
}
public function getPappersActivityLogWrappers(): Collection
{
return $this->pappersActivityLogWrappers;
}
public function addPappersActivityLogWrapper(PappersActivityLogWrapper $pappersActivityLogWrapper): self
{
if (!$this->pappersActivityLogWrappers->contains($pappersActivityLogWrapper)) {
$this->pappersActivityLogWrappers[] = $pappersActivityLogWrapper;
$pappersActivityLogWrapper->setUser($this);
}
return $this;
}
public function removePappersActivityLogWrapper(PappersActivityLogWrapper $pappersActivityLogWrapper): self
{
if ($this->pappersActivityLogWrappers->contains($pappersActivityLogWrapper)) {
$this->pappersActivityLogWrappers->removeElement($pappersActivityLogWrapper);
// set the owning side to null (unless already changed)
if ($pappersActivityLogWrapper->getUser() === $this) {
$pappersActivityLogWrapper->setUser(null);
}
}
return $this;
}
}