Skip to content

Commit

Permalink
NTR: PISHPS-301: add elastic indexer for entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Jul 4, 2024
1 parent c843557 commit 4f6bedc
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Components\RefundManager\Elasticsearch;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Kiener\MolliePayments\Components\RefundManager\DAL\Refund\RefundDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IterableQuery;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Elasticsearch\Admin\Indexer\AbstractAdminIndexer;

class RefundAdminSearchIndexer extends AbstractAdminIndexer
{
/**
* @internal
*/
public function __construct(
private readonly Connection $connection,

Check failure on line 24 in src/Components/RefundManager/Elasticsearch/RefundAdminSearchIndexer.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 24
private readonly IteratorFactory $factory,
private readonly EntityRepository $repository,
private readonly int $indexingBatchSize
) {
}

public function getDecorated(): AbstractAdminIndexer
{
throw new DecorationPatternException(self::class);
}

public function getName(): string
{
return 'mollie_refund';
}

public function getEntity(): string
{
return RefundDefinition::ENTITY_NAME;
}

public function getIterator(): IterableQuery
{
return $this->factory->createIterator($this->getEntity(), null, $this->indexingBatchSize);
}

public function fetch(array $ids): array
{
$data = $this->connection->fetchAllAssociative(
'
SELECT LOWER(HEX(mollie_refund.id)) as id,
type,
public_description,
internal_description,
FROM mollie_refund
WHERE mollie_refund.id IN (:ids)
GROUP BY mollie_refund.id
',
[
'ids' => Uuid::fromHexToBytesList($ids),
],
[
'ids' => ArrayParameterType::BINARY,
]
);

$mapped = [];
foreach ($data as $row) {
$id = (string) $row['id'];
$text = \implode(' ', array_filter($row));
$mapped[$id] = ['id' => $id, 'text' => \strtolower($text)];
}

return $mapped;
}

public function globalData(array $result, Context $context): array
{
$ids = array_column($result['hits'], 'id');

return [
'total' => (int) $result['total'],
'data' => $this->repository->search(new Criteria($ids), $context)->getEntities(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Components\Subscription\Elasticsearch;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Kiener\MolliePayments\Components\Subscription\DAL\Subscription\SubscriptionDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IterableQuery;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Elasticsearch\Admin\Indexer\AbstractAdminIndexer;

class SubscriptionAdminSearchIndexer extends AbstractAdminIndexer
{
/**
* @internal
*/
public function __construct(
private readonly Connection $connection,

Check failure on line 24 in src/Components/Subscription/Elasticsearch/SubscriptionAdminSearchIndexer.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 24
private readonly IteratorFactory $factory,
private readonly EntityRepository $repository,
private readonly int $indexingBatchSize
) {
}

public function getDecorated(): AbstractAdminIndexer
{
throw new DecorationPatternException(self::class);
}

public function getName(): string
{
return 'mollie_subscription';
}

public function getEntity(): string
{
return SubscriptionDefinition::ENTITY_NAME;
}

public function getIterator(): IterableQuery
{
return $this->factory->createIterator($this->getEntity(), null, $this->indexingBatchSize);
}

public function fetch(array $ids): array
{
$data = $this->connection->fetchAllAssociative(
'
SELECT LOWER(HEX(mollie_subscription.id)) as id,
mollie_id,
mollie_customer_id,
description,
next_payment_at,
last_reminded_at,
canceled_at,
mandate_id,
status
FROM mollie_subscription
WHERE mollie_subscription.id IN (:ids)
GROUP BY mollie_subscription.id
',
[
'ids' => Uuid::fromHexToBytesList($ids),
],
[
'ids' => ArrayParameterType::BINARY,
]
);

$mapped = [];
foreach ($data as $row) {
$id = (string) $row['id'];
$text = \implode(' ', array_filter($row));
$mapped[$id] = ['id' => $id, 'text' => \strtolower($text)];
}

return $mapped;
}

public function globalData(array $result, Context $context): array
{
$ids = array_column($result['hits'], 'id');

return [
'total' => (int) $result['total'],
'data' => $this->repository->search(new Criteria($ids), $context)->getEntities(),
];
}
}
11 changes: 11 additions & 0 deletions src/Resources/config/services/refundmanager/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,16 @@
<tag name="shopware.entity.extension"/>
</service>

<service id="Kiener\MolliePayments\Components\RefundManager\Elasticsearch\RefundAdminSearchIndexer">

<argument type="service" id="Doctrine\DBAL\Connection"/>
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory" />
<argument type="service" id="mollie_refund.repository"/>
<argument>%elasticsearch.indexing_batch_size%</argument>

<tag name="shopware.elastic.admin-searcher-index" key="mollie_refund"/>
</service>


</services>
</container>
10 changes: 10 additions & 0 deletions src/Resources/config/services/subscription/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,15 @@
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\Services\SubscriptionRenewing\OrderCloneService"/>
</service>

<service id="Kiener\MolliePayments\Components\Subscription\Elasticsearch\SubscriptionAdminSearchIndexer">
<argument type="service" id="Doctrine\DBAL\Connection"/>
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory" />
<argument type="service" id="mollie_subscription.repository"/>
<argument>%elasticsearch.indexing_batch_size%</argument>

<tag name="shopware.elastic.admin-searcher-index" key="mollie_subscription"/>

</service>

</services>
</container>

0 comments on commit 4f6bedc

Please sign in to comment.