src/Entity/Customer/Customer.php line 35

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Entity\Customer;
  12. use App\Entity\Device\Device;
  13. use App\Entity\Geographical\Address;
  14. use App\Entity\Order\Order;
  15. use App\Entity\User\AppUser;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Doctrine\Common\Collections\Collection;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Monofony\Contracts\Core\Model\Customer\CustomerInterface;
  20. use Monofony\Contracts\Core\Model\User\AppUserInterface;
  21. use Sylius\Component\Customer\Model\Customer as BaseCustomer;
  22. use Sylius\Component\User\Model\UserInterface;
  23. use Symfony\Component\Validator\Constraints\Length;
  24. use Symfony\Component\Validator\Constraints\NotBlank;
  25. use Symfony\Component\Validator\Constraints\PositiveOrZero;
  26. use Symfony\Component\Validator\Constraints\Valid;
  27. use Webmozart\Assert\Assert;
  28. #[ORM\Entity]
  29. #[ORM\Table(name'sylius_customer')]
  30. class Customer extends BaseCustomer implements CustomerInterface
  31. {
  32.     #[ORM\OneToOne(mappedBy'customer'targetEntityAppUser::class, cascade: ['persist'])]
  33.     #[Valid]
  34.     private ?UserInterface $user null;
  35.     #[ORM\OneToMany(mappedBy'customer'targetEntityAddress::class, cascade: ['persist'], orphanRemovaltrue)]
  36.     #[Valid]
  37.     private Collection $addresses;
  38.     #[ORM\OneToMany(mappedBy'customer'targetEntityOrder::class, orphanRemovaltrue)]
  39.     private Collection $orders;
  40.     #[ORM\Column(type'integer')]
  41.     #[NotBlank(groups: ['sylius']), PositiveOrZero(groups: ['sylius'])]
  42.     private ?int $credits 0;
  43.     #[ORM\Column(type'string'nullabletrue)]
  44.     private ?string $street null;
  45.     #[ORM\Column(type'string'nullabletrue)]
  46.     private ?string $street2 null;
  47.     #[ORM\Column(type'string'nullabletrue)]
  48.     private ?string $city null;
  49.     #[ORM\Column(type'string'nullabletrue)]
  50.     private ?string $state null;
  51.     #[ORM\Column(type'string'length20nullabletrue)]
  52.     private ?string $postcode null;
  53.     #[ORM\Column(type'string'length100nullabletrue)]
  54.     private ?string $country null;
  55.     #[ORM\OneToMany(mappedBy'customer'targetEntityDevice::class)]
  56.     private Collection $devices;
  57.     public function __construct()
  58.     {
  59.         $this->addresses = new ArrayCollection();
  60.         $this->orders = new ArrayCollection();
  61.         $this->devices = new ArrayCollection();
  62.         parent::__construct();
  63.     }
  64.     public function getUser(): ?UserInterface
  65.     {
  66.         return $this->user;
  67.     }
  68.     public function setUser(?UserInterface $user): void
  69.     {
  70.         if ($this->user === $user) {
  71.             return;
  72.         }
  73.         Assert::nullOrIsInstanceOf($userAppUserInterface::class);
  74.         $previousUser $this->user;
  75.         $this->user $user;
  76.         if ($previousUser instanceof AppUserInterface) {
  77.             $previousUser->setCustomer(null);
  78.         }
  79.         if ($user instanceof AppUserInterface) {
  80.             $user->setCustomer($this);
  81.         }
  82.     }
  83.     public function setEnabled(?bool $enabled): void
  84.     {
  85.         if (null !== $this->user) {
  86.             $this->user->setEnabled($enabled);
  87.         }
  88.     }
  89.     public function getAddresses(): ?Collection
  90.     {
  91.         return $this->addresses;
  92.     }
  93.     public function hasAddress(Address $address): bool
  94.     {
  95.         return $this->addresses->contains($address);
  96.     }
  97.     public function addAddress(Address $address): void
  98.     {
  99.         if (!$this->hasAddress($address)) {
  100.             $this->addresses->add($address);
  101.             $address->setCustomer($this);
  102.         }
  103.     }
  104.     public function removeAddress(Address $address): void
  105.     {
  106.         $this->addresses->removeElement($address);
  107.         $address->setCustomer(null);
  108.     }
  109.     public function getOrders(): ?Collection
  110.     {
  111.         return $this->orders;
  112.     }
  113.     public function hasOrder(Order $order): bool
  114.     {
  115.         return $this->orders->contains($order);
  116.     }
  117.     public function addOrder(Order $order): void
  118.     {
  119.         if (!$this->hasOrder($order)) {
  120.             $this->orders->add($order);
  121.             $order->setCustomer($this);
  122.         }
  123.     }
  124.     public function removeOrder(Order $order): void
  125.     {
  126.         $this->orders->removeElement($order);
  127.         $order->setCustomer(null);
  128.     }
  129.     /**
  130.      * Get the value of credits.
  131.      *
  132.      * @return ?int
  133.      */
  134.     public function getCredits(): ?int
  135.     {
  136.         return $this->credits;
  137.     }
  138.     /**
  139.      * Set the value of credits.
  140.      */
  141.     public function setCredits(?int $credits): self
  142.     {
  143.         $this->credits $credits;
  144.         return $this;
  145.     }
  146.     /**
  147.      * Get the value of street.
  148.      *
  149.      * @return ?string
  150.      */
  151.     public function getStreet(): ?string
  152.     {
  153.         return $this->street;
  154.     }
  155.     /**
  156.      * Set the value of street.
  157.      */
  158.     public function setStreet(?string $street): self
  159.     {
  160.         $this->street $street;
  161.         return $this;
  162.     }
  163.     /**
  164.      * Get the value of street2.
  165.      *
  166.      * @return ?string
  167.      */
  168.     public function getStreet2(): ?string
  169.     {
  170.         return $this->street2;
  171.     }
  172.     /**
  173.      * Set the value of street2.
  174.      */
  175.     public function setStreet2(?string $street2): self
  176.     {
  177.         $this->street2 $street2;
  178.         return $this;
  179.     }
  180.     /**
  181.      * Get the value of city.
  182.      *
  183.      * @return ?string
  184.      */
  185.     public function getCity(): ?string
  186.     {
  187.         return $this->city;
  188.     }
  189.     /**
  190.      * Set the value of city.
  191.      */
  192.     public function setCity(?string $city): self
  193.     {
  194.         $this->city $city;
  195.         return $this;
  196.     }
  197.     /**
  198.      * Get the value of postcode.
  199.      *
  200.      * @return ?string
  201.      */
  202.     public function getPostcode(): ?string
  203.     {
  204.         return $this->postcode;
  205.     }
  206.     /**
  207.      * Set the value of postcode.
  208.      */
  209.     public function setPostcode(?string $postcode): self
  210.     {
  211.         $this->postcode $postcode;
  212.         return $this;
  213.     }
  214.     /**
  215.      * @return string|null
  216.      */
  217.     public function getState(): ?string
  218.     {
  219.         return $this->state;
  220.     }
  221.     /**
  222.      * @param string|null $state
  223.      */
  224.     public function setState(?string $state): void
  225.     {
  226.         $this->state $state;
  227.     }
  228.     /**
  229.      * Get the value of country.
  230.      *
  231.      * @return ?string
  232.      */
  233.     public function getCountry(): ?string
  234.     {
  235.         return $this->country;
  236.     }
  237.     /**
  238.      * Set the value of country.
  239.      */
  240.     public function setCountry(?string $country): self
  241.     {
  242.         $this->country $country;
  243.         return $this;
  244.     }
  245.     public function getDevices(): Collection
  246.     {
  247.         return $this->devices;
  248.     }
  249.     public function hasDevice(Device $device): bool
  250.     {
  251.         return $this->devices->contains($device);
  252.     }
  253.     public function addDevice(Device $device): void
  254.     {
  255.         if (!$this->hasDevice($device)) {
  256.             $this->devices->add($device);
  257.         }
  258.     }
  259.     public function removeDevice(Device $device): void
  260.     {
  261.         $this->devices->removeElement($device);
  262.     }
  263.     public function removeDeviceWithToken(string $token): void
  264.     {
  265.         $device $this->devices->filter(function (Device $device) use ($token) {
  266.             return $device->getToken() === $token;
  267.         })->first();
  268.         if ($device) {
  269.             $this->devices->removeElement($device);
  270.         }
  271.     }
  272.     public function getBillingAddress(): ?string
  273.     {
  274.         return sprintf(
  275.             '%s %s %s %s %s %s',
  276.             $this->getFullName(),
  277.             $this->getStreet(),
  278.             $this->getStreet2(),
  279.             $this->getPostcode(),
  280.             $this->getCity(),
  281.             $this->getCountry(),
  282.         );
  283.     }
  284. }