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

PromotionAdminSearchIndexer::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Promotion\PromotionDefinition;
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
 * @final
22
 */
23
#[Package('system-settings')]
24
class PromotionAdminSearchIndexer extends AbstractAdminIndexer
25
{
26
    private Connection $connection;
27
28
    private IteratorFactory $factory;
29
30
    private EntityRepository $repository;
31
32
    private int $indexingBatchSize;
33
34
    public function __construct(
35
        Connection $connection,
36
        IteratorFactory $factory,
37
        EntityRepository $repository,
38
        int $indexingBatchSize
39
    ) {
40
        $this->connection = $connection;
41
        $this->factory = $factory;
42
        $this->repository = $repository;
43
        $this->indexingBatchSize = $indexingBatchSize;
44
    }
45
46
    public function getDecorated(): AbstractAdminIndexer
47
    {
48
        throw new DecorationPatternException(self::class);
49
    }
50
51
    public function getEntity(): string
52
    {
53
        return PromotionDefinition::ENTITY_NAME;
54
    }
55
56
    public function getName(): string
57
    {
58
        return 'promotion-listing';
59
    }
60
61
    public function getIterator(): IterableQuery
62
    {
63
        return $this->factory->createIterator($this->getEntity(), null, $this->indexingBatchSize);
64
    }
65
66
    /**
67
     * @param array<string, mixed> $result
68
     *
69
     * @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...
70
     */
71
    public function globalData(array $result, Context $context): array
72
    {
73
        $ids = array_column($result['hits'], 'id');
74
75
        return [
76
            'total' => (int) $result['total'],
77
            'data' => $this->repository->search(new Criteria($ids), $context)->getEntities(),
78
        ];
79
    }
80
81
    /**
82
     * @param array<string>|array<int, array<string>> $ids
83
     *
84
     * @throws \Doctrine\DBAL\Exception
85
     *
86
     * @return array<int|string, array<string, mixed>>
87
     */
88
    public function fetch(array $ids): array
89
    {
90
        $data = $this->connection->fetchAllAssociative(
91
            '
92
            SELECT LOWER(HEX(promotion.id)) as id,
93
                   GROUP_CONCAT(DISTINCT promotion_translation.name) as name
94
            FROM promotion
95
                INNER JOIN promotion_translation
96
                    ON promotion.id = promotion_translation.promotion_id
97
            WHERE promotion.id IN (:ids)
98
            GROUP BY promotion.id
99
        ',
100
            [
101
                'ids' => Uuid::fromHexToBytesList($ids),
102
            ],
103
            [
104
                'ids' => Connection::PARAM_STR_ARRAY,
105
            ]
106
        );
107
108
        $mapped = [];
109
        foreach ($data as $row) {
110
            $id = $row['id'];
111
            $text = \implode(' ', array_filter($row));
112
            $mapped[$id] = ['id' => $id, 'text' => \strtolower($text)];
113
        }
114
115
        return $mapped;
116
    }
117
}
118