Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(orders): fix audits table being queried for each order in overview #1156

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/Pdk/Audit/Repository/WcPdkAuditRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
use MyParcelNL\Pdk\Storage\Contract\StorageInterface;
use MyParcelNL\WooCommerce\Database\Contract\WpDatabaseServiceInterface;

final class WcPdkAuditRepository extends Repository implements PdkAuditRepositoryInterface
/**
* @final
*/
class WcPdkAuditRepository extends Repository implements PdkAuditRepositoryInterface
{
/**
* @var \MyParcelNL\WooCommerce\Database\Contract\WpDatabaseServiceInterface
Expand All @@ -36,19 +39,21 @@ public function __construct(StorageInterface $storage, WpDatabaseServiceInterfac
*/
public function all(): AuditCollection
{
$audits = $this->wpDatabaseService->getAll(Pdk::get('tableNameAudits'));
return $this->retrieve('audits', function () {
$audits = $this->wpDatabaseService->getAll(Pdk::get('tableNameAudits'));

return new AuditCollection($audits->map(static function (array $audit): Audit {
return new Audit([
'id' => $audit['auditId'],
'arguments' => json_decode($audit['arguments'], true),
'action' => $audit['action'],
'model' => $audit['model'],
'modelIdentifier' => $audit['modelIdentifier'],
'created' => new DateTime($audit['created']),
'type' => $audit['type'],
]);
}));
return new AuditCollection($audits->map(static function (array $audit): Audit {
return new Audit([
'id' => $audit['auditId'],
'arguments' => json_decode($audit['arguments'], true),
'action' => $audit['action'],
'model' => $audit['model'],
'modelIdentifier' => $audit['modelIdentifier'],
'created' => new DateTime($audit['created']),
'type' => $audit['type'],
]);
}));
});
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Mock/MockWcPdkAuditRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\WooCommerce\Tests\Mock;

use MyParcelNL\WooCommerce\Pdk\Audit\Repository\WcPdkAuditRepository;
use Symfony\Contracts\Service\ResetInterface;

final class MockWcPdkAuditRepository extends WcPdkAuditRepository implements ResetInterface
{
public function reset(): void
{
$this->storage->delete('audits');
}
}
2 changes: 2 additions & 0 deletions tests/Mock/MockWcPdkBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MyParcelNL\Pdk\Base\Concern\PdkInterface;
use MyParcelNL\WooCommerce\Database\Contract\WpDatabaseServiceInterface;
use MyParcelNL\WooCommerce\Pdk\Audit\Repository\WcPdkAuditRepository;
use MyParcelNL\WooCommerce\Pdk\WcPdkBootstrapper;
use function DI\get;

Expand Down Expand Up @@ -98,6 +99,7 @@ protected function getAdditionalConfig(
self::$config,
[
WpDatabaseServiceInterface::class => get(MockWpDatabaseService::class),
WcPdkAuditRepository::class => get(MockWcPdkAuditRepository::class),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
'orderIds' => [$wcOrder->get_id()],
]);

/** @var \MyParcelNL\Pdk\Audit\Contract\PdkAuditRepositoryInterface $auditRepository */
/** @var \MyParcelNL\WooCommerce\Tests\Mock\MockWcPdkAuditRepository $auditRepository */
$auditRepository = Pdk::get(PdkAuditRepositoryInterface::class);

// Clear the cache of retrieved audits
$auditRepository->reset();

$audit = $auditRepository->all()
->first();

Expand Down
Loading