vendor/sylius/sylius/src/Sylius/Component/Core/Model/Shipment.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\Order\Model\AdjustmentInterface as BaseAdjustmentInterface;
  15. use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
  16. use Sylius\Component\Shipping\Model\Shipment as BaseShipment;
  17. class Shipment extends BaseShipment implements ShipmentInterface
  18. {
  19.     /** @var BaseOrderInterface|null */
  20.     protected $order;
  21.     /**
  22.      * @var Collection|BaseAdjustmentInterface[]
  23.      *
  24.      * @psalm-var Collection<array-key, BaseAdjustmentInterface>
  25.      */
  26.     protected $adjustments;
  27.     /**
  28.      * @var int
  29.      */
  30.     protected $adjustmentsTotal 0;
  31.     public function __construct()
  32.     {
  33.         parent::__construct();
  34.         /** @var ArrayCollection<array-key, BaseAdjustmentInterface> $this->adjustments */
  35.         $this->adjustments = new ArrayCollection();
  36.     }
  37.     public function getOrder(): ?BaseOrderInterface
  38.     {
  39.         return $this->order;
  40.     }
  41.     public function setOrder(?BaseOrderInterface $order): void
  42.     {
  43.         $this->order $order;
  44.     }
  45.     public function getAdjustments(?string $type null): Collection
  46.     {
  47.         if (null === $type) {
  48.             return $this->adjustments;
  49.         }
  50.         return $this->adjustments->filter(function (AdjustmentInterface $adjustment) use ($type): bool {
  51.             return $type === $adjustment->getType();
  52.         });
  53.     }
  54.     public function addAdjustment(BaseAdjustmentInterface $adjustment): void
  55.     {
  56.         /** @var AdjustmentInterface $adjustment */
  57.         if ($this->hasAdjustment($adjustment)) {
  58.             return;
  59.         }
  60.         $this->adjustments->add($adjustment);
  61.         $adjustment->setShipment($this);
  62.         $this->recalculateAdjustmentsTotal();
  63.         $this->order->recalculateAdjustmentsTotal();
  64.     }
  65.     public function removeAdjustment(BaseAdjustmentInterface $adjustment): void
  66.     {
  67.         /** @var AdjustmentInterface $adjustment */
  68.         if ($adjustment->isLocked() || !$this->hasAdjustment($adjustment)) {
  69.             return;
  70.         }
  71.         $this->adjustments->removeElement($adjustment);
  72.         $adjustment->setShipment(null);
  73.         $this->recalculateAdjustmentsTotal();
  74.         $this->order->recalculateAdjustmentsTotal();
  75.     }
  76.     public function hasAdjustment(BaseAdjustmentInterface $adjustment): bool
  77.     {
  78.         return $this->adjustments->contains($adjustment);
  79.     }
  80.     public function getAdjustmentsTotal(?string $type null): int
  81.     {
  82.         $total 0;
  83.         foreach ($this->getAdjustments($type) as $adjustment) {
  84.             if (!$adjustment->isNeutral()) {
  85.                 $total += $adjustment->getAmount();
  86.             }
  87.         }
  88.         return $total;
  89.     }
  90.     public function removeAdjustments(?string $type null): void
  91.     {
  92.         foreach ($this->getAdjustments($type) as $adjustment) {
  93.             $this->removeAdjustment($adjustment);
  94.         }
  95.     }
  96.     public function recalculateAdjustmentsTotal(): void
  97.     {
  98.         $this->adjustmentsTotal $this->getAdjustmentsTotal();
  99.     }
  100. }