vendor/sylius/sylius/src/Sylius/Component/Core/Model/Customer.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  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 Sylius\Component\Core\Model;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Sylius\Component\Customer\Model\Customer as BaseCustomer;
  15. use Sylius\Component\User\Model\UserInterface as BaseUserInterface;
  16. use Webmozart\Assert\Assert;
  17. class Customer extends BaseCustomer implements CustomerInterface
  18. {
  19.     /**
  20.      * @var Collection|OrderInterface[]
  21.      *
  22.      * @psalm-var Collection<array-key, OrderInterface>
  23.      */
  24.     protected $orders;
  25.     /** @var AddressInterface|null */
  26.     protected $defaultAddress;
  27.     /**
  28.      * @var Collection|AddressInterface[]
  29.      *
  30.      * @psalm-var Collection<array-key, AddressInterface>
  31.      */
  32.     protected $addresses;
  33.     /** @var ShopUserInterface|null */
  34.     protected $user;
  35.     public function __construct()
  36.     {
  37.         parent::__construct();
  38.         /** @var ArrayCollection<array-key, OrderInterface> $this->orders */
  39.         $this->orders = new ArrayCollection();
  40.         /** @var ArrayCollection<array-key, AddressInterface> $this->addresses */
  41.         $this->addresses = new ArrayCollection();
  42.     }
  43.     public function getOrders(): Collection
  44.     {
  45.         return $this->orders;
  46.     }
  47.     public function getDefaultAddress(): ?AddressInterface
  48.     {
  49.         return $this->defaultAddress;
  50.     }
  51.     public function setDefaultAddress(?AddressInterface $defaultAddress): void
  52.     {
  53.         $this->defaultAddress $defaultAddress;
  54.         if (null !== $defaultAddress) {
  55.             $this->addAddress($defaultAddress);
  56.         }
  57.     }
  58.     public function addAddress(AddressInterface $address): void
  59.     {
  60.         if (!$this->hasAddress($address)) {
  61.             $this->addresses[] = $address;
  62.             $address->setCustomer($this);
  63.         }
  64.     }
  65.     public function removeAddress(AddressInterface $address): void
  66.     {
  67.         $this->addresses->removeElement($address);
  68.         $address->setCustomer(null);
  69.     }
  70.     public function hasAddress(AddressInterface $address): bool
  71.     {
  72.         return $this->addresses->contains($address);
  73.     }
  74.     public function getAddresses(): Collection
  75.     {
  76.         return $this->addresses;
  77.     }
  78.     public function getUser(): ?BaseUserInterface
  79.     {
  80.         return $this->user;
  81.     }
  82.     public function setUser(?BaseUserInterface $user): void
  83.     {
  84.         if ($this->user === $user) {
  85.             return;
  86.         }
  87.         /** @var ShopUserInterface|null $user */
  88.         Assert::nullOrIsInstanceOf($userShopUserInterface::class);
  89.         $previousUser $this->user;
  90.         $this->user $user;
  91.         /** @psalm-suppress RedundantConditionGivenDocblockType */
  92.         if ($previousUser instanceof ShopUserInterface) {
  93.             $previousUser->setCustomer(null);
  94.         }
  95.         if ($user instanceof ShopUserInterface) {
  96.             $user->setCustomer($this);
  97.         }
  98.     }
  99.     public function hasUser(): bool
  100.     {
  101.         return null !== $this->user;
  102.     }
  103. }