src/Customer/Entity/Customer.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Customer\Entity;
  3. use App\Authentication\Entity\User;
  4. use App\Order\Entity\Order;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use App\Framework\Entity\BlameableEntityTrait;
  12. use App\Framework\Entity\TimestampableEntityTrait;
  13. /**
  14.  * @ORM\Table(
  15.  *  indexes={
  16.  *      @ORM\Index(name="search", columns={"firstName", "lastName", "companyName"}, flags={"fulltext"}),
  17.  *      @ORM\Index(name="search_email", columns={"email"}, flags={"fulltext"})
  18.  *  }
  19.  * )
  20.  * @ORM\Entity(repositoryClass="App\Customer\Repository\CustomerRepository")
  21.  * @ORM\HasLifecycleCallbacks()
  22.  * @UniqueEntity(
  23.  *    fields={"email"},
  24.  *    message="error.user.email_used"
  25.  * )
  26.  */
  27. class Customer
  28. {
  29.     use BlameableEntityTrait;
  30.     use TimestampableEntityTrait;
  31.     /**
  32.      * @ORM\Column(type="integer")
  33.      * @ORM\Id
  34.      * @ORM\GeneratedValue(strategy="AUTO")
  35.      */
  36.     private int $id;
  37.     /**
  38.      * @ORM\Column(type="string", length=100, nullable=true, name="firstName")
  39.      */
  40.     private ?string $firstName null;
  41.     /**
  42.      * @ORM\Column(type="string", length=100, nullable=true, name="lastName")
  43.      */
  44.     private ?string $lastName null;
  45.     /**
  46.      * @ORM\Column(type="string", length=100, nullable=true, name="companyName")
  47.      */
  48.     private ?string $companyName null;
  49.     /**
  50.      * @ORM\Column(type="string", length=100, unique=true, nullable=true)
  51.      */
  52.     #[Assert\Email(message'El email \'{{ value }}\' no es valido.')]
  53.     private ?string $email null;
  54.     /**
  55.      * @ORM\Column(type="boolean", name="isWholesaler")
  56.      */
  57.     private bool $isWholesaler;
  58.     /**
  59.      * @ORM\OneToMany(targetEntity="App\Customer\Entity\StreetAddress", mappedBy="customer", cascade={"persist", "remove"})
  60.      */
  61.     private Collection $streetAddresses;
  62.     /**
  63.      * @ORM\OneToMany(targetEntity="BillingAddress", mappedBy="customer", cascade={"persist", "remove"})
  64.      */
  65.     private Collection $billingAddresses;
  66.     /**
  67.      * @ORM\OneToMany(targetEntity="App\Order\Entity\Order", mappedBy="customer")
  68.      */
  69.     private Collection $orders;
  70.     /**
  71.      * @ORM\Column(type="boolean")
  72.      */
  73.     private bool $active;
  74.     /**
  75.      * @ORM\ManyToOne(targetEntity="App\Authentication\Entity\User")
  76.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  77.      */
  78.     private ?User $user null;
  79.     public function __construct()
  80.     {
  81.         $this->active true;
  82.         $this->isWholesaler false;
  83.         $this->streetAddresses = new ArrayCollection();
  84.         $this->billingAddresses = new ArrayCollection();
  85.         $this->orders = new ArrayCollection();
  86.     }
  87.     public function getId(): int
  88.     {
  89.         return $this->id;
  90.     }
  91.     public function getFirstName(): ?string
  92.     {
  93.         return $this->firstName;
  94.     }
  95.     public function getLastName(): ?string
  96.     {
  97.         return $this->lastName;
  98.     }
  99.     public function getFullName(): string
  100.     {
  101.         return ($this->isWholesaler && !empty($this->companyName)) ? $this->companyName : ($this->firstName ' ' $this->lastName);
  102.     }
  103.     public function getCompanyName(): ?string
  104.     {
  105.         return $this->companyName;
  106.     }
  107.     public function getEmail(): ?string
  108.     {
  109.         return $this->email;
  110.     }
  111.     public function getIsWholesaler(): bool
  112.     {
  113.         return $this->isWholesaler;
  114.     }
  115.     public function setId(int $id): void
  116.     {
  117.         $this->id $id;
  118.     }
  119.     public function setFirstName(?string $firstName): void
  120.     {
  121.         $this->firstName $firstName;
  122.     }
  123.     public function setLastName(?string $lastName): void
  124.     {
  125.         $this->lastName $lastName;
  126.     }
  127.     public function setCompanyName(?string $companyName): void
  128.     {
  129.         $this->companyName $companyName;
  130.     }
  131.     public function setEmail(?string $email): void
  132.     {
  133.         $this->email $email;
  134.     }
  135.     public function setIsWholesaler(bool $isWholesaler): void
  136.     {
  137.         $this->isWholesaler $isWholesaler;
  138.     }
  139.     public function getDisplayName(): ?string
  140.     {
  141.         if ($this->firstName && $this->lastName) {
  142.             return $this->lastName ' ' $this->firstName;
  143.         } elseif ($this->companyName) {
  144.             return $this->companyName;
  145.         }
  146.         return $this->firstName;
  147.     }
  148.     public function addShippingAddress(StreetAddress $streetAddresses): void
  149.     {
  150.         $this->streetAddresses[] = $streetAddresses;
  151.     }
  152.     public function removeShippingAddress(StreetAddress $streetAddresses): void
  153.     {
  154.         $this->streetAddresses->removeElement($streetAddresses);
  155.     }
  156.     /**
  157.      * @return StreetAddress[]|Collection
  158.      */
  159.     public function getShippingAddresses(): Collection
  160.     {
  161.         return $this->streetAddresses;
  162.     }
  163.     public function addBillingAddress(BillingAddress $billingAddress): void
  164.     {
  165.         $this->billingAddresses[] = $billingAddress;
  166.     }
  167.     public function removeBillingAddress(BillingAddress $billingAddress): void
  168.     {
  169.         $this->billingAddresses->removeElement($billingAddress);
  170.     }
  171.     public function getBillingAddresses(): Collection
  172.     {
  173.         return $this->billingAddresses;
  174.     }
  175.     public function addOrder(Order $orders): void
  176.     {
  177.         $this->orders[] = $orders;
  178.     }
  179.     public function removeOrder(Order $orders): void
  180.     {
  181.         $this->orders->removeElement($orders);
  182.     }
  183.     public function getOrders(): Collection
  184.     {
  185.         return $this->orders;
  186.     }
  187.     public function setActive(bool $active): void
  188.     {
  189.         $this->active $active;
  190.     }
  191.     public function getActive(): bool
  192.     {
  193.         return $this->active;
  194.     }
  195.     /**
  196.      * @param User|null $user
  197.      */
  198.     public function setUser(?User $user): void
  199.     {
  200.         $this->user $user;
  201.     }
  202.     public function getUser(): ?User
  203.     {
  204.         return $this->user;
  205.     }
  206.     public function hasUser(): bool
  207.     {
  208.         return $this->getUser() !== null;
  209.     }
  210. }