diff --git a/lib/Core/EpisodeAction/EpisodeActionReader.php b/lib/Core/EpisodeAction/EpisodeActionReader.php index 87aee49..0ab16fa 100644 --- a/lib/Core/EpisodeAction/EpisodeActionReader.php +++ b/lib/Core/EpisodeAction/EpisodeActionReader.php @@ -4,33 +4,28 @@ declare(strict_types=1); namespace OCA\GPodderSync\Core\EpisodeAction; class EpisodeActionReader { + + const EPISODEACTION_IDENTIFIER = 'EpisodeAction{'; + /** - * Reads and parses an EpisodeActions string and returns an EpisodeAction array - * * @param string $episodeActionString - * @return array + * @return EpisodeAction[] */ public function fromString(string $episodeActionString): array { - $episodeActions = array(); + $episodeActions = []; - $seek = 0; + $episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionString); - while (strpos($episodeActionString, 'EpisodeAction{', $seek) >= $seek) { - if (($seek = strpos($episodeActionString, 'EpisodeAction{', $seek)) === false) { - continue; - } + for($i = 1; $i < count($episodeActionStrings); $i++) { preg_match( '/EpisodeAction{(podcast=\')(?.*?)(\', episode=\')(?.*?)(\', action=)(?.*?)(, timestamp=)(?.*?)(, started=)(?.*?)(, position=)(?.*?)(, total=)(?.*?)}]*/', - substr($episodeActionString, $seek), + self::EPISODEACTION_IDENTIFIER . $episodeActionStrings[$i], $matches ); - // change for next iteration - $seek++; - - array_push($episodeActions, new EpisodeAction( + $episodeActions[] = new EpisodeAction( $matches["podcast"], $matches["episode"], $matches["action"], @@ -38,7 +33,7 @@ class EpisodeActionReader { (int)$matches["started"], (int)$matches["position"], (int)$matches["total"], - )); + ); } return $episodeActions; diff --git a/lib/Core/EpisodeAction/EpisodeActionSaver.php b/lib/Core/EpisodeAction/EpisodeActionSaver.php index 233e0a0..30026af 100644 --- a/lib/Core/EpisodeAction/EpisodeActionSaver.php +++ b/lib/Core/EpisodeAction/EpisodeActionSaver.php @@ -29,13 +29,13 @@ class EpisodeActionSaver } /** - * @param $data + * @param string $data * - * @return array + * @return EpisodeActionEntity[] */ public function saveEpisodeAction($data, string $userId): array { - $response = array(); + $episodeActionEntities = []; $episodeActions = $this->episodeActionReader->fromString($data); @@ -51,16 +51,16 @@ class EpisodeActionSaver $episodeActionEntity->setUserId($userId); try { - array_push($response, $this->episodeActionWriter->save($episodeActionEntity)); + $episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity); } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { - array_push($response, $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId)); + $episodeActionEntities[] = $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId); } catch (Exception $exception) { if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { - array_push($response, $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId)); + $episodeActionEntities[] = $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId); } } } - return $response; + return $episodeActionEntities; } /**