ResolvedCriteriaProductSearchRoute   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 38 3
A __construct() 0 7 1
A getDecorated() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Product\SalesChannel\Search;
4
5
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
6
use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
7
use Shopware\Core\Content\Product\ProductEvents;
8
use Shopware\Core\Content\Product\SalesChannel\Listing\Processor\CompositeListingProcessor;
9
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingFeaturesSubscriber;
10
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
12
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
13
use Shopware\Core\Framework\Feature;
14
use Shopware\Core\Framework\Log\Package;
15
use Shopware\Core\System\SalesChannel\SalesChannelContext;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\Annotation\Route;
18
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
19
20
#[Route(defaults: ['_routeScope' => ['store-api']])]
21
#[Package('system-settings')]
22
class ResolvedCriteriaProductSearchRoute extends AbstractProductSearchRoute
23
{
24
    final public const DEFAULT_SEARCH_SORT = 'score';
25
    final public const STATE = 'search-route-context';
26
27
    /**
28
     * @internal
29
     */
30
    public function __construct(
31
        private readonly AbstractProductSearchRoute $decorated,
32
        private readonly EventDispatcherInterface $eventDispatcher,
33
        private readonly DefinitionInstanceRegistry $registry,
34
        private readonly RequestCriteriaBuilder $criteriaBuilder,
35
        private readonly CompositeListingProcessor $processor
36
    ) {
37
    }
38
39
    public function getDecorated(): AbstractProductSearchRoute
40
    {
41
        return $this->decorated;
42
    }
43
44
    #[Route(path: '/store-api/search', name: 'store-api.search', methods: ['POST'], defaults: ['_entity' => 'product'])]
45
    public function load(Request $request, SalesChannelContext $context, Criteria $criteria): ProductSearchRouteResponse
46
    {
47
        if (!$request->get('order')) {
48
            $request->request->set('order', self::DEFAULT_SEARCH_SORT);
49
        }
50
51
        $criteria->addState(self::STATE);
52
        if (!Feature::isActive('v6.6.0.0')) {
53
            $context->getContext()->addState(ProductListingFeaturesSubscriber::HANDLED_STATE);
0 ignored issues
show
Deprecated Code introduced by
The constant Shopware\Core\Content\Pr...bscriber::HANDLED_STATE has been deprecated: tag:v6.6.0 - Will be removed ( Ignorable by Annotation )

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

53
            $context->getContext()->addState(/** @scrutinizer ignore-deprecated */ ProductListingFeaturesSubscriber::HANDLED_STATE);

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

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

Loading history...
54
        }
55
56
        $criteria = $this->criteriaBuilder->handleRequest(
57
            $request,
58
            $criteria,
59
            $this->registry->getByEntityName('product'),
60
            $context->getContext()
61
        );
62
63
        $this->processor->prepare($request, $criteria, $context);
64
65
        $this->eventDispatcher->dispatch(
66
            new ProductSearchCriteriaEvent($request, $criteria, $context),
67
            ProductEvents::PRODUCT_SEARCH_CRITERIA
68
        );
69
70
        $response = $this->getDecorated()->load($request, $context, $criteria);
71
72
        $this->processor->process($request, $response->getListingResult(), $context);
73
74
        $this->eventDispatcher->dispatch(
75
            new ProductSearchResultEvent($request, $response->getListingResult(), $context),
76
            ProductEvents::PRODUCT_SEARCH_RESULT
77
        );
78
79
        $response->getListingResult()->addCurrentFilter('search', $request->get('search'));
80
81
        return $response;
82
    }
83
}
84