src/Entity/User.php line 16

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. /**
  10.  * @ORM\Entity(repositoryClass=UserRepository::class)
  11.  * @ORM\Table(name="`user`")
  12.  */
  13. class User implements UserInterfacePasswordAuthenticatedUserInterface
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=180, unique=true)
  23.      */
  24.     private $username;
  25.     /**
  26.      * @ORM\Column(type="string", length=180, unique=true)
  27.      */
  28.     private $mail;
  29.     /**
  30.      * @ORM\Column(type="json")
  31.      */
  32.     private $roles = [];
  33.     /**
  34.      * @var string The hashed password
  35.      * @ORM\Column(type="string")
  36.      */
  37.     private $password;
  38.     /**
  39.      * @ORM\OneToOne(targetEntity=Carrito::class, mappedBy="usuario", cascade={"persist", "remove"})
  40.      */
  41.     private $carrito;
  42.     /**
  43.      * @ORM\OneToMany(targetEntity=Venta::class, mappedBy="usuario")
  44.      */
  45.     private $ventas;
  46.     /**
  47.      * @ORM\OneToOne(targetEntity=Direccion::class, cascade={"persist", "remove"})
  48.      */
  49.     private $direccion;
  50.     /**
  51.      * @ORM\OneToMany(targetEntity=Publicacion::class, mappedBy="autor")
  52.      */
  53.     private $publicacions;
  54.     /**
  55.      * @ORM\Column(type="string", length=255, nullable=true)
  56.      */
  57.     private $telefono;
  58.     /**
  59.      * @ORM\ManyToMany(targetEntity=Curso::class, inversedBy="users")
  60.      */
  61.     private $cursos;
  62.     /**
  63.      * @ORM\Column(type="string", length=255)
  64.      */
  65.     private $apellido;
  66.     /**
  67.      * @ORM\Column(type="string", length=255)
  68.      */
  69.     private $nombre;
  70.     /**
  71.      * @ORM\OneToOne(targetEntity=Imagen::class, cascade={"persist", "remove"})
  72.      */
  73.     private $avatar;
  74.     /**
  75.      * @ORM\OneToMany(targetEntity=Contacto::class, mappedBy="usuarioLogueado")
  76.      */
  77.     private $contactos;
  78.     public function __construct()
  79.     {
  80.         $this->ventas = new ArrayCollection();
  81.         $this->publicacions = new ArrayCollection();
  82.         $this->cursos = new ArrayCollection();
  83.         $this->contactos = new ArrayCollection();
  84.     }
  85.     public function getId(): ?int
  86.     {
  87.         return $this->id;
  88.     }
  89.     /**
  90.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  91.      */
  92.     public function getUsername(): string
  93.     {
  94.         return (string) $this->username;
  95.     }
  96.     public function setUsername(string $username): self
  97.     {
  98.         $this->username $username;
  99.         return $this;
  100.     }
  101.     public function getMail(): string
  102.     {
  103.         return (string) $this->mail;
  104.     }
  105.     public function setMail(string $mail): self
  106.     {
  107.         $this->mail $mail;
  108.         return $this;
  109.     }
  110.     /**
  111.      * A visual identifier that represents this user.
  112.      *
  113.      * @see UserInterface
  114.      */
  115.     public function getUserIdentifier(): string
  116.     {
  117.         return (string) $this->username;
  118.     }
  119.     //Funcion que me dice si el usuario tiene rol admin
  120.     public function isAdmin()
  121.     {
  122.         //Obtengo los roles
  123.         $rolesArray $this->getRoles();
  124.         //Defino el flag para el rol
  125.         $flagAdmin false;
  126.         foreach ($rolesArray as $rol) {
  127.             if ($rol == "ROLE_ADMIN") {
  128.                 $flagAdmin true;
  129.                 break;
  130.             }
  131.         }
  132.         return $flagAdmin;
  133.     }
  134.     /**
  135.      * @see UserInterface
  136.      */
  137.     public function getRoles(): array
  138.     {
  139.         $roles $this->roles;
  140.         // guarantee every user at least has ROLE_USER
  141.         $roles[] = 'ROLE_USER';
  142.         return array_unique($roles);
  143.     }
  144.     public function setRoles(array $roles): self
  145.     {
  146.         $this->roles $roles;
  147.         return $this;
  148.     }
  149.     /**
  150.      * @see PasswordAuthenticatedUserInterface
  151.      */
  152.     public function getPassword(): string
  153.     {
  154.         return $this->password;
  155.     }
  156.     public function setPassword(string $password): self
  157.     {
  158.         $this->password $password;
  159.         return $this;
  160.     }
  161.     /**
  162.      * Returning a salt is only needed, if you are not using a modern
  163.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  164.      *
  165.      * @see UserInterface
  166.      */
  167.     public function getSalt(): ?string
  168.     {
  169.         return null;
  170.     }
  171.     /**
  172.      * @see UserInterface
  173.      */
  174.     public function eraseCredentials()
  175.     {
  176.         // If you store any temporary, sensitive data on the user, clear it here
  177.         // $this->plainPassword = null;
  178.     }
  179.     public function __toString()
  180.     {
  181.         return strtoupper("$this->username");
  182.     }
  183.     public function getCarrito(): ?Carrito
  184.     {
  185.         return $this->carrito;
  186.     }
  187.     public function setCarrito(?Carrito $carrito): self
  188.     {
  189.         // unset the owning side of the relation if necessary
  190.         if ($carrito === null && $this->carrito !== null) {
  191.             $this->carrito->setUsuario(null);
  192.         }
  193.         // set the owning side of the relation if necessary
  194.         if ($carrito !== null && $carrito->getUsuario() !== $this) {
  195.             $carrito->setUsuario($this);
  196.         }
  197.         $this->carrito $carrito;
  198.         return $this;
  199.     }
  200.     /**
  201.      * @return Collection|Venta[]
  202.      */
  203.     public function getVentas(): Collection
  204.     {
  205.         return $this->ventas;
  206.     }
  207.     public function addVenta(Venta $venta): self
  208.     {
  209.         if (!$this->ventas->contains($venta)) {
  210.             $this->ventas[] = $venta;
  211.             $venta->setUsuario($this);
  212.         }
  213.         return $this;
  214.     }
  215.     public function removeVenta(Venta $venta): self
  216.     {
  217.         if ($this->ventas->removeElement($venta)) {
  218.             // set the owning side to null (unless already changed)
  219.             if ($venta->getUsuario() === $this) {
  220.                 $venta->setUsuario(null);
  221.             }
  222.         }
  223.         return $this;
  224.     }
  225.     public function getDireccion(): ?Direccion
  226.     {
  227.         return $this->direccion;
  228.     }
  229.     public function setDireccion(?Direccion $direccion): self
  230.     {
  231.         $this->direccion $direccion;
  232.         return $this;
  233.     }
  234.     /**
  235.      * @return Collection|Publicacion[]
  236.      */
  237.     public function getPublicacions(): Collection
  238.     {
  239.         return $this->publicacions;
  240.     }
  241.     public function addPublicacion(Publicacion $publicacion): self
  242.     {
  243.         if (!$this->publicacions->contains($publicacion)) {
  244.             $this->publicacions[] = $publicacion;
  245.             $publicacion->setAutor($this);
  246.         }
  247.         return $this;
  248.     }
  249.     public function removePublicacion(Publicacion $publicacion): self
  250.     {
  251.         if ($this->publicacions->removeElement($publicacion)) {
  252.             // set the owning side to null (unless already changed)
  253.             if ($publicacion->getAutor() === $this) {
  254.                 $publicacion->setAutor(null);
  255.             }
  256.         }
  257.         return $this;
  258.     }
  259.     public function getTelefono(): ?string
  260.     {
  261.         return $this->telefono;
  262.     }
  263.     public function setTelefono(?string $telefono): self
  264.     {
  265.         $this->telefono $telefono;
  266.         return $this;
  267.     }
  268.     //Funcion interna para verificar si un usuario compro el curso
  269.     public function getImagenCurso($idCurso)
  270.     {
  271.         $cursoSeleccionado null;
  272.         //Itero entre todos los cursos para saber si un usuario lo tiene o no
  273.         foreach ($this->getCursos() as $curso) {
  274.             if ($curso->getId() == $idCurso) {
  275.                 $cursoSeleccionado $curso;
  276.             }
  277.         }
  278.         if ($cursoSeleccionado != null) {
  279.             if ($cursoSeleccionado->getImagenes()) {
  280.                 //Obtengo Portada
  281.                 $imagen $cursoSeleccionado->getImagenes()->first();
  282.                 //Retorno
  283.                 return $imagen->getUrl();
  284.             }
  285.         }
  286.         //Si no lo encontre entre todos los cursos retorno false
  287.         return null;
  288.     }
  289.     //Funcion que me dice si un usuario posee un curso o no
  290.     public function poseeUnCurso($curso)
  291.     {
  292.         //Obtengo los cursos actuales..
  293.         $cursos $this->getCursos();
  294.         //Si el curso esta entre los cursos del usuario..
  295.         if ($cursos->contains($curso)) {
  296.             return true;
  297.         } else {
  298.             return false;
  299.         }
  300.     }
  301.     /**
  302.      * @return Collection<int, Curso>
  303.      */
  304.     public function getCursos(): Collection
  305.     {
  306.         return $this->cursos;
  307.     }
  308.     public function addCurso(Curso $curso): self
  309.     {
  310.         if (!$this->cursos->contains($curso)) {
  311.             $this->cursos[] = $curso;
  312.         }
  313.         return $this;
  314.     }
  315.     public function removeCurso(Curso $curso): self
  316.     {
  317.         $this->cursos->removeElement($curso);
  318.         return $this;
  319.     }
  320.     public function getApellido(): ?string
  321.     {
  322.         return $this->apellido;
  323.     }
  324.     public function setApellido(string $apellido): self
  325.     {
  326.         $this->apellido $apellido;
  327.         return $this;
  328.     }
  329.     public function getNombre(): ?string
  330.     {
  331.         return $this->nombre;
  332.     }
  333.     public function setNombre(string $nombre): self
  334.     {
  335.         $this->nombre $nombre;
  336.         return $this;
  337.     }
  338.     public function getAvatar(): ?Imagen
  339.     {
  340.         return $this->avatar;
  341.     }
  342.     public function setAvatar(?Imagen $avatar): self
  343.     {
  344.         $this->avatar $avatar;
  345.         return $this;
  346.     }
  347.     /**
  348.      * @return Collection<int, Contacto>
  349.      */
  350.     public function getContactos(): Collection
  351.     {
  352.         return $this->contactos;
  353.     }
  354.     public function addContacto(Contacto $contacto): self
  355.     {
  356.         if (!$this->contactos->contains($contacto)) {
  357.             $this->contactos[] = $contacto;
  358.             $contacto->setUsuarioLogueado($this);
  359.         }
  360.         return $this;
  361.     }
  362.     public function removeContacto(Contacto $contacto): self
  363.     {
  364.         if ($this->contactos->removeElement($contacto)) {
  365.             // set the owning side to null (unless already changed)
  366.             if ($contacto->getUsuarioLogueado() === $this) {
  367.                 $contacto->setUsuarioLogueado(null);
  368.             }
  369.         }
  370.         return $this;
  371.     }
  372.     public function getApellidoNombre(): string
  373.     {
  374.         return (string) $this->apellido "," $this->nombre;
  375.     }
  376.     public function getVentasEnProceso()
  377.     {
  378.         $ventasAux = new ArrayCollection();
  379.         foreach ($this->getVentas() as $venta) {
  380.             if ($venta->getEstado()->getId() == 2) {
  381.                 $ventasAux->add($venta);
  382.             }
  383.         }
  384.         return $ventasAux;
  385.     }
  386.     public function getVentasFinalizadas()
  387.     {
  388.         $ventasAux = new ArrayCollection();
  389.         foreach ($this->getVentas() as $venta) {
  390.             if ($venta->getEstado()->getId() == 3) {
  391.                 $ventasAux->add($venta);
  392.             }
  393.         }
  394.         return $ventasAux;
  395.     }
  396.     public function getVentasCanceladas()
  397.     {
  398.         $ventasAux = new ArrayCollection();
  399.         foreach ($this->getVentas() as $venta) {
  400.             if ($venta->getEstado()->getId() == 4) {
  401.                 $ventasAux->add($venta);
  402.             }
  403.         }
  404.         return $ventasAux;
  405.     }
  406.     public function getVentasRechazadas()
  407.     {
  408.         $ventasAux = new ArrayCollection();
  409.         foreach ($this->getVentas() as $venta) {
  410.             if ($venta->getEstado() == 5) {
  411.                 $ventasAux->add($venta);
  412.             }
  413.         }
  414.         return $ventasAux;
  415.     }
  416. }