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

DownloadRoute   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 9

3 Methods

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