Increase code quality, use explode instead of strpos/substr

Makes code phpdoc conform and changes to a better readable EpisodeActions parsing.
This commit is contained in:
Jonathan Flueren 2021-08-23 13:30:18 +02:00 committed by thrillfall
parent 48b78669e6
commit cd17ad5079
2 changed files with 17 additions and 22 deletions

View File

@ -4,33 +4,28 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\EpisodeAction; namespace OCA\GPodderSync\Core\EpisodeAction;
class EpisodeActionReader { class EpisodeActionReader {
const EPISODEACTION_IDENTIFIER = 'EpisodeAction{';
/** /**
* Reads and parses an EpisodeActions string and returns an EpisodeAction array
*
* @param string $episodeActionString * @param string $episodeActionString
* @return array * @return EpisodeAction[]
*/ */
public function fromString(string $episodeActionString): array { public function fromString(string $episodeActionString): array {
$episodeActions = array(); $episodeActions = [];
$seek = 0; $episodeActionStrings = explode(self::EPISODEACTION_IDENTIFIER, $episodeActionString);
while (strpos($episodeActionString, 'EpisodeAction{', $seek) >= $seek) { for($i = 1; $i < count($episodeActionStrings); $i++) {
if (($seek = strpos($episodeActionString, 'EpisodeAction{', $seek)) === false) {
continue;
}
preg_match( preg_match(
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/', '/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
substr($episodeActionString, $seek), self::EPISODEACTION_IDENTIFIER . $episodeActionStrings[$i],
$matches $matches
); );
// change for next iteration $episodeActions[] = new EpisodeAction(
$seek++;
array_push($episodeActions, new EpisodeAction(
$matches["podcast"], $matches["podcast"],
$matches["episode"], $matches["episode"],
$matches["action"], $matches["action"],
@ -38,7 +33,7 @@ class EpisodeActionReader {
(int)$matches["started"], (int)$matches["started"],
(int)$matches["position"], (int)$matches["position"],
(int)$matches["total"], (int)$matches["total"],
)); );
} }
return $episodeActions; return $episodeActions;

View File

@ -29,13 +29,13 @@ class EpisodeActionSaver
} }
/** /**
* @param $data * @param string $data
* *
* @return array * @return EpisodeActionEntity[]
*/ */
public function saveEpisodeAction($data, string $userId): array public function saveEpisodeAction($data, string $userId): array
{ {
$response = array(); $episodeActionEntities = [];
$episodeActions = $this->episodeActionReader->fromString($data); $episodeActions = $this->episodeActionReader->fromString($data);
@ -51,16 +51,16 @@ class EpisodeActionSaver
$episodeActionEntity->setUserId($userId); $episodeActionEntity->setUserId($userId);
try { try {
array_push($response, $this->episodeActionWriter->save($episodeActionEntity)); $episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
array_push($response, $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId)); $episodeActionEntities[] = $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId);
} catch (Exception $exception) { } catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { 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;
} }
/** /**