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

[FAU-428] Split massive HIS code relationship queries into smaller ones #13

Merged
merged 1 commit into from
Jul 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ final class WordPressDatabaseDegreeProgramCollectionRepository implements Degree
{
public function __construct(
private DegreeProgramViewRepository $degreeProgramViewRepository,
private WpQueryArgsBuilder $queryArgsBuilder
private WpQueryArgsBuilder $queryArgsBuilder,
private WpQuerySplitter $querySplitter
) {
}

public function findRawCollection(CollectionCriteria $criteria): PaginationAwareCollection
{
$query = new WP_Query();
$criteria = $this->querySplitter->maybeSplitQuery($criteria);
/** @var array<int> $ids */
$ids = $query->query(
$this->queryArgsBuilder
Expand All @@ -48,6 +50,10 @@ public function findRawCollection(CollectionCriteria $criteria): PaginationAware
public function findTranslatedCollection(CollectionCriteria $criteria, string $languageCode): PaginationAwareCollection
{
$query = new WP_Query();
$criteria = $this->querySplitter->maybeSplitQuery(
$criteria->withLanguage($languageCode)
);

/** @var array<int> $ids */
$ids = $query->query(
$this->queryArgsBuilder
Expand Down
49 changes: 49 additions & 0 deletions src/Infrastructure/Repository/WpQuerySplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Fau\DegreeProgram\Common\Infrastructure\Repository;

use Fau\DegreeProgram\Common\Application\Repository\CollectionCriteria;
use WP_Query;

final class WpQuerySplitter
{
public function __construct(
private WpQueryArgsBuilder $queryArgsBuilder
) {
}

/**
* @param CollectionCriteria $criteria
* @return CollectionCriteria
*/
public function maybeSplitQuery(CollectionCriteria $criteria): CollectionCriteria
{
$hisCodes = $criteria->hisCodes();

if (count($hisCodes) <= 1) {
return $criteria;
}

$ids = [];

foreach ($hisCodes as $hisCode) {
$criteria = $criteria->withHisCodes([$hisCode]);
$query = new WP_Query();
/** @var array<int> $ids */
$ids = array_merge(
$query->query(
$this->queryArgsBuilder
->build($criteria)
->args()
),
$ids
);
}

return $criteria
->withHisCodes([])
->withInclude($ids);
}
}