Passed
Push — trunk ( 2d6001...da76dd )
by Christian
15:37 queued 13s
created

GrantDownloadAccessAction::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Flow\Dispatching\Action;
4
5
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItemDownload\OrderLineItemDownloadEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Checkout\O...rLineItemDownloadEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Shopware\Core\Checkout\Order\OrderEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Checkout\Order\OrderEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Shopware\Core\Content\Flow\Dispatching\DelayableAction;
8
use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
9
use Shopware\Core\Content\Product\State;
10
use Shopware\Core\Framework\Context;
11
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
12
use Shopware\Core\Framework\Event\OrderAware;
13
14
/**
15
 * @package business-ops
16
 *
17
 * @internal
18
 */
19
class GrantDownloadAccessAction extends FlowAction implements DelayableAction
20
{
21
    private EntityRepository $orderLineItemDownloadRepository;
22
23
    public function __construct(EntityRepository $orderLineItemDownloadRepository)
24
    {
25
        $this->orderLineItemDownloadRepository = $orderLineItemDownloadRepository;
26
    }
27
28
    public static function getName(): string
29
    {
30
        return 'action.grant.download.access';
31
    }
32
33
    /**
34
     * @return array<int, string>
35
     */
36
    public function requirements(): array
37
    {
38
        return [OrderAware::class];
39
    }
40
41
    public function handleFlow(StorableFlow $flow): void
42
    {
43
        if (!$flow->hasData(OrderAware::ORDER)) {
44
            return;
45
        }
46
47
        /** @var OrderEntity $order */
48
        $order = $flow->getData(OrderAware::ORDER);
49
50
        $this->update($flow->getContext(), $flow->getConfig(), $order);
51
    }
52
53
    /**
54
     * @param array<string, mixed> $config
55
     */
56
    private function update(Context $context, array $config, OrderEntity $order): void
57
    {
58
        if (!isset($config['value'])) {
59
            return;
60
        }
61
62
        $lineItems = $order->getLineItems();
63
64
        if (!$lineItems) {
65
            return;
66
        }
67
68
        $downloadIds = [];
69
70
        foreach ($lineItems->filterGoodsFlat() as $lineItem) {
71
            $states = $lineItem->getStates();
72
73
            if (!$lineItem->getDownloads() || !\in_array(State::IS_DOWNLOAD, $states, true)) {
74
                continue;
75
            }
76
77
            /** @var OrderLineItemDownloadEntity $download */
78
            foreach ($lineItem->getDownloads() as $download) {
79
                $downloadIds[] = $download->getId();
80
                $download->setAccessGranted((bool) $config['value']);
81
            }
82
        }
83
84
        if (empty($downloadIds)) {
85
            return;
86
        }
87
88
        $this->orderLineItemDownloadRepository->update(
89
            array_map(function (string $id) use ($config): array {
90
                return ['id' => $id, 'accessGranted' => $config['value']];
91
            }, array_unique($downloadIds)),
92
            $context
93
        );
94
    }
95
}
96