This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * WellCommerce Open-Source E-Commerce Platform |
||
4 | * |
||
5 | * This file is part of the WellCommerce package. |
||
6 | * |
||
7 | * (c) Adam Piotrowski <[email protected]> |
||
8 | * |
||
9 | * For the full copyright and license information, |
||
10 | * please view the LICENSE file that was distributed with this source code. |
||
11 | */ |
||
12 | |||
13 | namespace WellCommerce\Component\Search\Adapter\ElasticSearch; |
||
14 | |||
15 | use Doctrine\Common\Collections\Collection; |
||
16 | use Elasticsearch\Client; |
||
17 | use Elasticsearch\ClientBuilder; |
||
18 | use WellCommerce\Component\Search\Adapter\AdapterInterface; |
||
19 | use WellCommerce\Component\Search\Adapter\QueryBuilderInterface; |
||
20 | use WellCommerce\Component\Search\Adapter\SearchAdapterConfiguratorInterface; |
||
21 | use WellCommerce\Component\Search\Model\DocumentInterface; |
||
22 | use WellCommerce\Component\Search\Model\FieldInterface; |
||
23 | use WellCommerce\Component\Search\Request\SearchRequestInterface; |
||
24 | |||
25 | /** |
||
26 | * Class ElasticSearchAdapter |
||
27 | * |
||
28 | * @author Adam Piotrowski <[email protected]> |
||
29 | */ |
||
30 | final class ElasticSearchAdapter implements AdapterInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var SearchAdapterConfiguratorInterface |
||
34 | */ |
||
35 | private $configurator; |
||
36 | |||
37 | /** |
||
38 | * @var Client |
||
39 | */ |
||
40 | private $client; |
||
41 | |||
42 | public function __construct(SearchAdapterConfiguratorInterface $configurator) |
||
43 | { |
||
44 | $this->configurator = $configurator; |
||
45 | } |
||
46 | |||
47 | public function search(SearchRequestInterface $request): array |
||
48 | { |
||
49 | $params = [ |
||
50 | 'index' => $this->getIndexName($request->getLocale()), |
||
51 | 'type' => $request->getType()->getName(), |
||
52 | "size" => $this->getOption('maxResults'), |
||
53 | 'body' => $this->createQueryBuilder($request)->getQuery(), |
||
54 | ]; |
||
55 | |||
56 | $results = $this->getClient()->search($params); |
||
57 | |||
58 | return $this->processResults($results); |
||
0 ignored issues
–
show
|
|||
59 | } |
||
60 | |||
61 | public function addDocument(DocumentInterface $document) |
||
62 | { |
||
63 | $params = [ |
||
64 | 'index' => $this->getIndexName($document->getLocale()), |
||
65 | 'type' => $document->getType()->getName(), |
||
66 | 'id' => $document->getIdentifier(), |
||
67 | 'body' => $this->createDocumentBody($document), |
||
68 | ]; |
||
69 | |||
70 | $this->getClient()->index($params); |
||
71 | } |
||
72 | |||
73 | public function addDocuments(Collection $documents, string $locale, string $type) |
||
74 | { |
||
75 | $documents->map(function (DocumentInterface $document) { |
||
76 | $this->addDocument($document); |
||
77 | }); |
||
78 | } |
||
79 | |||
80 | public function removeDocument(DocumentInterface $document) |
||
81 | { |
||
82 | $params = [ |
||
83 | 'index' => $this->getIndexName($document->getLocale()), |
||
84 | 'type' => $document->getType()->getName(), |
||
85 | 'id' => $document->getIdentifier(), |
||
86 | ]; |
||
87 | |||
88 | $this->getClient()->delete($params); |
||
89 | } |
||
90 | |||
91 | public function updateDocument(DocumentInterface $document) |
||
92 | { |
||
93 | $params = [ |
||
94 | 'index' => $this->getIndexName($document->getLocale()), |
||
95 | 'type' => $document->getType()->getName(), |
||
96 | 'id' => $document->getIdentifier(), |
||
97 | 'body' => $this->createDocumentBody($document), |
||
98 | ]; |
||
99 | |||
100 | $this->getClient()->update($params); |
||
101 | } |
||
102 | |||
103 | public function getIndexName(string $locale): string |
||
104 | { |
||
105 | return sprintf('%s%s', $this->getOption('indexPrefix'), $locale); |
||
106 | } |
||
107 | |||
108 | public function createIndex(string $locale, string $type) |
||
109 | { |
||
110 | return $this->getClient()->indices()->create([ |
||
111 | 'index' => $this->getIndexName($locale), |
||
112 | 'body' => [ |
||
113 | 'settings' => [ |
||
114 | 'number_of_shards' => $this->getOption('shards'), |
||
115 | 'number_of_replicas' => $this->getOption('replicas'), |
||
116 | ], |
||
117 | ], |
||
118 | ]); |
||
119 | } |
||
120 | |||
121 | public function removeIndex(string $locale, string $type) |
||
122 | { |
||
123 | if (false === $this->hasIndex($locale)) { |
||
124 | return false; |
||
125 | } |
||
126 | |||
127 | return $this->getClient()->indices()->delete([ |
||
128 | 'index' => $this->getIndexName($locale), |
||
129 | ]); |
||
130 | } |
||
131 | |||
132 | public function flushIndex(string $locale, string $type) |
||
133 | { |
||
134 | if (false === $this->hasIndex($locale)) { |
||
135 | return $this->createIndex($locale, $type); |
||
136 | } |
||
137 | |||
138 | return $this->getClient()->indices()->flush([ |
||
139 | 'index' => $this->getIndexName($locale), |
||
140 | ]); |
||
141 | } |
||
142 | |||
143 | public function getStats(string $locale) |
||
144 | { |
||
145 | return $this->getClient()->indices()->stats([ |
||
146 | 'index' => $this->getIndexName($locale), |
||
147 | ]); |
||
148 | } |
||
149 | |||
150 | private function createQueryBuilder(SearchRequestInterface $request): QueryBuilderInterface |
||
151 | { |
||
152 | $queryBuilderClass = $this->getOption('builderClass'); |
||
153 | |||
154 | return new $queryBuilderClass($request, $this->getOption('termMinLength')); |
||
155 | } |
||
156 | |||
157 | private function createDocumentBody(DocumentInterface $document): array |
||
158 | { |
||
159 | $body = []; |
||
160 | |||
161 | $document->getFields()->map(function (FieldInterface $field) use (&$body) { |
||
162 | $body[$field->getName()] = $field->getValue(); |
||
163 | }); |
||
164 | |||
165 | return $body; |
||
166 | } |
||
167 | |||
168 | private function getClient(): Client |
||
169 | { |
||
170 | if (null === $this->client) { |
||
171 | $this->client = ClientBuilder::create()->build(); |
||
172 | } |
||
173 | |||
174 | return $this->client; |
||
175 | } |
||
176 | |||
177 | private function hasIndex(string $locale): bool |
||
178 | { |
||
179 | return $this->getClient()->indices()->exists([ |
||
180 | 'index' => $this->getIndexName($locale), |
||
181 | ]); |
||
182 | } |
||
183 | |||
184 | private function processResults(array $results): array |
||
185 | { |
||
186 | $identifiers = []; |
||
187 | |||
188 | foreach ($results['hits']['hits'] as $hit) { |
||
189 | $identifiers[] = $hit['_id']; |
||
190 | } |
||
191 | |||
192 | return $identifiers; |
||
193 | } |
||
194 | |||
195 | private function getOption(string $name) |
||
196 | { |
||
197 | return $this->configurator->getSearchAdapterOptions()[$name]; |
||
198 | } |
||
199 | } |
||
200 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: