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

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItemDownload\OrderLineItemDownloadEntity;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Content\Flow\Dispatching\DelayableAction;
  6. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  7. use Shopware\Core\Content\Product\State;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\Event\OrderAware;
  11. use Shopware\Core\Framework\Log\Package;
  12. /**
  13.  * @internal
  14.  */
  15. #[Package('services-settings')]
  16. class GrantDownloadAccessAction extends FlowAction implements DelayableAction
  17. {
  18.     public function __construct(private readonly EntityRepository $orderLineItemDownloadRepository)
  19.     {
  20.     }
  21.     public static function getName(): string
  22.     {
  23.         return 'action.grant.download.access';
  24.     }
  25.     /**
  26.      * @return array<int, string>
  27.      */
  28.     public function requirements(): array
  29.     {
  30.         return [OrderAware::class];
  31.     }
  32.     public function handleFlow(StorableFlow $flow): void
  33.     {
  34.         if (!$flow->hasData(OrderAware::ORDER)) {
  35.             return;
  36.         }
  37.         /** @var OrderEntity $order */
  38.         $order $flow->getData(OrderAware::ORDER);
  39.         $this->update($flow->getContext(), $flow->getConfig(), $order);
  40.     }
  41.     /**
  42.      * @param array<string, mixed> $config
  43.      */
  44.     private function update(Context $context, array $configOrderEntity $order): void
  45.     {
  46.         if (!isset($config['value'])) {
  47.             return;
  48.         }
  49.         $lineItems $order->getLineItems();
  50.         if (!$lineItems) {
  51.             return;
  52.         }
  53.         $downloadIds = [];
  54.         foreach ($lineItems->filterGoodsFlat() as $lineItem) {
  55.             $states $lineItem->getStates();
  56.             if (!$lineItem->getDownloads() || !\in_array(State::IS_DOWNLOAD$statestrue)) {
  57.                 continue;
  58.             }
  59.             /** @var OrderLineItemDownloadEntity $download */
  60.             foreach ($lineItem->getDownloads() as $download) {
  61.                 $downloadIds[] = $download->getId();
  62.                 $download->setAccessGranted((bool) $config['value']);
  63.             }
  64.         }
  65.         if (empty($downloadIds)) {
  66.             return;
  67.         }
  68.         $this->orderLineItemDownloadRepository->update(
  69.             array_map(fn (string $id): array => ['id' => $id'accessGranted' => $config['value']], array_unique($downloadIds)),
  70.             $context
  71.         );
  72.     }
  73. }