From d016fa613c445e1dbd6d50e564bc2d50e919bc59 Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Fri, 24 Jun 2022 19:30:54 +0300 Subject: [PATCH] Prioritize relative ids for episodes, as external ids reported from backends is often incorrect for episodes. --- src/Libs/Mappers/Import/DirectMapper.php | 14 +++++++++++++- src/Libs/Mappers/Import/MemoryMapper.php | 14 +++++++++++++- src/Libs/Storage/PDO/PDOAdapter.php | 18 +++++++++--------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Libs/Mappers/Import/DirectMapper.php b/src/Libs/Mappers/Import/DirectMapper.php index 374039f9..73f1be3f 100644 --- a/src/Libs/Mappers/Import/DirectMapper.php +++ b/src/Libs/Mappers/Import/DirectMapper.php @@ -527,7 +527,19 @@ final class DirectMapper implements ImportInterface return $entity->id; } - foreach ([...$entity->getRelativePointers(), ...$entity->getPointers()] as $key) { + // -- Prioritize relative ids for episodes, External ids are often incorrect for episodes. + if (true === $entity->isEpisode()) { + foreach ($entity->getRelativePointers() as $key) { + $lookup = $key . '/' . $entity->type; + if (null !== ($this->pointers[$lookup] ?? null)) { + return $this->pointers[$lookup]; + } + } + } + + // -- look up movies based on guid. + // -- if episode didn't have any match using relative id then fallback to external ids. + foreach ($entity->getPointers() as $key) { $lookup = $key . '/' . $entity->type; if (null !== ($this->pointers[$lookup] ?? null)) { return $this->pointers[$lookup]; diff --git a/src/Libs/Mappers/Import/MemoryMapper.php b/src/Libs/Mappers/Import/MemoryMapper.php index 9d32d206..3b34772c 100644 --- a/src/Libs/Mappers/Import/MemoryMapper.php +++ b/src/Libs/Mappers/Import/MemoryMapper.php @@ -451,7 +451,19 @@ final class MemoryMapper implements ImportInterface return self::GUID . $entity->id; } - foreach ([...$entity->getRelativePointers(), ...$entity->getPointers()] as $key) { + // -- Prioritize relative ids for episodes, External ids are often incorrect for episodes. + if (true === $entity->isEpisode()) { + foreach ($entity->getRelativePointers() as $key) { + $lookup = $key . '/' . $entity->type; + if (null !== ($this->pointers[$lookup] ?? null)) { + return $this->pointers[$lookup]; + } + } + } + + // -- look up movies based on guid. + // -- if episode didn't have any match using relative id then fallback to external ids. + foreach ($entity->getPointers() as $key) { $lookup = $key . '/' . $entity->type; if (null !== ($this->pointers[$lookup] ?? null)) { return $this->pointers[$lookup]; diff --git a/src/Libs/Storage/PDO/PDOAdapter.php b/src/Libs/Storage/PDO/PDOAdapter.php index a306b2ac..1ec26a93 100644 --- a/src/Libs/Storage/PDO/PDOAdapter.php +++ b/src/Libs/Storage/PDO/PDOAdapter.php @@ -465,15 +465,6 @@ final class PDOAdapter implements StorageInterface 'type' => $entity->type, ]; - foreach ($entity->getGuids() as $key => $val) { - if (empty($val)) { - continue; - } - - $guids[] = "JSON_EXTRACT(" . iFace::COLUMN_GUIDS . ",'$.{$key}') = :g_{$key}"; - $cond['g_' . $key] = $val; - } - $sqlEpisode = ''; if (true === $entity->isEpisode()) { @@ -492,6 +483,15 @@ final class PDOAdapter implements StorageInterface } } + foreach ($entity->getGuids() as $key => $val) { + if (empty($val)) { + continue; + } + + $guids[] = "JSON_EXTRACT(" . iFace::COLUMN_GUIDS . ",'$.{$key}') = :g_{$key}"; + $cond['g_' . $key] = $val; + } + if (empty($guids)) { return null; }