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

LineItemDownloadLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B load() 0 24 7
A getLineItemDownloadPayload() 0 29 3
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\EntityRepository;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
12
13
class LineItemDownloadLoader
14
{
15
    private EntityRepository $productDownloadRepository;
16
17
    /**
18
     * @internal
19
     */
20
    public function __construct(EntityRepository $productDownloadRepository)
21
    {
22
        $this->productDownloadRepository = $productDownloadRepository;
23
    }
24
25
    /**
26
     * @param mixed[][] $lineItems
27
     *
28
     * @return array<int, array<int, array{position: int, mediaId: string, accessGranted: bool}>>
29
     */
30
    public function load(array $lineItems, Context $context): array
31
    {
32
        $lineItemKeys = [];
33
        foreach ($lineItems as $key => $lineItem) {
34
            $productId = $lineItem['referencedId'] ?? null;
35
            $states = $lineItem['states'] ?? null;
36
37
            if (
38
                !$productId
39
                || !$states
40
                || !\in_array(State::IS_DOWNLOAD, $states, true)
41
                || !empty($lineItem['downloads'])
42
            ) {
43
                continue;
44
            }
45
46
            $lineItemKeys[(string) $productId] = (int) $key;
47
        }
48
49
        if (empty($lineItemKeys)) {
50
            return [];
51
        }
52
53
        return $this->getLineItemDownloadPayload($lineItemKeys, $context);
54
    }
55
56
    /**
57
     * @param array<string, int> $lineItemKeys
58
     *
59
     * @return array<int, array<int, array{position: int, mediaId: string, accessGranted: bool}>>
60
     */
61
    private function getLineItemDownloadPayload(array $lineItemKeys, Context $context): array
62
    {
63
        $productIds = array_keys($lineItemKeys);
64
65
        $criteria = new Criteria();
66
        $criteria->addFilter(new EqualsAnyFilter('productId', $productIds));
67
68
        $context = clone $context;
69
        $context->assign(['versionId' => Defaults::LIVE_VERSION]);
70
71
        /** @var ProductDownloadCollection $productDownloads */
72
        $productDownloads = $this->productDownloadRepository->search($criteria, $context)->getEntities();
73
74
        $downloads = [];
75
        foreach ($productDownloads->getElements() as $productDownload) {
76
            $key = $lineItemKeys[$productDownload->getProductId()] ?? null;
77
78
            if ($key === null) {
79
                continue;
80
            }
81
82
            $downloads[$key][] = [
83
                'position' => $productDownload->getPosition(),
84
                'mediaId' => $productDownload->getMediaId(),
85
                'accessGranted' => false,
86
            ];
87
        }
88
89
        return $downloads;
90
    }
91
}
92