Passed
Push — trunk ( 9b2ebb...065c18 )
by Christian
10:35 queued 13s
created

CheckoutFinishPageLoader::getOrder()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 30
nc 5
nop 2
dl 0
loc 46
rs 9.1288
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Page\Checkout\Finish;
4
5
use Shopware\Core\Checkout\Cart\CartException;
6
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
7
use Shopware\Core\Checkout\Order\OrderEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Checkout\Order\OrderEntity 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...
8
use Shopware\Core\Checkout\Order\OrderException;
9
use Shopware\Core\Checkout\Order\SalesChannel\AbstractOrderRoute;
10
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
11
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
12
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
13
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
14
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
15
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
16
use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
17
use Shopware\Core\Profiling\Profiler;
18
use Shopware\Core\System\SalesChannel\SalesChannelContext;
19
use Shopware\Storefront\Page\GenericPageLoaderInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
23
class CheckoutFinishPageLoader
24
{
25
    private EventDispatcherInterface $eventDispatcher;
26
27
    private GenericPageLoaderInterface $genericLoader;
28
29
    private AbstractOrderRoute $orderRoute;
30
31
    /**
32
     * @internal
33
     */
34
    public function __construct(
35
        EventDispatcherInterface $eventDispatcher,
36
        GenericPageLoaderInterface $genericLoader,
37
        AbstractOrderRoute $orderRoute
38
    ) {
39
        $this->eventDispatcher = $eventDispatcher;
40
        $this->genericLoader = $genericLoader;
41
        $this->orderRoute = $orderRoute;
42
    }
43
44
    /**
45
     * @throws CategoryNotFoundException
46
     * @throws CustomerNotLoggedInException
47
     * @throws InconsistentCriteriaIdsException
48
     * @throws MissingRequestParameterException
49
     * @throws OrderException
50
     */
51
    public function load(Request $request, SalesChannelContext $salesChannelContext): CheckoutFinishPage
52
    {
53
        $page = $this->genericLoader->load($request, $salesChannelContext);
54
55
        $page = CheckoutFinishPage::createFrom($page);
56
57
        if ($page->getMetaInformation()) {
58
            $page->getMetaInformation()->setRobots('noindex,follow');
59
        }
60
61
        Profiler::trace('finish-page-order-loading', function () use ($page, $request, $salesChannelContext): void {
62
            $page->setOrder($this->getOrder($request, $salesChannelContext));
63
        });
64
65
        $page->setChangedPayment((bool) $request->get('changedPayment', false));
66
67
        $page->setPaymentFailed((bool) $request->get('paymentFailed', false));
68
69
        $this->eventDispatcher->dispatch(
70
            new CheckoutFinishPageLoadedEvent($page, $salesChannelContext, $request)
71
        );
72
73
        if ($page->getOrder()->getItemRounding()) {
74
            $salesChannelContext->setItemRounding($page->getOrder()->getItemRounding());
75
            $salesChannelContext->getContext()->setRounding($page->getOrder()->getItemRounding());
76
        }
77
        if ($page->getOrder()->getTotalRounding()) {
78
            $salesChannelContext->setTotalRounding($page->getOrder()->getTotalRounding());
79
        }
80
81
        return $page;
82
    }
83
84
    /**
85
     * @throws CustomerNotLoggedInException
86
     * @throws InconsistentCriteriaIdsException
87
     * @throws MissingRequestParameterException
88
     * @throws OrderException
89
     */
90
    private function getOrder(Request $request, SalesChannelContext $salesChannelContext): OrderEntity
91
    {
92
        $customer = $salesChannelContext->getCustomer();
93
        if ($customer === null) {
94
            throw CartException::customerNotLoggedIn();
95
        }
96
97
        $orderId = $request->get('orderId');
98
        if (!$orderId) {
99
            throw new MissingRequestParameterException('orderId', '/orderId');
100
        }
101
102
        $criteria = (new Criteria([$orderId]))
103
            ->addFilter(new EqualsFilter('order.orderCustomer.customerId', $customer->getId()))
104
            ->addAssociation('lineItems.cover')
105
            ->addAssociation('transactions.paymentMethod')
106
            ->addAssociation('deliveries.shippingMethod')
107
            ->addAssociation('billingAddress.salutation')
108
            ->addAssociation('billingAddress.country')
109
            ->addAssociation('billingAddress.countryState')
110
            ->addAssociation('deliveries.shippingOrderAddress.salutation')
111
            ->addAssociation('deliveries.shippingOrderAddress.country')
112
            ->addAssociation('deliveries.shippingOrderAddress.countryState');
113
114
        $criteria->getAssociation('transactions')->addSorting(new FieldSorting('createdAt'));
115
116
        $this->eventDispatcher->dispatch(
117
            new CheckoutFinishPageOrderCriteriaEvent($criteria, $salesChannelContext)
118
        );
119
120
        try {
121
            $searchResult = $this->orderRoute
122
                ->load(new Request(), $salesChannelContext, $criteria)
123
                ->getOrders();
124
        } catch (InvalidUuidException $e) {
125
            throw OrderException::orderNotFound($orderId);
126
        }
127
128
        /** @var OrderEntity|null $order */
129
        $order = $searchResult->get($orderId);
130
131
        if (!$order) {
132
            throw OrderException::orderNotFound($orderId);
133
        }
134
135
        return $order;
136
    }
137
}
138