Passed
Push — master ( 5a03eb...1fdf24 )
by Christian
11:41 queued 11s
created

WishlistPageLoader::createCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 9.9666
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Page\Wishlist;
4
5
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
6
use Shopware\Core\Checkout\Customer\Aggregate\CustomerWishlist\CustomerWishlistEntity;
7
use Shopware\Core\Checkout\Customer\Exception\CustomerWishlistNotFoundException;
8
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLoadWishlistRoute;
9
use Shopware\Core\Checkout\Customer\SalesChannel\LoadWishlistRouteResponse;
10
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
11
use Shopware\Core\Content\Product\ProductCollection;
12
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
13
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
14
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
15
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
16
use Shopware\Core\System\SalesChannel\SalesChannelContext;
17
use Shopware\Storefront\Page\GenericPageLoaderInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
21
class WishlistPageLoader
22
{
23
    private const LIMIT = 24;
24
25
    private const DEFAULT_PAGE = 1;
26
27
    /**
28
     * @var GenericPageLoaderInterface
29
     */
30
    private $genericLoader;
31
32
    /**
33
     * @var EventDispatcherInterface
34
     */
35
    private $eventDispatcher;
36
37
    /**
38
     * @var AbstractLoadWishlistRoute
39
     */
40
    private $wishlistLoadRoute;
41
42
    public function __construct(
43
        GenericPageLoaderInterface $genericLoader,
44
        AbstractLoadWishlistRoute $wishlistLoadRoute,
45
        EventDispatcherInterface $eventDispatcher
46
    ) {
47
        $this->genericLoader = $genericLoader;
48
        $this->wishlistLoadRoute = $wishlistLoadRoute;
49
        $this->eventDispatcher = $eventDispatcher;
50
    }
51
52
    /**
53
     * @throws CategoryNotFoundException
54
     * @throws CustomerNotLoggedInException
55
     * @throws InconsistentCriteriaIdsException
56
     * @throws MissingRequestParameterException
57
     */
58
    public function load(Request $request, SalesChannelContext $context): WishlistPage
59
    {
60
        if (!$context->getCustomer()) {
61
            throw new CustomerNotLoggedInException();
62
        }
63
64
        $criteria = $this->createCriteria($request);
65
66
        $page = $this->genericLoader->load($request, $context);
67
        $page = WishlistPage::createFrom($page);
68
69
        try {
70
            $page->setWishlist($this->wishlistLoadRoute->load($request, $context, $criteria));
71
        } catch (CustomerWishlistNotFoundException $exception) {
72
            $page->setWishlist(
73
                new LoadWishlistRouteResponse(
74
                    new CustomerWishlistEntity(),
75
                    new EntitySearchResult(
76
                        0,
77
                        new ProductCollection(),
78
                        null,
79
                        $criteria,
80
                        $context->getContext()
81
                    )
82
                )
83
            );
84
        }
85
86
        $this->eventDispatcher->dispatch(
87
            new WishlistPageLoadedEvent($page, $context, $request)
88
        );
89
90
        return $page;
91
    }
92
93
    private function createCriteria(Request $request): Criteria
94
    {
95
        $limit = (int) $request->query->get('limit', self::LIMIT);
96
        $page = (int) $request->query->get('p', self::DEFAULT_PAGE);
97
        $offset = $limit * ($page - 1);
98
99
        return (new Criteria())
100
            ->addAssociation('manufacturer')
101
            ->addAssociation('options.group')
102
            ->setLimit($limit)
103
            ->setOffset($offset)
104
            ->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
105
    }
106
}
107