<?php
namespace App\Entity;
use App\Entity\MagasinUser;
use ApiPlatform\Metadata\Get;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use ApiPlatform\Metadata\ApiResource;
// use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\Common\Collections\Collection;
// use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[ApiResource(security: "is_granted('ROLE_USER')")]
#[Get (normalizationContext: ['groups' => 'user:item'],security: "is_granted('ROLE_USER')")]
#[GetCollection(normalizationContext: ['groups' => 'user:list'])]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
// #[Put(security: "is_granted('ROLE_ADMIN') or object.owner == user")]
// #[Post(security: "is_granted('ROLE_ADMIN')")]
// operations: [
// new Get(normalizationContext: ['groups' => 'user:item']),
// new GetCollection(normalizationContext: ['groups' => 'user:list'])
// ],
// order: ['email' => 'DESC', 'role' => 'ASC'],
// paginationEnabled: false,
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['user:list', 'user:item'])]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Groups(['user:list', 'user:item'])]
private ?string $email = null;
#[ORM\Column]
#[Groups(['user:list', 'user:item'])]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(length: 50)]
private ?string $pseudo = null;
// #[ORM\Column]
// private ?int $accept_cgv = null;
#[ORM\Column(type: 'boolean')]
private $accept_cgv = false;
#[ORM\Column]
private ?int $accept_publication_promotion = null;
#[ORM\Column(length: 500, nullable: true)]
private ?string $addresse = null;
#[ORM\Column(length: 14, nullable: true)]
private ?string $num_telephone = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $nomm = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $prenom = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Produit::class, orphanRemoval: true)]
private Collection $produits;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Commande::class)]
private Collection $commandes;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Addresse::class)]
private Collection $addresses;
#[ORM\ManyToOne(inversedBy: 'users')]
#[ORM\JoinColumn(nullable: false)]
private ?PaymentT $paymentT = null;
#[ORM\OneToMany(mappedBy: 'created_by', targetEntity: MagasinUser::class)]
private Collection $superadminlinkedmagasins;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: MagasinUser::class)]
private Collection $magasinsuser;
public function __construct()
{
$this->produits = new ArrayCollection();
$this->commandes = new ArrayCollection();
$this->addresses = new ArrayCollection();
//$this->magasins magasins= new ArrayCollection();
$this->superadminlinkedmagasins = new ArrayCollection();
$this->magasinsuser = new ArrayCollection();
}
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;
}
/**
* @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;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getPseudo(): ?string
{
return $this->pseudo;
}
public function setPseudo(string $pseudo): self
{
$this->pseudo = $pseudo;
return $this;
}
public function getAcceptCgv(): ?bool
{
return $this->accept_cgv;
}
public function setAcceptCgv(bool $accept_cgv): self
{
$this->accept_cgv = $accept_cgv;
return $this;
}
public function getAcceptPublicationPromotion(): ?bool
{
return $this->accept_publication_promotion;
}
public function setAcceptPublicationPromotion(bool $accept_publication_promotion): self
{
$this->accept_publication_promotion = $accept_publication_promotion;
return $this;
}
public function getAddresse(): ?string
{
return $this->addresse;
}
public function setAddresse(?string $addresse): self
{
$this->addresse = $addresse;
return $this;
}
public function getNumTelephone(): ?string
{
return $this->num_telephone;
}
public function setNumTelephone(?string $num_telephone): self
{
$this->num_telephone = $num_telephone;
return $this;
}
public function getNomm(): ?string
{
return $this->nomm;
}
public function setNomm(?string $nomm): self
{
$this->nomm = $nomm;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(?string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
/**
* @return Collection<int, Produit>
*/
public function getProduits(): Collection
{
return $this->produits;
}
public function addProduit(Produit $produit): self
{
if (!$this->produits->contains($produit)) {
$this->produits->add($produit);
$produit->setUser($this);
}
return $this;
}
//orphane remove
public function removeProduit(Produit $produit): self
{
if ($this->produits->removeElement($produit)) {
// set the owning side to null (unless already changed)
if ($produit->getUser() === $this) {
$produit->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Commande>
*/
public function getCommandes(): Collection
{
return $this->commandes;
}
public function addCommande(Commande $commande): self
{
if (!$this->commandes->contains($commande)) {
$this->commandes->add($commande);
$commande->setUser($this);
}
return $this;
}
public function removeCommande(Commande $commande): self
{
if ($this->commandes->removeElement($commande)) {
// set the owning side to null (unless already changed)
if ($commande->getUser() === $this) {
$commande->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, addresse>
*/
public function getAddresses(): Collection
{
return $this->addresses;
}
public function addAddress(Addresse $address): self
{
if (!$this->addresses->contains($address)) {
$this->addresses->add($address);
$address->setUser($this);
}
return $this;
}
public function removeAddress(Addresse $address): self
{
if ($this->addresses->removeElement($address)) {
// set the owning side to null (unless already changed)
if ($address->getUser() === $this) {
$address->setUser(null);
}
}
return $this;
}
public function getPaymentT(): ?PaymentT
{
return $this->paymentT;
}
public function setPaymentT(?PaymentT $paymentT): self
{
$this->paymentT = $paymentT;
return $this;
}
/**
* @return Collection<int, MagasinUser>
*/
public function getSuperadminlinkedmagasins(): Collection
{
return $this->superadminlinkedmagasins;
}
public function addSuperadminlinkedmagasin(MagasinUser $superadminlinkedmagasin): static
{
if (!$this->superadminlinkedmagasins->contains($superadminlinkedmagasin)) {
$this->superadminlinkedmagasins->add($superadminlinkedmagasin);
$superadminlinkedmagasin->setCreatedBy($this);
}
return $this;
}
public function removeSuperadminlinkedmagasin(MagasinUser $superadminlinkedmagasin): static
{
if ($this->superadminlinkedmagasins->removeElement($superadminlinkedmagasin)) {
// set the owning side to null (unless already changed)
if ($superadminlinkedmagasin->getCreatedBy() === $this) {
$superadminlinkedmagasin->setCreatedBy(null);
}
}
return $this;
}
/**
* @return Collection<int, MagasinUser>
*/
public function getMagasinsuser(): Collection
{
return $this->magasinsuser;
}
public function addMagasinsuser(MagasinUser $magasinsuser): static
{
if (!$this->magasinsuser->contains($magasinsuser)) {
$this->magasinsuser->add($magasinsuser);
$magasinsuser->setUser($this);
}
return $this;
}
public function removeMagasinsuser(MagasinUser $magasinsuser): static
{
if ($this->magasinsuser->removeElement($magasinsuser)) {
// set the owning side to null (unless already changed)
if ($magasinsuser->getUser() === $this) {
$magasinsuser->setUser(null);
}
}
return $this;
}
public function getLinkedMagasins(): array
{
return $this->magasinsuser
->map(fn(MagasinUser $link) => $link->getMagasin())
->filter(fn($magasin) => $magasin !== null)
->toArray();
}
}