custom/plugins/CogiLicenseManager/src/Subscriber/Product.php line 134

Open in your IDE?
  1. <?php
  2. namespace Cogi\CogiLicenseManager\Subscriber;
  3. use Cogi\CogiLicenseManager\Core\License\LicenseEntity;
  4. use Cogi\CogiLicenseManager\Core\LicenseKey\LicenseKeyEntity;
  5. use Cogi\CogiLicenseManager\Core\LicenseProduct\LicenseProductEntity;
  6. use Cogi\CogiLicenseManager\Service\LicenseManagerService;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class Product implements EventSubscriberInterface {
  19.     /**
  20.      * @var SalesChannelContext
  21.      */
  22.     protected $salesChannelContext;
  23.     /**
  24.      * @var array
  25.      */
  26.     protected $config;
  27.     /**
  28.      * @var EntityRepositoryInterface
  29.      */
  30.     protected $licenseProductRepository;
  31.     /**
  32.      * @var EntityRepositoryInterface
  33.      */
  34.     protected $licenseRepository;
  35.     /**
  36.      * @var EntityRepositoryInterface
  37.      */
  38.     protected $licensePoolRepository;
  39.     /**
  40.      * @var LicenseManagerService
  41.      */
  42.     protected LicenseManagerService $licenseService;
  43.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $licenseProductRepositoryEntityRepositoryInterface $licenseRepositoryEntityRepositoryInterface $licensePoolRepositoryLicenseManagerService $licenseService) {
  44.         $this->config $systemConfigService->getDomain('CogiLicenseManager.config');
  45.         $this->licenseProductRepository $licenseProductRepository;
  46.         $this->licenseRepository $licenseRepository;
  47.         $this->licensePoolRepository $licensePoolRepository;
  48.         $this->licenseService $licenseService;
  49.     }
  50.     public static function getSubscribedEvents(): array {
  51.         return [
  52.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
  53.             SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved',
  54.         ];
  55.     }
  56.     /**
  57.      * @param EntityLoadedEvent $event
  58.      */
  59.     public function onProductLoaded(EntityLoadedEvent $event): void {
  60.         if (!($this->salesChannelContext instanceof SalesChannelContext)) {
  61.             return;
  62.         }
  63.         /** @var ProductEntity $product */
  64.         foreach ($event->getEntities() as $product) {
  65.             $criteria = new Criteria();
  66.             $criteria->addAssociation('pool');
  67.             $criteria->addFilter(new EqualsFilter('productId'$product->getId()));
  68.             $result $this->licenseProductRepository->search($criteria$this->salesChannelContext->getContext())->getEntities();
  69.             if (!($result->first() === null)) {
  70.                 $product->addExtension("cogiLicenseManager"$result);
  71.                 /** @var LicenseProductEntity $licenseProduct */
  72.                 $licenseProduct $result->first();
  73.                 $stock 0;
  74.                 $freeLicenses $this->licenseService->getFreeLicenses($licenseProduct->getPool()->getId(), $this->salesChannelContext->getContext(), 0);
  75.                 /** @var LicenseKeyEntity $key */
  76.                 foreach ($freeLicenses as $key) {
  77.                     if ($key->isMultiKey()) {
  78.                         if ($key->getMultiKeyLimit() > && $key->getMultiKeyLimit() > $key->getMultiKeyUsed()){
  79.                             $stock += ($key->getMultiKeyLimit() - $key->getMultiKeyUsed()) - $this->licenseService->getReservedCountMultiKey($key->id$this->salesChannelContext->getContext());
  80.                         }
  81.                     }else{
  82.                         $stock++;
  83.                     }
  84.                 }
  85.                 if ($licenseProduct->getCount() > 1) {
  86.                     if (($stock $licenseProduct->getCount()) < 1) {
  87.                         $stock 0;
  88.                     } else {
  89.                         $i = ($stock $licenseProduct->getCount());
  90.                         $stock round($i0PHP_ROUND_HALF_DOWN);
  91.                         if ($stock 1$stock 1;
  92.                     }
  93.                 }
  94.                 $product->setMaxPurchase($stock);
  95.                 $product->setStock($stock);
  96.                 if ($stock != $product->getStock()) {
  97.                     $newStock $this->licenseService->updateProductStock($product->getId(), $licenseProduct->getPool()->getId());
  98.                     if ($newStock >= 0) {
  99.                         $product->setStock($newStock);
  100.                         $product->setMaxPurchase($newStock);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.     /**
  107.      * @param SalesChannelContextResolvedEvent $event
  108.      */
  109.     public function onSalesChannelContextResolved(SalesChannelContextResolvedEvent $event): void {
  110.         $this->salesChannelContext $event->getSalesChannelContext();
  111.     }
  112.     /**
  113.      * Gibt wieder ob ein Automatisches anpassen des Bestandes Aktiviert ist
  114.      * @return bool
  115.      */
  116.     private function isAutoStockEnabled(): bool {
  117.         return $this->config['CogiLicenseManager.config.stockupdate'] ?? true;
  118.     }
  119. }