Skip to content

Commit

Permalink
feat: correctly assign campo keys to multiple terms within degree pro…
Browse files Browse the repository at this point in the history
…gram
  • Loading branch information
zhyian committed Sep 4, 2024
1 parent 98ddde8 commit 2ba357a
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/Infrastructure/Search/FilterableTermsUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ private function setObjectTerms(
string $taxonomy,
MultilingualString|MultilingualList|MultilingualLink|MultilingualLinks|Degree|AdmissionRequirement $property
): void {

$termIds = $this->maybeCreateTerms($taxonomy, $property);
/** @var array<int, TermData> $terms */
$terms = $this->maybeCreateTerms($taxonomy, $property);
wp_set_object_terms(
$rawView->id()->asInt(),
$termIds,
array_keys($terms),
$taxonomy
);

if (! isset($termIds[0])) {
if (!$terms) {
return;
}

$this->maybeUpdateCampoKeys($rawView, $taxonomy, (int) $termIds[0]);
$this->maybeUpdateCampoKeys($rawView, $taxonomy, $terms);
}

private function isValidPostId(int $postId): bool
Expand All @@ -179,6 +179,8 @@ private function isValidPostId(int $postId): bool
* phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
*
* Refactoring could decrease performance.
*
* @return array<int, TermData>
*/
private function maybeCreateTerms(
string $taxonomy,
Expand Down Expand Up @@ -225,15 +227,15 @@ private function maybeCreateTerms(

if ($termData->termId()) {
// Term was persisted already
$result[] = $termData->termId();
$result[$termData->termId()] = $termData;
$this->updateTerm($termData);
continue;
}

$termId = $this->createTerm($termData);

if (is_int($termId)) {
$result[] = $termId;
$result[$termId] = $termData;
$taxonomiesCache[$taxonomy][$termId] = [
self::TAXONOMIES_CACHE_NAME_PROPERTY => $termData->name()->inGerman(),
self::TAXONOMIES_CACHE_ORIGINAL_ID_PROPERTY => $termData->remoteTermId(),
Expand Down Expand Up @@ -540,16 +542,25 @@ private static function arraySearchBy(array $array, string $property, mixed $val
return null;
}

private function maybeUpdateCampoKeys(DegreeProgramViewRaw $rawView, string $taxonomy, int $termId): void
private function maybeUpdateCampoKeys(DegreeProgramViewRaw $rawView, string $taxonomy, array $terms): void
{
$campoKeys = $rawView->campoKeys()->asArray();
$campoKeyType = CampoKeysRepository::TAXONOMY_TO_CAMPO_KEY_MAP[$taxonomy] ?? '';
$campoKey = $campoKeys[$campoKeyType] ?? null;
/** @var TermData $term */
foreach ($terms as $term) {
$campoKeys = $rawView->campoKeys()->asArray();
$campoKeyType = CampoKeysRepository::TAXONOMY_TO_CAMPO_KEY_MAP[$taxonomy] ?? '';
$taxonomyCampoKeys = $campoKeys[$campoKeyType] ?? null;

if (is_null($campoKey)) {
return;
}
if (!is_array($taxonomyCampoKeys)) {

Check failure on line 553 in src/Infrastructure/Search/FilterableTermsUpdater.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php / static-analysis-php

RedundantConditionGivenDocblockType

src/Infrastructure/Search/FilterableTermsUpdater.php:553:18: RedundantConditionGivenDocblockType: Docblock-defined type null|string for $taxonomyCampoKeys is never array<array-key, mixed> (see https://psalm.dev/156)
continue;
}

update_term_meta($termId, CampoKeysRepository::CAMPO_KEY_TERM_META_KEY, $campoKey);
$termCampoKey = $taxonomyCampoKeys[$term->remoteTermId()] ?? null;

Check failure on line 557 in src/Infrastructure/Search/FilterableTermsUpdater.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php / static-analysis-php

MixedArrayAccess

src/Infrastructure/Search/FilterableTermsUpdater.php:557:29: MixedArrayAccess: Cannot access array value on mixed variable $taxonomyCampoKeys (see https://psalm.dev/051)

if (is_null($termCampoKey)) {
continue;
}

update_term_meta($term->termId(), CampoKeysRepository::CAMPO_KEY_TERM_META_KEY, $termCampoKey);
}
}
}

0 comments on commit 2ba357a

Please sign in to comment.