vendor/shopware/core/Content/Flow/Dispatching/Action/GrantDownloadAccessAction.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItemDownload\OrderLineItemDownloadEntity;
  5. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  8. use Shopware\Core\Content\Product\State;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\Event\DelayAware;
  12. use Shopware\Core\Framework\Event\FlowEvent;
  13. use Shopware\Core\Framework\Event\OrderAware;
  14. use Shopware\Core\Framework\Feature;
  15. use Shopware\Core\Framework\Log\Package;
  16. /**
  17.  * @internal
  18.  */
  19. #[Package('business-ops')]
  20. class GrantDownloadAccessAction extends FlowAction
  21. {
  22.     private EntityRepositoryInterface $orderLineItemDownloadRepository;
  23.     public function __construct(EntityRepositoryInterface $orderLineItemDownloadRepository)
  24.     {
  25.         $this->orderLineItemDownloadRepository $orderLineItemDownloadRepository;
  26.     }
  27.     public static function getName(): string
  28.     {
  29.         return 'action.grant.download.access';
  30.     }
  31.     /**
  32.      *  @deprecated tag:v6.5.0 Will be removed
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         if (Feature::isActive('v6.5.0.0')) {
  37.             return [];
  38.         }
  39.         Feature::triggerDeprecationOrThrow(
  40.             'v6.5.0.0',
  41.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0')
  42.         );
  43.         return [
  44.             self::getName() => 'handle',
  45.         ];
  46.     }
  47.     /**
  48.      * @return array<int, string>
  49.      */
  50.     public function requirements(): array
  51.     {
  52.         return [OrderAware::class, DelayAware::class];
  53.     }
  54.     /**
  55.      * @deprecated tag:v6.5.0 Will be removed
  56.      */
  57.     public function handle(FlowEvent $event): void
  58.     {
  59.         Feature::triggerDeprecationOrThrow(
  60.             'v6.5.0.0',
  61.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0')
  62.         );
  63.         $baseEvent $event->getEvent();
  64.         if (!$baseEvent instanceof CheckoutOrderPlacedEvent && !$baseEvent instanceof OrderStateMachineStateChangeEvent) {
  65.             return;
  66.         }
  67.         $this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getOrder());
  68.     }
  69.     public function handleFlow(StorableFlow $flow): void
  70.     {
  71.         if (!$flow->hasData(OrderAware::ORDER)) {
  72.             return;
  73.         }
  74.         /** @var OrderEntity $order */
  75.         $order $flow->getData(OrderAware::ORDER);
  76.         $this->update($flow->getContext(), $flow->getConfig(), $order);
  77.     }
  78.     /**
  79.      * @param array<string, mixed> $config
  80.      */
  81.     private function update(Context $context, array $configOrderEntity $order): void
  82.     {
  83.         if (!isset($config['value'])) {
  84.             return;
  85.         }
  86.         $lineItems $order->getLineItems();
  87.         if (!$lineItems) {
  88.             return;
  89.         }
  90.         $downloadIds = [];
  91.         foreach ($lineItems->filterGoodsFlat() as $lineItem) {
  92.             $states $lineItem->getStates();
  93.             if (!$lineItem->getDownloads() || !\in_array(State::IS_DOWNLOAD$statestrue)) {
  94.                 continue;
  95.             }
  96.             /** @var OrderLineItemDownloadEntity $download */
  97.             foreach ($lineItem->getDownloads() as $download) {
  98.                 $downloadIds[] = $download->getId();
  99.                 $download->setAccessGranted((bool) $config['value']);
  100.             }
  101.         }
  102.         if (empty($downloadIds)) {
  103.             return;
  104.         }
  105.         $this->orderLineItemDownloadRepository->update(
  106.             array_map(function (string $id) use ($config): array {
  107.                 return ['id' => $id'accessGranted' => $config['value']];
  108.             }, array_unique($downloadIds)),
  109.             $context
  110.         );
  111.     }
  112. }