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

DownloadRoute::load()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 34
rs 8.8333
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer\SalesChannel;
4
5
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
6
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItemDownload\OrderLineItemDownloadEntity;
7
use Shopware\Core\Content\Media\File\DownloadResponseGenerator;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
12
use Shopware\Core\Framework\Log\Package;
13
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
14
use Shopware\Core\Framework\Routing\Annotation\Since;
15
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
16
use Shopware\Core\System\SalesChannel\SalesChannelContext;
17
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Routing\Annotation\Route;
21
22
/**
23
 * @Route(defaults={"_routeScope"={"store-api"}})
24
 */
25
#[Package('customer-order')]
26
class DownloadRoute extends AbstractDownloadRoute
27
{
28
    private EntityRepositoryInterface $downloadRepository;
29
30
    private DownloadResponseGenerator $downloadResponseGenerator;
31
32
    /**
33
     * @internal
34
     */
35
    public function __construct(
36
        EntityRepositoryInterface $downloadRepository,
37
        DownloadResponseGenerator $downloadResponseGenerator
38
    ) {
39
        $this->downloadRepository = $downloadRepository;
40
        $this->downloadResponseGenerator = $downloadResponseGenerator;
41
    }
42
43
    public function getDecorated(): AbstractDownloadRoute
44
    {
45
        throw new DecorationPatternException(self::class);
46
    }
47
48
    /**
49
     * @Since("6.4.19.0")
50
     * @Route("/store-api/order/download/{orderId}/{downloadId}", name="store-api.account.order.single.download", methods={"GET"}, defaults={"_loginRequired"=true, "_loginRequiredAllowGuest"=true})
51
     */
52
    public function load(Request $request, SalesChannelContext $context): Response
53
    {
54
        $customer = $context->getCustomer();
55
        $downloadId = $request->get('downloadId', false);
56
        $orderId = $request->get('orderId', false);
57
58
        if (!$customer) {
59
            throw new CustomerNotLoggedInException();
60
        }
61
62
        if ($downloadId === false || $orderId === false) {
63
            throw new MissingRequestParameterException(!$downloadId ? 'downloadId' : 'orderId');
64
        }
65
66
        $criteria = new Criteria([$downloadId]);
67
        $criteria->addAssociation('media');
68
        $criteria->addFilter(new MultiFilter(
69
            MultiFilter::CONNECTION_AND,
70
            [
71
                new EqualsFilter('orderLineItem.order.id', $orderId),
72
                new EqualsFilter('orderLineItem.order.orderCustomer.customerId', $customer->getId()),
73
                new EqualsFilter('accessGranted', true),
74
            ]
75
        ));
76
77
        $download = $this->downloadRepository->search($criteria, $context->getContext())->first();
78
79
        if (!$download instanceof OrderLineItemDownloadEntity || !$download->getMedia()) {
80
            throw new FileNotFoundException($downloadId);
81
        }
82
83
        $media = $download->getMedia();
84
85
        return $this->downloadResponseGenerator->getResponse($media, $context);
86
    }
87
}
88