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;
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=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<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;

View File

@ -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;
}
/**