custom/plugins/AcrisStockCS/src/AcrisStockCS.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Stock;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Order\OrderStates;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  10. use Shopware\Core\Framework\Plugin\KernelPluginLoader\KernelPluginLoader;
  11. use Shopware\Core\Framework\Plugin\KernelPluginLoader\StaticKernelPluginLoader;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Symfony\Component\HttpKernel\Kernel;
  14. class AcrisStockCS extends Plugin
  15. {
  16.     public function activate(ActivateContext $activateContext): void
  17.     {
  18.         $this->putClassesToContainer();
  19.         $products $this->getAffectedProductIds($activateContext->getContext());
  20.         $stockUpdater $this->container->get('Acris\Stock\Core\Content\Product\DataAbstractionLayer\StockUpdater');
  21.         $stockUpdater->decreaseStockOnActivation($products$activateContext->getContext());
  22.     }
  23.     public function deactivate(DeactivateContext $deactivateContext): void
  24.     {
  25.         $products $this->getAffectedProductIds($deactivateContext->getContext());
  26.         $stockUpdater $this->container->get('Acris\Stock\Core\Content\Product\DataAbstractionLayer\StockUpdater');
  27.         $stockUpdater->increaseStockOnDeactivation($products$deactivateContext->getContext());
  28.     }
  29.     private function getAffectedProductIds(Context $context): array {
  30.         $connection $this->container->get(Connection::class);
  31.         $sql '
  32. SELECT LOWER(HEX(order_line_item.product_id)) as referenced_id, order_line_item.quantity as quantity
  33. FROM order_line_item
  34.     INNER JOIN `order`
  35.         ON `order`.id = order_line_item.order_id
  36.         AND `order`.version_id = order_line_item.order_version_id
  37.     INNER JOIN state_machine_state
  38.         ON state_machine_state.id = `order`.state_id
  39.         AND (state_machine_state.technical_name = :progress_state)
  40. WHERE order_line_item.type = :type
  41.     AND order_line_item.version_id = :version
  42.     AND order_line_item.product_id IS NOT NULL
  43. GROUP BY referenced_id;
  44.         ';
  45.         $rows $connection->fetchAllAssociative(
  46.             $sql,
  47.             [
  48.                 'type' => LineItem::PRODUCT_LINE_ITEM_TYPE,
  49.                 'version' => Uuid::fromHexToBytes($context->getVersionId()),
  50.                 'progress_state' => OrderStates::STATE_IN_PROGRESS
  51.             ]
  52.         );
  53.         return $rows;
  54.     }
  55.     private function putClassesToContainer(): void
  56.     {
  57.         /** @var Kernel $kernel */
  58.         $kernel $this->container->get('kernel');
  59.         /** @var KernelPluginLoader $pluginLoader */
  60.         $pluginLoader $this->container->get(KernelPluginLoader::class);
  61.         //get all plugins and mark the current plugin as active, so that its classes get added to the container
  62.         $plugins $pluginLoader->getPluginInfos();
  63.         foreach ($plugins as $i => $pluginData) {
  64.             if ($pluginData['name'] === $this->getName()) {
  65.                 $plugins[$i]['active'] = 1;
  66.             }
  67.         }
  68.         //load all plugins
  69.         $tmpStaticPluginLoader = new StaticKernelPluginLoader(
  70.             $pluginLoader->getClassLoader(),
  71.             $kernel->getContainer()->getParameter('kernel.plugin_dir'),
  72.             $plugins
  73.         );
  74.         //reboot kernel to apply the changes to the container
  75.         $kernel->reboot(null$tmpStaticPluginLoader);
  76.         //handle an exception, when calling getContainer on a not booted kernel
  77.         /** @var \Symfony\Component\DependencyInjection\ContainerInterface|null $newContainer */
  78.         $newContainer $kernel->getContainer();
  79.         if (!$newContainer) {
  80.             throw new \RuntimeException('Failed to reboot the kernel');
  81.         }
  82.         $this->container $newContainer;
  83.     }
  84. }