Passed
Push — master ( 5cf429...20326d )
by Christian
12:06 queued 12s
created

DocumentPageLoader::load()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 40
rs 9.2568
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Page\Account\Document;
4
5
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
6
use Shopware\Core\Checkout\Document\DocumentService;
7
use Shopware\Core\Checkout\Document\Exception\InvalidDocumentException;
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\Routing\Exception\MissingRequestParameterException;
13
use Shopware\Core\System\SalesChannel\SalesChannelContext;
14
use Shopware\Storefront\Page\GenericPageLoaderInterface;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class DocumentPageLoader
19
{
20
    /**
21
     * @var GenericPageLoaderInterface
22
     */
23
    private $genericLoader;
24
25
    /**
26
     * @var EventDispatcherInterface
27
     */
28
    private $eventDispatcher;
29
30
    /**
31
     * @var DocumentService
32
     */
33
    private $documentService;
34
35
    /**
36
     * @var EntityRepositoryInterface
37
     */
38
    private $documentRepository;
39
40
    public function __construct(
41
        GenericPageLoaderInterface $genericLoader,
42
        EventDispatcherInterface $eventDispatcher,
43
        DocumentService $documentService,
44
        EntityRepositoryInterface $documentRepository
45
    ) {
46
        $this->genericLoader = $genericLoader;
47
        $this->eventDispatcher = $eventDispatcher;
48
        $this->documentService = $documentService;
49
        $this->documentRepository = $documentRepository;
50
    }
51
52
    /**
53
     * @throws CustomerNotLoggedInException
54
     * @throws InvalidDocumentException
55
     * @throws MissingRequestParameterException
56
     */
57
    public function load(Request $request, SalesChannelContext $salesChannelContext): DocumentPage
58
    {
59
        if (!$salesChannelContext->getCustomer() && $request->get('deepLinkCode', false) === false) {
60
            throw new CustomerNotLoggedInException();
61
        }
62
63
        if ($request->get('documentId', false) === false) {
64
            throw new MissingRequestParameterException('documentId');
65
        }
66
67
        $documentId = $request->get('documentId');
68
69
        $criteria = new Criteria();
70
        $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND, [
71
            new EqualsFilter('id', $documentId),
72
            new EqualsFilter('deepLinkCode', $request->get('deepLinkCode')),
73
        ]));
74
        $criteria->addAssociation('documentMediaFile');
75
        $criteria->addAssociation('documentType');
76
77
        $document = $this->documentRepository->search($criteria, $salesChannelContext->getContext())->get($documentId);
78
79
        if (!$document) {
80
            throw new InvalidDocumentException($documentId);
0 ignored issues
show
Bug introduced by
It seems like $documentId can also be of type null; however, parameter $documentId of Shopware\Core\Checkout\D...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            throw new InvalidDocumentException(/** @scrutinizer ignore-type */ $documentId);
Loading history...
81
        }
82
83
        $generatedDocument = $this->documentService->getDocument($document, $salesChannelContext->getContext());
84
85
        $page = $this->genericLoader->load($request, $salesChannelContext);
86
87
        $page = DocumentPage::createFrom($page);
88
89
        $page->setDocument($generatedDocument);
90
        $page->setDeepLinkCode($request->get('deepLinkCode'));
91
92
        $this->eventDispatcher->dispatch(
93
            new DocumentPageLoadedEvent($page, $salesChannelContext, $request)
94
        );
95
96
        return $page;
97
    }
98
}
99