1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Client\CatalogPriceProductConnector\Plugin; |
||
9 | |||
10 | use Elastica\Query; |
||
11 | use Elastica\Query\BoolQuery; |
||
12 | use Elastica\Query\Nested; |
||
13 | use Elastica\Query\Term; |
||
14 | use Generated\Shared\Search\PageIndexMap; |
||
15 | use InvalidArgumentException; |
||
16 | use Spryker\Client\Kernel\AbstractPlugin; |
||
17 | use Spryker\Client\Search\Dependency\Plugin\QueryExpanderPluginInterface; |
||
18 | use Spryker\Client\Search\Dependency\Plugin\QueryInterface; |
||
19 | |||
20 | /** |
||
21 | * @method \Spryker\Client\CatalogPriceProductConnector\CatalogPriceProductConnectorFactory getFactory() |
||
22 | */ |
||
23 | class ProductPriceQueryExpanderPlugin extends AbstractPlugin implements QueryExpanderPluginInterface |
||
24 | { |
||
25 | /** |
||
26 | * @api |
||
27 | * |
||
28 | * @param \Spryker\Client\Search\Dependency\Plugin\QueryInterface $searchQuery |
||
29 | * @param array<string, mixed> $requestParameters |
||
30 | * |
||
31 | * @return \Spryker\Client\Search\Dependency\Plugin\QueryInterface |
||
32 | */ |
||
33 | public function expandQuery(QueryInterface $searchQuery, array $requestParameters = []) |
||
34 | { |
||
35 | $boolQuery = $this->getBoolQuery($searchQuery->getSearchQuery()); |
||
36 | |||
37 | $priceIdentifier = $this->getFactory()->createPriceIdentifierBuilder() |
||
38 | ->buildIdentifierForCurrentCurrency(); |
||
39 | |||
40 | $termQuery = $this->createPriceTermQuery($priceIdentifier); |
||
41 | $productPriceFilter = $this->createNestedProductPriceFilter($termQuery); |
||
42 | |||
43 | $boolQuery->addFilter($productPriceFilter); |
||
44 | |||
45 | return $searchQuery; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param \Elastica\Query $query |
||
50 | * |
||
51 | * @throws \InvalidArgumentException |
||
52 | * |
||
53 | * @return \Elastica\Query\BoolQuery |
||
54 | */ |
||
55 | protected function getBoolQuery(Query $query) |
||
56 | { |
||
57 | /** @var \Elastica\Query\AbstractQuery|array $boolQuery */ |
||
58 | $boolQuery = $query->getQuery(); |
||
59 | if (!$boolQuery instanceof BoolQuery) { |
||
60 | throw new InvalidArgumentException(sprintf( |
||
61 | 'Product Price Query Expander available only with %s, got: %s', |
||
62 | BoolQuery::class, |
||
63 | is_object($boolQuery) ? get_class($boolQuery) : gettype($boolQuery), |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
64 | )); |
||
65 | } |
||
66 | |||
67 | return $boolQuery; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $priceFacetName |
||
72 | * |
||
73 | * @return \Elastica\Query\Term |
||
74 | */ |
||
75 | protected function createPriceTermQuery($priceFacetName) |
||
76 | { |
||
77 | return (new Term()) |
||
78 | ->setTerm(PageIndexMap::INTEGER_FACET_FACET_NAME, $priceFacetName); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param \Elastica\Query\Term $termQuery |
||
83 | * |
||
84 | * @return \Elastica\Query\Nested |
||
85 | */ |
||
86 | protected function createNestedProductPriceFilter(Term $termQuery) |
||
87 | { |
||
88 | return (new Nested()) |
||
89 | ->setQuery($termQuery) |
||
90 | ->setPath(PageIndexMap::INTEGER_FACET); |
||
91 | } |
||
92 | } |
||
93 |