From 20a647906a3b07be306a58158f49b817828dc455 Mon Sep 17 00:00:00 2001 From: thrillfall Date: Fri, 27 Aug 2021 12:51:29 +0200 Subject: [PATCH] do not pass same data twice --- lib/Core/EpisodeAction/EpisodeActionSaver.php | 41 ++++++++++++------- tests/Integration/AppTest.php | 2 +- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/Core/EpisodeAction/EpisodeActionSaver.php b/lib/Core/EpisodeAction/EpisodeActionSaver.php index 30026af..b96cb20 100644 --- a/lib/Core/EpisodeAction/EpisodeActionSaver.php +++ b/lib/Core/EpisodeAction/EpisodeActionSaver.php @@ -40,23 +40,15 @@ class EpisodeActionSaver $episodeActions = $this->episodeActionReader->fromString($data); foreach ($episodeActions as $episodeAction) { - $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); + $episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId); - try { + try { $episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity); } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { - $episodeActionEntities[] = $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId); + $episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId); } catch (Exception $exception) { 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 * * @return EpisodeActionEntity */ private function updateEpisodeAction( - EpisodeAction $episodeAction, EpisodeActionEntity $episodeActionEntity, string $userId ): EpisodeActionEntity { $idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode( - $episodeAction->getEpisode(), + $episodeActionEntity->getEpisode(), $userId )->getId(); $episodeActionEntity->setId($idEpisodeActionEntityToUpdate); 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; + } } diff --git a/tests/Integration/AppTest.php b/tests/Integration/AppTest.php index 2477795..221ee70 100644 --- a/tests/Integration/AppTest.php +++ b/tests/Integration/AppTest.php @@ -15,7 +15,7 @@ class AppTest extends TestCase { private $container; - public function setUp() { + public function setUp() : void { parent::setUp(); $app = new App('gpoddersync'); $this->container = $app->getContainer();