1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Elasticsearch\Admin\Indexer; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Shopware\Core\Content\Property\PropertyGroupDefinition; |
7
|
|
|
use Shopware\Core\Framework\Context; |
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IterableQuery; |
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory; |
10
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Entity; |
11
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection; |
12
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
13
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
14
|
|
|
use Shopware\Core\Framework\Log\Package; |
15
|
|
|
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException; |
16
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
#[Package('system-settings')] |
22
|
|
|
final class PropertyGroupAdminSearchIndexer extends AbstractAdminIndexer |
23
|
|
|
{ |
24
|
|
|
private Connection $connection; |
25
|
|
|
|
26
|
|
|
private IteratorFactory $factory; |
27
|
|
|
|
28
|
|
|
private EntityRepository $repository; |
29
|
|
|
|
30
|
|
|
private int $indexingBatchSize; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
Connection $connection, |
34
|
|
|
IteratorFactory $factory, |
35
|
|
|
EntityRepository $repository, |
36
|
|
|
int $indexingBatchSize |
37
|
|
|
) { |
38
|
|
|
$this->connection = $connection; |
39
|
|
|
$this->factory = $factory; |
40
|
|
|
$this->repository = $repository; |
41
|
|
|
$this->indexingBatchSize = $indexingBatchSize; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getDecorated(): AbstractAdminIndexer |
45
|
|
|
{ |
46
|
|
|
throw new DecorationPatternException(self::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getEntity(): string |
50
|
|
|
{ |
51
|
|
|
return PropertyGroupDefinition::ENTITY_NAME; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getName(): string |
55
|
|
|
{ |
56
|
|
|
return 'property-group-listing'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getIterator(): IterableQuery |
60
|
|
|
{ |
61
|
|
|
return $this->factory->createIterator($this->getEntity(), null, $this->indexingBatchSize); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array<string, mixed> $result |
66
|
|
|
* |
67
|
|
|
* @return array{total:int, data:EntityCollection<Entity>} |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
public function globalData(array $result, Context $context): array |
70
|
|
|
{ |
71
|
|
|
$ids = array_column($result['hits'], 'id'); |
72
|
|
|
|
73
|
|
|
return [ |
74
|
|
|
'total' => (int) $result['total'], |
75
|
|
|
'data' => $this->repository->search(new Criteria($ids), $context)->getEntities(), |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array<string>|array<int, array<string>> $ids |
81
|
|
|
* |
82
|
|
|
* @throws \Doctrine\DBAL\Exception |
83
|
|
|
* |
84
|
|
|
* @return array<int|string, array<string, mixed>> |
85
|
|
|
*/ |
86
|
|
|
public function fetch(array $ids): array |
87
|
|
|
{ |
88
|
|
|
$data = $this->connection->fetchAllAssociative( |
89
|
|
|
' |
90
|
|
|
SELECT LOWER(HEX(property_group.id)) as id, |
91
|
|
|
GROUP_CONCAT(DISTINCT property_group_translation.name) as name |
92
|
|
|
FROM property_group |
93
|
|
|
INNER JOIN property_group_translation |
94
|
|
|
ON property_group.id = property_group_translation.property_group_id |
95
|
|
|
WHERE property_group.id IN (:ids) |
96
|
|
|
GROUP BY property_group.id |
97
|
|
|
', |
98
|
|
|
[ |
99
|
|
|
'ids' => Uuid::fromHexToBytesList($ids), |
100
|
|
|
], |
101
|
|
|
[ |
102
|
|
|
'ids' => Connection::PARAM_STR_ARRAY, |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$mapped = []; |
107
|
|
|
foreach ($data as $row) { |
108
|
|
|
$id = $row['id']; |
109
|
|
|
$text = \implode(' ', array_filter($row)); |
110
|
|
|
$mapped[$id] = ['id' => $id, 'text' => \strtolower($text)]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $mapped; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|