Passed
Push — 6.4 ( 969936...be76f2 )
by Christian
44:06 queued 29:02
created

GrantDownloadAccessAction   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 112
rs 10
c 1
b 0
f 0
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handleFlow() 0 10 2
A getName() 0 3 1
A handle() 0 13 3
A __construct() 0 3 1
A requirements() 0 3 1
A getSubscribedEvents() 0 13 2
B update() 0 37 8
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
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\Content\Fl...ching\Action\FlowAction has been deprecated: tag:v6.5.0 - reason:remove-subscriber - FlowActions won't be executed over the event system anymore, therefore the actions won't implement the EventSubscriberInterface anymore. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
class GrantDownloadAccessAction extends /** @scrutinizer ignore-deprecated */ FlowAction
Loading history...
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