Passed
Push — master ( 4c0618...e0994f )
by Christian
11:02
created

AccountOrderDetailPageLoader::load()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 29
nc 4
nop 2
dl 0
loc 48
rs 9.456
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Page\Account\Order;
4
5
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
6
use Shopware\Core\Checkout\Order\OrderEntity;
7
use Shopware\Core\Checkout\Order\SalesChannel\AbstractOrderRoute;
8
use Shopware\Core\Checkout\Order\SalesChannel\OrderRouteResponseStruct;
9
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
10
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
12
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
13
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
14
use Shopware\Core\System\SalesChannel\SalesChannelContext;
15
use Shopware\Storefront\Event\RouteRequest\OrderRouteRequestEvent;
16
use Shopware\Storefront\Page\GenericPageLoaderInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
21
class AccountOrderDetailPageLoader
22
{
23
    /**
24
     * @var GenericPageLoaderInterface
25
     */
26
    private $genericLoader;
27
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    private $eventDispatcher;
32
33
    /**
34
     * @var AbstractOrderRoute
35
     */
36
    private $orderRoute;
37
38
    public function __construct(
39
        GenericPageLoaderInterface $genericLoader,
40
        EventDispatcherInterface $eventDispatcher,
41
        AbstractOrderRoute $orderRoute
42
    ) {
43
        $this->genericLoader = $genericLoader;
44
        $this->eventDispatcher = $eventDispatcher;
45
        $this->orderRoute = $orderRoute;
46
    }
47
48
    /**
49
     * @throws CategoryNotFoundException
50
     * @throws CustomerNotLoggedInException
51
     * @throws InconsistentCriteriaIdsException
52
     * @throws MissingRequestParameterException
53
     */
54
    public function load(Request $request, SalesChannelContext $salesChannelContext): AccountOrderDetailPage
55
    {
56
        if (!$salesChannelContext->getCustomer()) {
57
            throw new CustomerNotLoggedInException();
58
        }
59
60
        $orderId = (string) $request->get('id');
61
62
        if ($orderId === '') {
63
            throw new MissingRequestParameterException('id');
64
        }
65
66
        $criteria = new Criteria([$orderId]);
67
        $criteria
68
            ->addAssociation('lineItems')
69
            ->addAssociation('orderCustomer')
70
            ->addAssociation('transactions.paymentMethod')
71
            ->addAssociation('deliveries.shippingMethod')
72
            ->addAssociation('lineItems.cover');
73
74
        $criteria->getAssociation('transactions')
75
            ->addSorting(new FieldSorting('createdAt'));
76
77
        $apiRequest = new Request();
78
79
        $event = new OrderRouteRequestEvent($request, $apiRequest, $salesChannelContext, $criteria);
80
        $this->eventDispatcher->dispatch($event);
81
82
        /** @var OrderRouteResponseStruct $result */
83
        $result = $this->orderRoute
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Core\Checkout\O...tractOrderRoute::load() has been deprecated: tag:v6.4.0 - Parameter $criteria will be mandatory in future implementation ( Ignorable by Annotation )

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

83
        $result = /** @scrutinizer ignore-deprecated */ $this->orderRoute

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Shopware\Core\Checkout\O...teResponse::getObject() has been deprecated: tag:v6.4.0 - Use `getPaymentsChangeable` and `getOrders` ( Ignorable by Annotation )

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

83
        $result = /** @scrutinizer ignore-deprecated */ $this->orderRoute

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
84
            ->load($event->getStoreApiRequest(), $salesChannelContext, $criteria)
0 ignored issues
show
Unused Code introduced by
The call to Shopware\Core\Checkout\O...tractOrderRoute::load() has too many arguments starting with $criteria. ( Ignorable by Annotation )

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

84
            ->/** @scrutinizer ignore-call */ load($event->getStoreApiRequest(), $salesChannelContext, $criteria)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
85
            ->getObject();
86
87
        $order = $result->getOrders()->first();
88
89
        if (!$order instanceof OrderEntity) {
90
            throw new NotFoundHttpException();
91
        }
92
93
        $page = AccountOrderDetailPage::createFrom($this->genericLoader->load($request, $salesChannelContext));
94
        $page->setLineItems($order->getNestedLineItems());
95
        $page->setOrder($order);
96
97
        $this->eventDispatcher->dispatch(
98
            new AccountOrderDetailPageLoadedEvent($page, $salesChannelContext, $request)
99
        );
100
101
        return $page;
102
    }
103
}
104