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

LineItemDownloadLoader::load()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 24
rs 8.8333
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Cart\Order;
4
5
use Shopware\Core\Content\Product\Aggregate\ProductDownload\ProductDownloadCollection;
6
use Shopware\Core\Content\Product\State;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
12
use Shopware\Core\Framework\Log\Package;
13
14
#[Package('checkout')]
15
class LineItemDownloadLoader
16
{
17
    private EntityRepositoryInterface $productDownloadRepository;
18
19
    /**
20
     * @internal
21
     */
22
    public function __construct(EntityRepositoryInterface $productDownloadRepository)
23
    {
24
        $this->productDownloadRepository = $productDownloadRepository;
25
    }
26
27
    /**
28
     * @param mixed[][] $lineItems
29
     *
30
     * @return array<int, array<int, array{position: int, mediaId: string, accessGranted: bool}>>
31
     */
32
    public function load(array $lineItems, Context $context): array
33
    {
34
        $lineItemKeys = [];
35
        foreach ($lineItems as $key => $lineItem) {
36
            $productId = $lineItem['referencedId'] ?? null;
37
            $states = $lineItem['states'] ?? null;
38
39
            if (
40
                !$productId
41
                || !$states
42
                || !\in_array(State::IS_DOWNLOAD, $states, true)
43
                || !empty($lineItem['downloads'])
44
            ) {
45
                continue;
46
            }
47
48
            $lineItemKeys[(string) $productId] = (int) $key;
49
        }
50
51
        if (empty($lineItemKeys)) {
52
            return [];
53
        }
54
55
        return $this->getLineItemDownloadPayload($lineItemKeys, $context);
56
    }
57
58
    /**
59
     * @param array<string, int> $lineItemKeys
60
     *
61
     * @return array<int, array<int, array{position: int, mediaId: string, accessGranted: bool}>>
62
     */
63
    private function getLineItemDownloadPayload(array $lineItemKeys, Context $context): array
64
    {
65
        $productIds = array_keys($lineItemKeys);
66
67
        $criteria = new Criteria();
68
        $criteria->addFilter(new EqualsAnyFilter('productId', $productIds));
69
70
        $context = clone $context;
71
        $context->assign(['versionId' => Defaults::LIVE_VERSION]);
72
73
        /** @var ProductDownloadCollection $productDownloads */
74
        $productDownloads = $this->productDownloadRepository->search($criteria, $context)->getEntities();
75
76
        $downloads = [];
77
        foreach ($productDownloads->getElements() as $productDownload) {
78
            $key = $lineItemKeys[$productDownload->getProductId()] ?? null;
79
80
            if ($key === null) {
81
                continue;
82
            }
83
84
            $downloads[$key][] = [
85
                'position' => $productDownload->getPosition(),
86
                'mediaId' => $productDownload->getMediaId(),
87
                'accessGranted' => false,
88
            ];
89
        }
90
91
        return $downloads;
92
    }
93
}
94