Passed
Push — master ( 9b0780...47580e )
by Christian
11:03 queued 10s
created

GuestWishlistPageletLoader::createCriteria()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 21
rs 9.8666
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Pagelet\Wishlist;
4
5
use Shopware\Core\Content\Product\ProductCollection;
6
use Shopware\Core\Content\Product\SalesChannel\AbstractProductListRoute;
7
use Shopware\Core\Content\Product\SalesChannel\ProductListResponse;
8
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
10
use Shopware\Core\Framework\Uuid\Uuid;
11
use Shopware\Core\System\SalesChannel\SalesChannelContext;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class GuestWishlistPageletLoader
16
{
17
    private const LIMIT = 100;
18
19
    /**
20
     * @var EventDispatcherInterface
21
     */
22
    private $eventDispatcher;
23
24
    /**
25
     * @var AbstractProductListRoute
26
     */
27
    private $productListRoute;
28
29
    public function __construct(
30
        AbstractProductListRoute $productListRoute,
31
        EventDispatcherInterface $eventDispatcher
32
    ) {
33
        $this->productListRoute = $productListRoute;
34
        $this->eventDispatcher = $eventDispatcher;
35
    }
36
37
    public function load(Request $request, SalesChannelContext $context): GuestWishlistPagelet
38
    {
39
        $page = new GuestWishlistPagelet();
40
41
        $criteria = $this->createCriteria($request);
42
43
        if (empty($criteria->getIds())) {
44
            $response = new ProductListResponse(new EntitySearchResult(
45
                0,
46
                new ProductCollection(),
47
                null,
48
                $criteria,
49
                $context->getContext()
50
            ));
51
        } else {
52
            $response = $this->productListRoute->load($criteria, $context);
53
        }
54
55
        $page->setSearchResult($response);
56
57
        $this->eventDispatcher->dispatch(new GuestWishlistPageletLoadedEvent($page, $context, $request));
58
59
        return $page;
60
    }
61
62
    private function createCriteria(Request $request): Criteria
63
    {
64
        $criteria = new Criteria();
65
66
        $productIds = $request->get('productIds', []);
67
68
        if (!\is_array($productIds)) {
69
            throw new \InvalidArgumentException('Argument $productIds is not an array');
70
        }
71
72
        $productIds = array_filter($productIds, function ($productId) {
73
            return Uuid::isValid($productId);
74
        });
75
76
        $criteria->setLimit(self::LIMIT);
77
        $criteria->setIds($productIds);
78
79
        return $criteria
80
            ->addAssociation('manufacturer')
81
            ->addAssociation('options.group')
82
            ->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
83
    }
84
}
85