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-434] Correctly assign campo keys to multiple terms within degree program #42

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Changes from 1 commit
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
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 @@
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 @@
* 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 @@

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 @@
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;
}
zhyian marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
}
Loading