Passed
Push — 6.4 ( be76f2...e19c4d )
by Christian
14:08 queued 13s
created

CustomerGroupAdminSearchIndexer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Admin\Indexer;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupDefinition;
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 CustomerGroupAdminSearchIndexer 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 CustomerGroupDefinition::ENTITY_NAME;
52
    }
53
54
    public function getName(): string
55
    {
56
        return 'customer-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>}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{total:int, data:EntityCollection<Entity>} at position 8 could not be parsed: Expected '}' at position 8, but found 'EntityCollection'.
Loading history...
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(customer_group.id)) as id,
91
                   GROUP_CONCAT(DISTINCT customer_group_translation.name) as name
92
            FROM customer_group
93
                INNER JOIN customer_group_translation
94
                    ON customer_group.id = customer_group_translation.customer_group_id
95
            WHERE customer_group.id IN (:ids)
96
            GROUP BY customer_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