do not pass same data twice

This commit is contained in:
thrillfall 2021-08-27 12:51:29 +02:00
parent 71c7b6e510
commit 20a647906a
2 changed files with 27 additions and 16 deletions

View File

@ -40,23 +40,15 @@ class EpisodeActionSaver
$episodeActions = $this->episodeActionReader->fromString($data); $episodeActions = $this->episodeActionReader->fromString($data);
foreach ($episodeActions as $episodeAction) { foreach ($episodeActions as $episodeAction) {
$episodeActionEntity = new EpisodeActionEntity(); $episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
$episodeActionEntity->setAction($episodeAction->getAction());
$episodeActionEntity->setPosition($episodeAction->getPosition());
$episodeActionEntity->setStarted($episodeAction->getStarted());
$episodeActionEntity->setTotal($episodeAction->getTotal());
$episodeActionEntity->setTimestamp($this->convertTimestampToDbDateTimeString($episodeAction->getTimestamp()));
$episodeActionEntity->setUserId($userId);
try { try {
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity); $episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
$episodeActionEntities[] = $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId); $episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
} catch (Exception $exception) { } catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$episodeActionEntities[] = $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId); $episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
} }
} }
} }
@ -76,23 +68,42 @@ class EpisodeActionSaver
} }
/** /**
* @param EpisodeAction $episodeAction
* @param EpisodeActionEntity $episodeActionEntity * @param EpisodeActionEntity $episodeActionEntity
* *
* @return EpisodeActionEntity * @return EpisodeActionEntity
*/ */
private function updateEpisodeAction( private function updateEpisodeAction(
EpisodeAction $episodeAction,
EpisodeActionEntity $episodeActionEntity, EpisodeActionEntity $episodeActionEntity,
string $userId string $userId
): EpisodeActionEntity ): EpisodeActionEntity
{ {
$idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode( $idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode(
$episodeAction->getEpisode(), $episodeActionEntity->getEpisode(),
$userId $userId
)->getId(); )->getId();
$episodeActionEntity->setId($idEpisodeActionEntityToUpdate); $episodeActionEntity->setId($idEpisodeActionEntityToUpdate);
return $this->episodeActionWriter->update($episodeActionEntity); return $this->episodeActionWriter->update($episodeActionEntity);
} }
/**
* @param EpisodeAction $episodeAction
* @param string $userId
*
* @return EpisodeActionEntity
*/
private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string $userId): EpisodeActionEntity
{
$episodeActionEntity = new EpisodeActionEntity();
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
$episodeActionEntity->setAction($episodeAction->getAction());
$episodeActionEntity->setPosition($episodeAction->getPosition());
$episodeActionEntity->setStarted($episodeAction->getStarted());
$episodeActionEntity->setTotal($episodeAction->getTotal());
$episodeActionEntity->setTimestamp($this->convertTimestampToDbDateTimeString($episodeAction->getTimestamp()));
$episodeActionEntity->setUserId($userId);
return $episodeActionEntity;
}
} }

View File

@ -15,7 +15,7 @@ class AppTest extends TestCase {
private $container; private $container;
public function setUp() { public function setUp() : void {
parent::setUp(); parent::setUp();
$app = new App('gpoddersync'); $app = new App('gpoddersync');
$this->container = $app->getContainer(); $this->container = $app->getContainer();