custom/static-plugins/GlobusSW6/src/Subscriber/StoreLocator/CurrentStoreStockSubscriber.php line 65

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\StoreLocator;
  3. use GlobusSW6\Service\App\AppFromRequestIdentifier;
  4. use GlobusSW6\Service\App\AppIdentifier;
  5. use GlobusSW6\Service\App\AppService;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. use Shopware\Core\Content\Product\ProductCollection;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use GlobusSW6\Service\IaneoBusinessExtension;
  13. class CurrentStoreStockSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var SessionInterface */
  16.     private $session;
  17.     /** @var IaneoBusinessExtension */
  18.     private $ianeoBusinessExtensionService;
  19.     /** @var AppService */
  20.     private $appService;
  21.     /**
  22.      * @param SessionInterface $session
  23.      * @param IaneoBusinessExtension $ianeoBusinessExtensionService
  24.      * @param AppService $appService
  25.      */
  26.     public function __construct(SessionInterface $sessionIaneoBusinessExtension $ianeoBusinessExtensionServiceAppService $appService)
  27.     {
  28.         $this->session $session;
  29.         $this->ianeoBusinessExtensionService $ianeoBusinessExtensionService;
  30.         $this->appService $appService;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             'product.search.result.loaded' => 'appendCurrentStoreStock'
  36.         ];
  37.     }
  38.     public function appendCurrentStoreStock(EntitySearchResultLoadedEvent $event)
  39.     {
  40.         // do not fire event in backend context
  41.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  42.             if ($event->getContext()->getSource()->isAdmin()) {
  43.                 return;
  44.             }
  45.         }
  46.         // get products
  47.         /** @var ProductCollection $products */
  48.         $products $event->getResult()->getEntities();
  49.         if ($products === null) {
  50.             return;
  51.         }
  52.         // get current store
  53.         $currentStore $this->session->get('ianeoCurrentStore');
  54.         $currentStores $this->createFakeArray($products->getIds(), $currentStore);
  55.         // Take max quantity in store from checkoutInfos, as it's already calculated there
  56.         // TODO-NGS: having the $maxQuantityInStore information in two places is redundant
  57.         $stocks = [];
  58.         /** @var ProductEntity $product */
  59.         foreach ($products as $product){
  60.             if(array_key_exists('ianeoExtensions'$product->getExtensions())){
  61.                 if($product->getExtensions()['ianeoExtensions']->has('checkoutInfos')){
  62.                     $maxQuantityInStore $product->getExtensions()['ianeoExtensions']['checkoutInfos']->getMaxQuantityInStore();
  63.                     $stocks[$product->getId()] = (is_null($maxQuantityInStore)) ? null $maxQuantityInStore ;
  64.                 }
  65.             }
  66.         }
  67.         // insert stock in ianeoExtension
  68.         $this->ianeoBusinessExtensionService->addAttributes($products$currentStores'currentStore');
  69.         $this->ianeoBusinessExtensionService->addAttributes($products$stocks'storeStock');
  70.     }
  71.     // TODO-NGS: Just for testing, remove this later on
  72.     private function createFakeArray(array $ids$value)
  73.     {
  74.         $arr = [];
  75.         foreach ($ids as $id) {
  76.             $arr[$id] = $value;
  77.         }
  78.         return $arr;
  79.     }
  80. }