From 5b5de9d8392b92d501247677eb0b32a2ae6e7559 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Thu, 15 Aug 2024 16:29:26 +0300 Subject: [PATCH 1/3] fix: prevent loading of degree programs when invalid his codes are provided --- src/Infrastructure/Repository/CampoKeysRepository.php | 8 +------- src/Infrastructure/Repository/WpQueryArgsBuilder.php | 10 +++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Infrastructure/Repository/CampoKeysRepository.php b/src/Infrastructure/Repository/CampoKeysRepository.php index 3031a45..9f7bd57 100644 --- a/src/Infrastructure/Repository/CampoKeysRepository.php +++ b/src/Infrastructure/Repository/CampoKeysRepository.php @@ -60,7 +60,6 @@ public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoK /** * Return a map of taxonomy keys to terms based on a given HIS code. * - * @throws RuntimeException * @return array */ public function taxonomyToTermsMapFromCampoKeys(CampoKeys $campoKeys): array @@ -77,12 +76,7 @@ public function taxonomyToTermsMapFromCampoKeys(CampoKeys $campoKeys): array } $term = $this->findTermByCampoKey($taxonomy, $campoKey); - - if (! $term instanceof WP_Term) { - throw new RuntimeException('Could not find term for Campo key: ' . $campoKey); - } - - $result[$taxonomy] = $term->term_id; + $result[$taxonomy] = $term instanceof WP_Term ? $term->term_id : 0; } return $result; diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php index 4fe1d87..6c7b74d 100644 --- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php +++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php @@ -107,13 +107,9 @@ public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryA 'relation' => 'AND', ]; - try { - $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys( - CampoKeys::fromHisCode($hisCode) - ); - } catch (RuntimeException) { - continue; - } + $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys( + CampoKeys::fromHisCode($hisCode) + ); if (count($taxonomyToTermMapping) === 0) { continue; From 4908dfe622073f6614a0eb216808c0bdc711a0aa Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Wed, 4 Sep 2024 10:42:01 +0300 Subject: [PATCH 2/3] feat: extend degree program data to include campo keys from all associated terms --- src/Domain/CampoKeys.php | 14 +++++++------- .../Repository/CampoKeysRepository.php | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Domain/CampoKeys.php b/src/Domain/CampoKeys.php index 7788684..666cd57 100644 --- a/src/Domain/CampoKeys.php +++ b/src/Domain/CampoKeys.php @@ -5,7 +5,7 @@ namespace Fau\DegreeProgram\Common\Domain; /** - * @psalm-type CampoKeysMap = array, string> + * @psalm-type CampoKeysMap = array, array|string> */ final class CampoKeys { @@ -13,13 +13,13 @@ final class CampoKeys 'type' => 'object', 'properties' => [ DegreeProgram::DEGREE => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::AREA_OF_STUDY => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::LOCATION => [ - 'type' => 'string', + 'type' => 'array', ], ], ]; @@ -28,13 +28,13 @@ final class CampoKeys 'type' => 'object', 'properties' => [ DegreeProgram::DEGREE => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::AREA_OF_STUDY => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::LOCATION => [ - 'type' => 'string', + 'type' => 'array', ], ], ]; diff --git a/src/Infrastructure/Repository/CampoKeysRepository.php b/src/Infrastructure/Repository/CampoKeysRepository.php index 9f7bd57..2a12279 100644 --- a/src/Infrastructure/Repository/CampoKeysRepository.php +++ b/src/Infrastructure/Repository/CampoKeysRepository.php @@ -51,7 +51,7 @@ public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoK continue; } - $map[$campoKeyType] = $campoKey; + $map[$campoKeyType][$term->term_id] = $campoKey; } return CampoKeys::fromArray($map); @@ -69,9 +69,9 @@ public function taxonomyToTermsMapFromCampoKeys(CampoKeys $campoKeys): array $campoKeys = $campoKeys->asArray(); foreach (self::TAXONOMY_TO_CAMPO_KEY_MAP as $taxonomy => $campoKeyType) { - $campoKey = $campoKeys[$campoKeyType] ?? ''; + $campoKey = $campoKeys[$campoKeyType] ?? null; - if ($campoKey === '') { + if (!is_string($campoKey) || $campoKey === '') { continue; } From 4a07adda6f38cc8099e8875c07bb04e354174056 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Wed, 4 Sep 2024 15:09:20 +0300 Subject: [PATCH 3/3] refactor: improve codebase --- src/Domain/CampoKeys.php | 16 +--------------- .../Repository/CampoKeysRepository.php | 18 ++++++++++++++++-- .../Repository/WpQueryArgsBuilder.php | 4 +--- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Domain/CampoKeys.php b/src/Domain/CampoKeys.php index 666cd57..48128bd 100644 --- a/src/Domain/CampoKeys.php +++ b/src/Domain/CampoKeys.php @@ -5,7 +5,7 @@ namespace Fau\DegreeProgram\Common\Domain; /** - * @psalm-type CampoKeysMap = array, array|string> + * @psalm-type CampoKeysMap = array, array> */ final class CampoKeys { @@ -45,8 +45,6 @@ final class CampoKeys DegreeProgram::LOCATION, ]; - private const HIS_CODE_DELIMITER = '|'; - private function __construct( /** * @var CampoKeysMap $map @@ -68,18 +66,6 @@ public static function fromArray(array $map): self return new self($map); } - public static function fromHisCode(string $hisCode): self - { - $parts = explode(self::HIS_CODE_DELIMITER, $hisCode); - $map = [ - DegreeProgram::DEGREE => $parts[0] ?? null, - DegreeProgram::AREA_OF_STUDY => $parts[1] ?? null, - DegreeProgram::LOCATION => $parts[6] ?? null, - ]; - - return new self(array_filter($map, fn($value) => !is_null($value))); - } - /** * @return CampoKeysMap */ diff --git a/src/Infrastructure/Repository/CampoKeysRepository.php b/src/Infrastructure/Repository/CampoKeysRepository.php index 2a12279..3cd6f3e 100644 --- a/src/Infrastructure/Repository/CampoKeysRepository.php +++ b/src/Infrastructure/Repository/CampoKeysRepository.php @@ -24,6 +24,8 @@ final class CampoKeysRepository public const CAMPO_KEY_TERM_META_KEY = 'uniquename'; + private const HIS_CODE_DELIMITER = '|'; + public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoKeys { /** @var WP_Error|array $terms */ @@ -62,11 +64,11 @@ public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoK * * @return array */ - public function taxonomyToTermsMapFromCampoKeys(CampoKeys $campoKeys): array + public function taxonomyToTermsMapFromHisCode(string $hisCode): array { $result = []; - $campoKeys = $campoKeys->asArray(); + $campoKeys = $this->campoKeysFromHisCode($hisCode); foreach (self::TAXONOMY_TO_CAMPO_KEY_MAP as $taxonomy => $campoKeyType) { $campoKey = $campoKeys[$campoKeyType] ?? null; @@ -101,4 +103,16 @@ private function findTermByCampoKey(string $taxonomy, string $campoKey): ?WP_Ter return $terms[0] ?? null; } + + public function campoKeysFromHisCode(string $hisCode): array + { + $parts = explode(self::HIS_CODE_DELIMITER, $hisCode); + $map = [ + DegreeProgram::DEGREE => $parts[0] ?? null, + DegreeProgram::AREA_OF_STUDY => $parts[1] ?? null, + DegreeProgram::LOCATION => $parts[6] ?? null, + ]; + + return array_filter($map, fn($value) => !is_null($value)); + } } diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php index 6c7b74d..8f3f41d 100644 --- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php +++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php @@ -107,9 +107,7 @@ public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryA 'relation' => 'AND', ]; - $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys( - CampoKeys::fromHisCode($hisCode) - ); + $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromHisCode($hisCode); if (count($taxonomyToTermMapping) === 0) { continue;