Skip to content

Commit

Permalink
Use explicit functions for checking type in CoreEntityMapper
Browse files Browse the repository at this point in the history
This saves a few string comparisons.
Check programme types before groups types as that's a more likely
outcome.
  • Loading branch information
BPScott committed Aug 10, 2017
1 parent eefa817 commit 0f51b43
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/Mapper/ProgrammesDbToDomain/CoreEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

/**
Expand All @@ -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');
}

Expand Down Expand Up @@ -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'] ?? '') . '"');
}

Expand All @@ -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'] ?? '') . '"');
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 0f51b43

Please sign in to comment.