SaleSearchQueryPlugin::getSearchQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Client\ExampleProductSalePage\Plugin\Elasticsearch\Query;
11
12
use Elastica\Query;
13
use Elastica\Query\AbstractQuery;
14
use Elastica\Query\BoolQuery;
15
use Elastica\Query\Nested;
16
use Elastica\Query\Term;
17
use Generated\Shared\Search\PageIndexMap;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Search\PageIndexMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Generated\Shared\Transfer\SearchContextTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SearchContextTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Spryker\Client\Kernel\AbstractPlugin;
20
use Spryker\Client\ProductLabel\Plugin\ProductLabelFacetConfigTransferBuilderPlugin;
21
use Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface;
22
use Spryker\Client\SearchExtension\Dependency\Plugin\SearchContextAwareQueryInterface;
23
24
/**
25
 * @method \Pyz\Client\ExampleProductSalePage\ExampleProductSalePageFactory getFactory()
26
 */
27
class SaleSearchQueryPlugin extends AbstractPlugin implements QueryInterface, SearchContextAwareQueryInterface
28
{
29
    protected const SOURCE_IDENTIFIER = 'page';
30
31
    protected Query $query;
32
33
    protected SearchContextTransfer $searchContextTransfer;
34
35
    public function __construct()
36
    {
37
        $this->query = $this->createSearchQuery();
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     * - Returns a query object for sale search.
43
     *
44
     * @api
45
     *
46
     * @return \Elastica\Query
47
     */
48
    public function getSearchQuery(): Query
49
    {
50
        return $this->query;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     * - Defines a context for sale search.
56
     *
57
     * @api
58
     *
59
     * @return \Generated\Shared\Transfer\SearchContextTransfer
60
     */
61
    public function getSearchContext(): SearchContextTransfer
62
    {
63
        if (!$this->hasSearchContext()) {
64
            $this->setupDefaultSearchContext();
65
        }
66
67
        return $this->searchContextTransfer;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     * - Sets a context for sale search.
73
     *
74
     * @api
75
     *
76
     * @param \Generated\Shared\Transfer\SearchContextTransfer $searchContextTransfer
77
     *
78
     * @return void
79
     */
80
    public function setSearchContext(SearchContextTransfer $searchContextTransfer): void
81
    {
82
        $this->searchContextTransfer = $searchContextTransfer;
83
    }
84
85
    protected function setupDefaultSearchContext(): void
86
    {
87
        $searchContextTransfer = new SearchContextTransfer();
88
        $searchContextTransfer->setSourceIdentifier(static::SOURCE_IDENTIFIER);
89
90
        $this->searchContextTransfer = $searchContextTransfer;
91
    }
92
93
    protected function createSearchQuery(): Query
94
    {
95
        $saleProductsFilter = $this->createSaleProductsFilter();
96
97
        $boolQuery = new BoolQuery();
98
        $boolQuery->addFilter($saleProductsFilter);
99
100
        return $this->createQuery($boolQuery);
101
    }
102
103
    protected function createSaleProductsFilter(): Nested
104
    {
105
        $saleProductsQuery = $this->createSaleProductsQuery();
106
107
        $saleProductsFilter = new Nested();
108
        $saleProductsFilter
109
            ->setQuery($saleProductsQuery)
110
            ->setPath(PageIndexMap::STRING_FACET);
111
112
        return $saleProductsFilter;
113
    }
114
115
    protected function createSaleProductsQuery(): BoolQuery
116
    {
117
        $storeTransfer = $this->getFactory()->getStore();
118
119
        $labelName = $this->getFactory()
120
            ->getConfig()
121
            ->getLabelSaleName();
122
123
        $defaultLocale = current($storeTransfer->getAvailableLocaleIsoCodes());
124
        $storageProductLabelTransfer = $this->getFactory()
125
            ->getProductLabelStorageClient()
126
            ->findLabelByName($labelName, $defaultLocale, $storeTransfer->getName());
127
128
        $labelId = $storageProductLabelTransfer ? $storageProductLabelTransfer->getIdProductLabel() : 0;
129
130
        $newProductsBoolQuery = new BoolQuery();
131
132
        $stringFacetFieldFilter = $this->createStringFacetFieldFilter(ProductLabelFacetConfigTransferBuilderPlugin::NAME);
133
        $stringFacetValueFilter = $this->createStringFacetValueFilter($labelId);
134
135
        $newProductsBoolQuery
136
            ->addFilter($stringFacetFieldFilter)
137
            ->addFilter($stringFacetValueFilter);
138
139
        return $newProductsBoolQuery;
140
    }
141
142
    protected function createStringFacetFieldFilter(string $fieldName): Term
143
    {
144
        $termQuery = new Term();
145
        $termQuery->setTerm(PageIndexMap::STRING_FACET_FACET_NAME, $fieldName);
146
147
        return $termQuery;
148
    }
149
150
    protected function createStringFacetValueFilter(int $idProductLabel): Term
151
    {
152
        $termQuery = new Term();
153
        $termQuery->setTerm(PageIndexMap::STRING_FACET_FACET_VALUE, (string)$idProductLabel);
154
155
        return $termQuery;
156
    }
157
158
    protected function createQuery(AbstractQuery $abstractQuery): Query
159
    {
160
        $query = new Query();
161
        $query
162
            ->setQuery($abstractQuery)
163
            ->setSource([PageIndexMap::SEARCH_RESULT_DATA]);
164
165
        return $query;
166
    }
167
168
    protected function hasSearchContext(): bool
169
    {
170
        return isset($this->searchContextTransfer);
171
    }
172
}
173