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