-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NTR: PISHPS-301: add elastic indexer for entities
- Loading branch information
Vitalij Mik
committed
Jul 4, 2024
1 parent
c843557
commit 4f6bedc
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/Components/RefundManager/Elasticsearch/RefundAdminSearchIndexer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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(), | ||
]; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/Components/Subscription/Elasticsearch/SubscriptionAdminSearchIndexer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters