src/Entity/User.php line 20

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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Entity(repositoryClass=UserRepository::class)
  13.  * @ORM\Table(name="`user`")
  14.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  15.  */
  16. class User implements UserInterfacePasswordAuthenticatedUserInterface
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="string", length=180, unique=true)
  26.      */
  27.     #[Assert\Email]
  28.     private $email;
  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\ManyToOne(targetEntity=Shop::class, inversedBy="users")
  40.      * @ORM\JoinColumn(nullable=true)
  41.      */
  42.     private $shop;
  43.     /**
  44.      * @ORM\ManyToMany(targetEntity=Shop::class, inversedBy="users")
  45.      * #[JoinTable(name: 'user_shop')]
  46.      */
  47.     private Collection $shops;
  48.     /**
  49.      * @ORM\Column(type="boolean")
  50.      */
  51.     private $isVerified false;
  52.     /**
  53.      * @ORM\OneToMany(targetEntity=Order::class, mappedBy="user")
  54.      */
  55.     private $orders;
  56.     /**
  57.      * @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
  58.      */
  59.     private $addresses;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="users")
  62.      */
  63.     private $company;
  64.     /**
  65.      * @ORM\Column(type="string", length=255, nullable=true)
  66.      */
  67.     private $firstname;
  68.     /**
  69.      * @ORM\Column(type="string", length=255, nullable=true)
  70.      */
  71.     private $lastname;
  72.     /**
  73.      * @ORM\Column(type="string", length=128, nullable=true)
  74.      * @Assert\Length(min = 10, max = 20, minMessage = "min_lenght", maxMessage = "max_lenght")
  75.      */
  76.     private $phone;
  77.     /**
  78.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="childrenUsers")
  79.      */
  80.     private $parent;
  81.     /**
  82.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="parent")
  83.      */
  84.     private $childrenUsers;
  85.     /**
  86.      * @ORM\OneToMany(targetEntity=UserSetting::class, mappedBy="user", cascade={"persist"})
  87.      */
  88.     private $settings;
  89.     /**
  90.      * @ORM\Column(type="boolean", nullable=true)
  91.      */
  92.     private $active;
  93.     /**
  94.      * @ORM\OneToMany(targetEntity=Sav::class, mappedBy="user")
  95.      */
  96.     private $savs;
  97.     public function __construct()
  98.     {
  99.         $this->orders = new ArrayCollection();
  100.         $this->addresses = new ArrayCollection();
  101.         $this->childrenUsers = new ArrayCollection();
  102.         $this->settings = new ArrayCollection();
  103.         $this->shops = new ArrayCollection();
  104.         $this->savs = new ArrayCollection();
  105.     }
  106.     public function __toString()
  107.     {
  108.         return $this->getEmail();
  109.     }
  110.     public function getId(): ?int
  111.     {
  112.         return $this->id;
  113.     }
  114.     public function getEmail(): ?string
  115.     {
  116.         return $this->email;
  117.     }
  118.     public function setEmail(string $email): self
  119.     {
  120.         $this->email $email;
  121.         return $this;
  122.     }
  123.     /**
  124.      * A visual identifier that represents this user.
  125.      *
  126.      * @see UserInterface
  127.      */
  128.     public function getUserIdentifier(): string
  129.     {
  130.         return (string)$this->email;
  131.     }
  132.     /**
  133.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  134.      */
  135.     public function getUsername(): string
  136.     {
  137.         return (string)$this->email;
  138.     }
  139.     /**
  140.      * @see UserInterface
  141.      */
  142.     public function getRoles(): array
  143.     {
  144.         $roles $this->roles;
  145.         // guarantee every user at least has ROLE_USER
  146.         $roles[] = 'ROLE_USER';
  147.         return array_unique($roles);
  148.     }
  149.     public function setRoles(array $roles): self
  150.     {
  151.         $this->roles $roles;
  152.         return $this;
  153.     }
  154.     /**
  155.      * @see PasswordAuthenticatedUserInterface
  156.      */
  157.     public function getPassword(): string
  158.     {
  159.         return $this->password;
  160.     }
  161.     public function setPassword(string $password): self
  162.     {
  163.         $this->password $password;
  164.         return $this;
  165.     }
  166.     /**
  167.      * Returning a salt is only needed, if you are not using a modern
  168.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  169.      *
  170.      * @see UserInterface
  171.      */
  172.     public function getSalt(): ?string
  173.     {
  174.         return null;
  175.     }
  176.     /**
  177.      * @see UserInterface
  178.      */
  179.     public function eraseCredentials()
  180.     {
  181.         // If you store any temporary, sensitive data on the user, clear it here
  182.         // $this->plainPassword = null;
  183.     }
  184.     public function getShop(): ?Shop
  185.     {
  186.         return $this->shop;
  187.     }
  188.     public function setShop(?Shop $shop): self
  189.     {
  190.         $this->shop $shop;
  191.         return $this;
  192.     }
  193.     public function isVerified(): bool
  194.     {
  195.         return $this->isVerified;
  196.     }
  197.     public function setIsVerified(bool $isVerified): self
  198.     {
  199.         $this->isVerified $isVerified;
  200.         return $this;
  201.     }
  202.     /**
  203.      * @return Collection<int, Order>
  204.      */
  205.     public function getOrders(): Collection
  206.     {
  207.         return $this->orders;
  208.     }
  209.     public function addOrder(Order $order): self
  210.     {
  211.         if (!$this->orders->contains($order)) {
  212.             $this->orders[] = $order;
  213.             $order->setUser($this);
  214.         }
  215.         return $this;
  216.     }
  217.     public function removeOrder(Order $order): self
  218.     {
  219.         if ($this->orders->removeElement($order)) {
  220.             // set the owning side to null (unless already changed)
  221.             if ($order->getUser() === $this) {
  222.                 $order->setUser(null);
  223.             }
  224.         }
  225.         return $this;
  226.     }
  227.     /**
  228.      * @return Collection<int, Address>
  229.      */
  230.     public function getAddresses(): Collection
  231.     {
  232.         //
  233.         //
  234.         //echo "e";
  235.         //die();
  236.         if($this->getCompany()) {
  237.            // echo "1";
  238.             if($this->getCompany()->getAddress()) {
  239.                 if (!$this->addresses->contains($this->getCompany()->getAddress())) {
  240.                     $this->addresses[] = $this->getCompany()->getAddress();
  241.                 }
  242.             }
  243.         }
  244.         return $this->addresses;
  245.     }
  246.     public function addAddress(Address $address): self
  247.     {
  248.         if (!$this->addresses->contains($address)) {
  249.             $this->addresses[] = $address;
  250.             $address->setUser($this);
  251.         }
  252.         return $this;
  253.     }
  254.     public function removeAddress(Address $address): self
  255.     {
  256.         if ($this->addresses->removeElement($address)) {
  257.             // set the owning side to null (unless already changed)
  258.             if ($address->getUser() === $this) {
  259.                 $address->setUser(null);
  260.             }
  261.         }
  262.         return $this;
  263.     }
  264.     public function getCompany(): ?Company
  265.     {
  266.         return $this->company;
  267.     }
  268.     public function setCompany(?Company $company): self
  269.     {
  270.         $this->company $company;
  271.         return $this;
  272.     }
  273.     public function getFirstname(): ?string
  274.     {
  275.         return $this->firstname;
  276.     }
  277.     public function setFirstname(string $firstname): self
  278.     {
  279.         $this->firstname $firstname;
  280.         return $this;
  281.     }
  282.     public function getLastname(): ?string
  283.     {
  284.         return $this->lastname;
  285.     }
  286.     public function setLastname(string $lastname): self
  287.     {
  288.         $this->lastname $lastname;
  289.         return $this;
  290.     }
  291.     public function getPhone(): ?string
  292.     {
  293.         return $this->phone;
  294.     }
  295.     public function setPhone(string $phone): self
  296.     {
  297.         $this->phone $phone;
  298.         return $this;
  299.     }
  300.     public function getParent(): ?self
  301.     {
  302.         return $this->parent;
  303.     }
  304.     public function setParent(?self $parent): self
  305.     {
  306.         $this->parent $parent;
  307.         return $this;
  308.     }
  309.     /**
  310.      * @return Collection<int, self>
  311.      */
  312.     public function getChildrenUsers(): Collection
  313.     {
  314.         return $this->childrenUsers;
  315.     }
  316.     public function addChildrenUser(self $childrenUser): self
  317.     {
  318.         if (!$this->childrenUsers->contains($childrenUser)) {
  319.             $this->childrenUsers[] = $childrenUser;
  320.             $childrenUser->setParent($this);
  321.         }
  322.         return $this;
  323.     }
  324.     public function removeChildrenUser(self $childrenUser): self
  325.     {
  326.         if ($this->childrenUsers->removeElement($childrenUser)) {
  327.             // set the owning side to null (unless already changed)
  328.             if ($childrenUser->getParent() === $this) {
  329.                 $childrenUser->setParent(null);
  330.             }
  331.         }
  332.         return $this;
  333.     }
  334.     /**
  335.      * @return Collection<int, UserSetting>
  336.      */
  337.     public function getSettings(): Collection
  338.     {
  339.         return $this->settings;
  340.     }
  341.     public function getSetting($name)
  342.     {
  343.         foreach ($this->getSettings() as $setting) {
  344.             if ($setting->getSetting()->getName() == $name) {
  345.                 if ($setting->getValue() == 'false') {
  346.                     return false;
  347.                 }
  348.                 return $setting->getValue();
  349.             }
  350.         }
  351.         return false;
  352.     }
  353.     public function addSetting(UserSetting $setting): void
  354.     {
  355.         $setting->setUser($this);
  356.         $this->settings->add($setting);
  357.         /*if (!$this->settings->contains($setting)) {
  358.             $this->settings[] = $setting;
  359.             $setting->setUser($this);
  360.         }
  361.         return $this;
  362.         */
  363.     }
  364.     public function removeSetting(UserSetting $setting): void
  365.     {
  366.         /*if ($this->settings->removeElement($setting)) {
  367.             // set the owning side to null (unless already changed)
  368.             if ($setting->getUser() === $this) {
  369.                 $setting->setUser(null);
  370.             }
  371.         }
  372.         return $this;*/
  373.     }
  374.     public function isGranted($role)
  375.     {
  376.         return in_array($role$this->getRoles());
  377.     }
  378.     public function isIsVerified(): ?bool
  379.     {
  380.         return $this->isVerified;
  381.     }
  382.     /**
  383.      * @return Collection<int, Shop>
  384.      */
  385.     public function getShops(): Collection
  386.     {
  387.         return $this->shops;
  388.     }
  389.     public function addShop(Shop $shop): static
  390.     {
  391.         if (!$this->shops->contains($shop)) {
  392.             $this->shops->add($shop);
  393.         }
  394.         return $this;
  395.     }
  396.     public function removeShop(Shop $shop): static
  397.     {
  398.         $this->shops->removeElement($shop);
  399.         return $this;
  400.     }
  401.     public function getActive():bool
  402.     {
  403.         return $this->active;
  404.     }
  405.     public function setActive(bool $active):self
  406.     {
  407.         $this->active $active;
  408.         return $this;
  409.     }
  410.     public function isActive(): ?bool
  411.     {
  412.         return $this->active;
  413.     }
  414.     /**
  415.      * @return Collection<int, Sav>
  416.      */
  417.     public function getSavs(): Collection
  418.     {
  419.         return $this->savs;
  420.     }
  421.     public function addSav(Sav $sav): static
  422.     {
  423.         if (!$this->savs->contains($sav)) {
  424.             $this->savs->add($sav);
  425.             $sav->setUser($this);
  426.         }
  427.         return $this;
  428.     }
  429.     public function removeSav(Sav $sav): static
  430.     {
  431.         if ($this->savs->removeElement($sav)) {
  432.             // set the owning side to null (unless already changed)
  433.             if ($sav->getUser() === $this) {
  434.                 $sav->setUser(null);
  435.             }
  436.         }
  437.         return $this;
  438.     }
  439. }