custom/plugins/RhiemRentalProducts/src/Subscriber/RentalProduct.php line 78

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rhiem\RhiemRentalProducts\Subscriber;
  4. use League\Period\Exception;
  5. use Rhiem\RhiemRentalProducts\Components\RentalProduct\DAL\RentalCheapestPriceContainer;
  6. use Rhiem\RhiemRentalProducts\Components\RentalProduct\RentalProductModeInterface;
  7. use Rhiem\RhiemRentalProducts\Components\RentalTime\RentalTime;
  8. use Rhiem\RhiemRentalProducts\Entities\RentalProduct\RentalProductEntity;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  10. use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class RentalProduct implements EventSubscriberInterfaceRentalProductModeInterface
  16. {
  17.     /**
  18.      * @var EntityRepositoryInterface
  19.      */
  20.     private $rentalProductRepository;
  21.     public function __construct(EntityRepositoryInterface $rentalProductRepository)
  22.     {
  23.         $this->rentalProductRepository $rentalProductRepository;
  24.     }
  25.     /**
  26.      * @return array
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             'rental_product.written' => 'onRentalProductWritten',
  32.             'order_line_item.loaded' => 'onOrderLineItemLoaded',
  33.             'rental_product.loaded' => 'loaded',
  34.         ];
  35.     }
  36.     /**
  37.      * @throws Exception
  38.      */
  39.     public function onRentalProductWritten(EntityWrittenEvent $event): void
  40.     {
  41.         $writeResults $event->getWriteResults();
  42.         foreach ($writeResults as $writeResult) {
  43.             $payload $writeResult->getPayload();
  44.             $rentalTimes = [];
  45.             if (!empty($payload['blockedPeriods'])) {
  46.                 foreach ($payload['blockedPeriods'] as $blockedPeriod) {
  47.                     $rentalTime RentalTime::createRentalTime(
  48.                         $payload['productId'],
  49.                         $blockedPeriod['blocked_quantity'],
  50.                         $blockedPeriod['rhiem_rental_products_rent_start'],
  51.                         $blockedPeriod['rhiem_rental_products_rent_end'],
  52.                         'blocked',
  53.                         self::DAYRENT,
  54.                         'UTC'
  55.                     );
  56.                     $rentalTimes[] = serialize($rentalTime);
  57.                 }
  58.                 $this->rentalProductRepository->upsert(
  59.                     [
  60.                         [
  61.                             'id' => $payload['id'],
  62.                             'rentalTimes' => $rentalTimes,
  63.                         ],
  64.                     ],
  65.                     $event->getContext()
  66.                 );
  67.             }
  68.         }
  69.     }
  70.     public function onOrderLineItemLoaded(EntityLoadedEvent $event): void
  71.     {
  72.         /** @var OrderLineItemEntity $entity */
  73.         foreach ($event->getEntities() as $entity) {
  74.             $payload $entity->getPayload();
  75.             if (!empty($payload['rentalProduct']) && !empty($payload['rentalProduct']['rentalTime'])) {
  76.                 $rentalTime unserialize($payload['rentalProduct']['rentalTime']);
  77.                 $payload['rentalProduct']['rentalTime'] = $rentalTime;
  78.                 $entity->setPayload($payload);
  79.             }
  80.         }
  81.     }
  82.     public function loaded(EntityLoadedEvent $event): void
  83.     {
  84.         /** @var RentalProductEntity $rentalProduct */
  85.         foreach ($event->getEntities() as $rentalProduct) {
  86.             $price $rentalProduct->getCheapestPrice();
  87.             if ($price instanceof CheapestPriceContainer) {
  88.                 $rentalPrice = new RentalCheapestPriceContainer($price);
  89.                 $resolved $rentalPrice->resolve($event->getContext());
  90.                 $rentalProduct->setCheapestPriceContainer($price);
  91.                 $rentalProduct->setCheapestPrice($resolved);
  92.             }
  93.         }
  94.     }
  95. }