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

OrderAdminSearchIndexer::globalData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 2
dl 0
loc 7
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\Order\OrderDefinition;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IterableQuery;
10
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
11
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
12
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
13
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
14
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
15
use Shopware\Core\Framework\Log\Package;
16
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
17
use Shopware\Core\Framework\Uuid\Uuid;
18
19
/**
20
 * @internal
21
 */
22
#[Package('system-settings')]
23
final class OrderAdminSearchIndexer extends AbstractAdminIndexer
24
{
25
    private Connection $connection;
26
27
    private IteratorFactory $factory;
28
29
    private EntityRepository $repository;
30
31
    private int $indexingBatchSize;
32
33
    public function __construct(
34
        Connection $connection,
35
        IteratorFactory $factory,
36
        EntityRepository $repository,
37
        int $indexingBatchSize
38
    ) {
39
        $this->connection = $connection;
40
        $this->factory = $factory;
41
        $this->repository = $repository;
42
        $this->indexingBatchSize = $indexingBatchSize;
43
    }
44
45
    public function getDecorated(): AbstractAdminIndexer
46
    {
47
        throw new DecorationPatternException(self::class);
48
    }
49
50
    public function getEntity(): string
51
    {
52
        return OrderDefinition::ENTITY_NAME;
53
    }
54
55
    public function getName(): string
56
    {
57
        return 'order-listing';
58
    }
59
60
    public function getIterator(): IterableQuery
61
    {
62
        return $this->factory->createIterator($this->getEntity(), null, $this->indexingBatchSize);
63
    }
64
65
    /**
66
     * @param array<string, mixed> $result
67
     *
68
     * @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...
69
     */
70
    public function globalData(array $result, Context $context): array
71
    {
72
        $ids = array_column($result['hits'], 'id');
73
74
        return [
75
            'total' => (int) $result['total'],
76
            'data' => $this->repository->search(new Criteria($ids), $context)->getEntities(),
77
        ];
78
    }
79
80
    /**
81
     * @param array<string>|array<int, array<string>> $ids
82
     *
83
     * @throws \Doctrine\DBAL\Exception
84
     *
85
     * @return array<int|string, array<string, mixed>>
86
     */
87
    public function fetch(array $ids): array
88
    {
89
        $data = $this->connection->fetchAllAssociative(
90
            '
91
            SELECT LOWER(HEX(order.id)) as id,
92
                   GROUP_CONCAT(DISTINCT tag.name) as tags,
93
                   GROUP_CONCAT(DISTINCT country_translation.name) as country,
94
                   GROUP_CONCAT(DISTINCT order_address.city) as city,
95
                   GROUP_CONCAT(DISTINCT order_address.street) as street,
96
                   GROUP_CONCAT(DISTINCT order_address.zipcode) as zipcode,
97
                   GROUP_CONCAT(DISTINCT order_address.phone_number) as phone_number,
98
                   GROUP_CONCAT(DISTINCT order_address.additional_address_line1) as additional_address_line1,
99
                   GROUP_CONCAT(DISTINCT order_address.additional_address_line2) as additional_address_line2,
100
                   GROUP_CONCAT(DISTINCT JSON_UNQUOTE(JSON_EXTRACT(document.config, "$.documentNumber"))) as documentNumber,
101
                   order_customer.first_name,
102
                   order_customer.last_name,
103
                   order_customer.email,
104
                   order_customer.company,
105
                   order_customer.customer_number,
106
                   `order`.order_number,
107
                   `order`.amount_total,
108
                   order_delivery.tracking_codes
109
            FROM `order`
110
                LEFT JOIN order_customer
111
                    ON `order`.id = order_customer.order_id
112
                LEFT JOIN order_address
113
                    ON `order`.id = order_address.order_id
114
                LEFT JOIN country
115
                    ON order_address.country_id = country.id
116
                LEFT JOIN country_translation
117
                    ON country.id = country_translation.country_id
118
                LEFT JOIN order_tag
119
                    ON `order`.id = order_tag.order_id
120
                LEFT JOIN tag
121
                    ON order_tag.tag_id = tag.id
122
                LEFT JOIN order_delivery
123
                    ON `order`.id = order_delivery.order_id
124
                LEFT JOIN document
125
                    ON `order`.id = document.order_id
126
            WHERE order.id IN (:ids) AND `order`.version_id = :versionId
127
            GROUP BY order.id
128
        ',
129
            [
130
                'ids' => Uuid::fromHexToBytesList($ids),
131
                'versionId' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION),
132
            ],
133
            [
134
                'ids' => Connection::PARAM_STR_ARRAY,
135
            ]
136
        );
137
138
        $mapped = [];
139
        foreach ($data as $row) {
140
            $id = $row['id'];
141
            $text = \implode(' ', array_filter(array_unique(array_values($row))));
142
            $mapped[$id] = ['id' => $id, 'text' => \strtolower($text)];
143
        }
144
145
        return $mapped;
146
    }
147
}
148