From 0f51b43369dab5d7b144591297a25b67b070bec7 Mon Sep 17 00:00:00 2001 From: Ben Scott Date: Thu, 10 Aug 2017 11:43:59 +0100 Subject: [PATCH] Use explicit functions for checking type in CoreEntityMapper This saves a few string comparisons. Check programme types before groups types as that's a more likely outcome. --- .../ProgrammesDbToDomain/CoreEntityMapper.php | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/Mapper/ProgrammesDbToDomain/CoreEntityMapper.php b/src/Mapper/ProgrammesDbToDomain/CoreEntityMapper.php index 4a8f04c2..a5510e3f 100644 --- a/src/Mapper/ProgrammesDbToDomain/CoreEntityMapper.php +++ b/src/Mapper/ProgrammesDbToDomain/CoreEntityMapper.php @@ -25,6 +25,9 @@ class CoreEntityMapper extends AbstractMapper { + private const PROGRAMME_TYPES = ['brand', 'clip', 'episode', 'series']; + private const GROUP_TYPES = ['collection', 'franchise', 'gallery', 'season']; + private $cache = []; /** @@ -36,12 +39,13 @@ class CoreEntityMapper extends AbstractMapper */ public function getCacheKey(array $dbEntity): string { - if ($this->entityIsA(Group::class, $dbEntity)) { - return $this->getCacheKeyForGroup($dbEntity); - } - if ($this->entityIsA(Programme::class, $dbEntity)) { + if ($this->isProgramme($dbEntity)) { return $this->getCacheKeyForProgramme($dbEntity); } + if ($this->isGroup($dbEntity)) { + return $this->getCacheKeyForGroup($dbEntity); + } + throw new InvalidArgumentException('Unrecognized Core Entity'); } @@ -72,18 +76,18 @@ public function getCacheKeyForProgramme(array $dbProgramme): string */ public function getDomainModel(array $dbEntity): CoreEntity { - if ($this->entityIsA(Group::class, $dbEntity)) { - return $this->getDomainModelForGroup($dbEntity); - } - if ($this->entityIsA(Programme::class, $dbEntity)) { + if ($this->isProgramme($dbEntity)) { return $this->getDomainModelForProgramme($dbEntity); } + if ($this->isGroup($dbEntity)) { + return $this->getDomainModelForGroup($dbEntity); + } throw new InvalidArgumentException('Unrecognized Core Entity'); } public function getDomainModelForGroup(array $dbEntity): Group { - if (!$this->entityIsA(Group::class, $dbEntity)) { + if (!$this->isGroup($dbEntity)) { throw new InvalidArgumentException('Could not build domain model for unknown group type "' . ($dbEntity['type'] ?? '') . '"'); } @@ -98,7 +102,7 @@ public function getDomainModelForGroup(array $dbEntity): Group public function getDomainModelForProgramme(array $dbEntity): Programme { - if (!$this->entityIsA(Programme::class, $dbEntity)) { + if (!$this->isProgramme($dbEntity)) { throw new InvalidArgumentException('Could not build domain model for unknown programme type "' . ($dbEntity['type'] ?? '') . '"'); } @@ -557,20 +561,13 @@ private function getSynopses($dbEntity): Synopses ); } - private function entityIsA(string $string, array $dbEntity): bool + private function isProgramme(array $dbEntity): bool { - if (!isset($dbEntity['type'])) { - return false; - } - - if ($string == Group::class && in_array($dbEntity['type'], ['collection', 'franchise', 'gallery', 'season'])) { - return true; - } - - if ($string == Programme::class && in_array($dbEntity['type'], ['brand', 'clip', 'episode', 'series'])) { - return true; - } + return isset($dbEntity['type']) && in_array($dbEntity['type'], self::PROGRAMME_TYPES); + } - return false; + private function isGroup(array $dbEntity): bool + { + return isset($dbEntity['type']) && in_array($dbEntity['type'], self::GROUP_TYPES); } }