1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Controller\Catalog; |
14
|
|
|
|
15
|
|
|
use Elastica\Client; |
16
|
|
|
use Elastica\Connection; |
17
|
|
|
use Elastica\Query\BoolQuery; |
18
|
|
|
use Elastica\QueryBuilder\DSL\Query; |
19
|
|
|
use FOS\RestBundle\View\View; |
20
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
|
24
|
|
|
final class GetCatalogAction |
25
|
|
|
{ |
26
|
|
|
/** @var ViewHandlerInterface */ |
27
|
|
|
private $viewHandler; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $host; |
31
|
|
|
|
32
|
|
|
/** @var int */ |
33
|
|
|
private $port; |
34
|
|
|
|
35
|
|
|
public function __construct(ViewHandlerInterface $viewHandler, string $host, int $port) |
36
|
|
|
{ |
37
|
|
|
$this->host = $host; |
38
|
|
|
$this->port = $port; |
39
|
|
|
$this->viewHandler = $viewHandler; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function __invoke(Request $request): Response |
43
|
|
|
{ |
44
|
|
|
$client = new Client(); |
45
|
|
|
$client->addConnection(new Connection(['host' => $this->host, 'port' => $this->port])); |
46
|
|
|
|
47
|
|
|
$index = $request->attributes->get('index'); |
48
|
|
|
$type = $request->attributes->get('type'); |
49
|
|
|
|
50
|
|
|
if (null === $type) { |
51
|
|
|
$requestPath = sprintf('%s/_search', $index); |
52
|
|
|
} else { |
53
|
|
|
$requestPath = sprintf('%s_%s/%s/_search', $index, $type, $type); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$queryParameters = $request->query->all(); |
57
|
|
|
|
58
|
|
|
$requestBody = []; |
59
|
|
|
|
60
|
|
|
if (isset($queryParameters['request'])) { |
61
|
|
|
$requestBody = \json_decode($queryParameters['request'], true); |
62
|
|
|
|
63
|
|
|
$boolQueryTerms = $requestBody['query']['bool']['filter']['terms'] ?? null; |
64
|
|
|
|
65
|
|
|
if (isset($boolQueryTerms['configurable_children.sku'])) { |
66
|
|
|
$sku = $boolQueryTerms['configurable_children.sku'][0]; |
67
|
|
|
} elseif (isset($boolQueryTerms['sku'])) { |
68
|
|
|
$sku = $boolQueryTerms['sku'][0]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (isset($sku)) { |
72
|
|
|
$boolQuery = new BoolQuery(); |
73
|
|
|
|
74
|
|
|
$boolQuery->addShould( |
75
|
|
|
(new Query()) |
76
|
|
|
->term()->setParam('sku', $sku) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$boolQuery->addShould( |
80
|
|
|
(new Query()) |
81
|
|
|
->nested() |
82
|
|
|
->setPath('configurable_children') |
83
|
|
|
->setQuery((new Query()) |
84
|
|
|
->term()->setParam('configurable_children.sku', $sku) |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$requestBody['query'] = $boolQuery->toArray(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$requestBody = \json_encode($requestBody); |
92
|
|
|
|
93
|
|
|
unset($queryParameters['request']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$elasticsearchResponse = $client->request($requestPath, Request::METHOD_GET, $requestBody, $queryParameters); |
97
|
|
|
|
98
|
|
|
return $this->viewHandler->handle(View::create($elasticsearchResponse->getData())); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|