Enable processing of multiple EpisodeActions
EpisodeAction/create now can deal with multiple EpisodeActions inside a single EpisodeAction string, they will be processed one after another. For this EpisodeActionReader->fromString() now returns an array of EpisodeActions.
This commit is contained in:
parent
d793b59637
commit
48b78669e6
|
@ -4,22 +4,44 @@ declare(strict_types=1);
|
||||||
namespace OCA\GPodderSync\Core\EpisodeAction;
|
namespace OCA\GPodderSync\Core\EpisodeAction;
|
||||||
|
|
||||||
class EpisodeActionReader {
|
class EpisodeActionReader {
|
||||||
public function fromString(string $episodeActionString): EpisodeAction {
|
/**
|
||||||
preg_match(
|
* Reads and parses an EpisodeActions string and returns an EpisodeAction array
|
||||||
'/\[EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
|
*
|
||||||
$episodeActionString,
|
* @param string $episodeActionString
|
||||||
$matches
|
* @return array
|
||||||
);
|
*/
|
||||||
|
public function fromString(string $episodeActionString): array {
|
||||||
|
|
||||||
|
$episodeActions = array();
|
||||||
|
|
||||||
return new EpisodeAction(
|
$seek = 0;
|
||||||
$matches["podcast"],
|
|
||||||
$matches["episode"],
|
while (strpos($episodeActionString, 'EpisodeAction{', $seek) >= $seek) {
|
||||||
$matches["action"],
|
if (($seek = strpos($episodeActionString, 'EpisodeAction{', $seek)) === false) {
|
||||||
$matches["timestamp"],
|
continue;
|
||||||
(int)$matches["started"],
|
}
|
||||||
(int)$matches["position"],
|
|
||||||
(int)$matches["total"],
|
preg_match(
|
||||||
);
|
'/EpisodeAction{(podcast=\')(?<podcast>.*?)(\', episode=\')(?<episode>.*?)(\', action=)(?<action>.*?)(, timestamp=)(?<timestamp>.*?)(, started=)(?<started>.*?)(, position=)(?<position>.*?)(, total=)(?<total>.*?)}]*/',
|
||||||
|
substr($episodeActionString, $seek),
|
||||||
|
$matches
|
||||||
|
);
|
||||||
|
|
||||||
|
// change for next iteration
|
||||||
|
$seek++;
|
||||||
|
|
||||||
|
array_push($episodeActions, new EpisodeAction(
|
||||||
|
$matches["podcast"],
|
||||||
|
$matches["episode"],
|
||||||
|
$matches["action"],
|
||||||
|
$matches["timestamp"],
|
||||||
|
(int)$matches["started"],
|
||||||
|
(int)$matches["position"],
|
||||||
|
(int)$matches["total"],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $episodeActions;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,30 +31,36 @@ class EpisodeActionSaver
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $data
|
||||||
*
|
*
|
||||||
* @return EpisodeActionEntity
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function saveEpisodeAction($data, string $userId): EpisodeActionEntity
|
public function saveEpisodeAction($data, string $userId): array
|
||||||
{
|
{
|
||||||
$episodeAction = $this->episodeActionReader->fromString($data);
|
$response = array();
|
||||||
$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);
|
|
||||||
|
|
||||||
try {
|
$episodeActions = $this->episodeActionReader->fromString($data);
|
||||||
return $this->episodeActionWriter->save($episodeActionEntity);
|
|
||||||
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
|
foreach ($episodeActions as $episodeAction) {
|
||||||
return $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId);
|
$episodeActionEntity = new EpisodeActionEntity();
|
||||||
} catch (Exception $exception) {
|
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
|
||||||
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
|
||||||
return $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId);
|
$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 {
|
||||||
|
array_push($response, $this->episodeActionWriter->save($episodeActionEntity));
|
||||||
|
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
|
||||||
|
array_push($response, $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId));
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||||
|
array_push($response, $this->updateEpisodeAction($episodeAction, $episodeActionEntity, $userId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,7 +9,8 @@ use Test\TestCase;
|
||||||
class EpisodeActionReaderTest extends TestCase {
|
class EpisodeActionReaderTest extends TestCase {
|
||||||
public function testCreateFromString(): void {
|
public function testCreateFromString(): void {
|
||||||
$reader = new EpisodeActionReader();
|
$reader = new EpisodeActionReader();
|
||||||
$episodeAction = $reader->fromString('[EpisodeAction{podcast=\'https://feeds.simplecast.com/wEl4UUJZ\', episode=\'https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ\', action=PLAY, timestamp=Tue May 18 23:45:11 GMT+02:00 2021, started=31, position=36, total=2474}]');
|
$episodeActions = $reader->fromString('[EpisodeAction{podcast=\'https://feeds.simplecast.com/wEl4UUJZ\', episode=\'https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ\', action=PLAY, timestamp=Tue May 18 23:45:11 GMT+02:00 2021, started=31, position=36, total=2474}]');
|
||||||
|
$episodeAction = $episodeActions[0];
|
||||||
$this->assertSame("https://feeds.simplecast.com/wEl4UUJZ", $episodeAction->getPodcast());
|
$this->assertSame("https://feeds.simplecast.com/wEl4UUJZ", $episodeAction->getPodcast());
|
||||||
$this->assertSame("https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", $episodeAction->getEpisode());
|
$this->assertSame("https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", $episodeAction->getEpisode());
|
||||||
$this->assertSame("PLAY", $episodeAction->getAction());
|
$this->assertSame("PLAY", $episodeAction->getAction());
|
||||||
|
@ -20,4 +21,33 @@ class EpisodeActionReaderTest extends TestCase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCreateFromMultipleEpisodesString(): void {
|
||||||
|
$reader = new EpisodeActionReader();
|
||||||
|
$episodeActions = $reader->fromString('[EpisodeAction{podcast=\'https://example.com/feed.xml\', episode=\'https://example.com/episode1.mp3\', action=PLAY, timestamp=Tue May 18 23:45:11 GMT+02:00 2021, started=31, position=36, total=2474},EpisodeAction{podcast=\'https://example.com/feed.xml\', episode=\'https://example.com/episode2.mp3\', action=DOWNLOAD, timestamp=Tue May 18 23:46:42 GMT+02:00 2021, started=31, position=36, total=2474},EpisodeAction{podcast=\'https://example.org/feed.xml\', episode=\'https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ\', action=PLAY, timestamp=Tue May 18 23:45:14 GMT+02:00 2021, started=0, position=211, total=3121}]');
|
||||||
|
|
||||||
|
$this->assertSame("https://example.com/feed.xml", $episodeActions[0]->getPodcast());
|
||||||
|
$this->assertSame("https://example.com/episode1.mp3", $episodeActions[0]->getEpisode());
|
||||||
|
$this->assertSame("PLAY", $episodeActions[0]->getAction());
|
||||||
|
$this->assertSame("Tue May 18 23:45:11 GMT+02:00 2021", $episodeActions[0]->getTimestamp());
|
||||||
|
$this->assertSame(31, $episodeActions[0]->getStarted());
|
||||||
|
$this->assertSame(36, $episodeActions[0]->getPosition());
|
||||||
|
$this->assertSame(2474, $episodeActions[0]->getTotal());
|
||||||
|
|
||||||
|
$this->assertSame("https://example.com/feed.xml", $episodeActions[1]->getPodcast());
|
||||||
|
$this->assertSame("https://example.com/episode2.mp3", $episodeActions[1]->getEpisode());
|
||||||
|
$this->assertSame("DOWNLOAD", $episodeActions[1]->getAction());
|
||||||
|
$this->assertSame("Tue May 18 23:46:42 GMT+02:00 2021", $episodeActions[1]->getTimestamp());
|
||||||
|
$this->assertSame(31, $episodeActions[1]->getStarted());
|
||||||
|
$this->assertSame(36, $episodeActions[1]->getPosition());
|
||||||
|
$this->assertSame(2474, $episodeActions[1]->getTotal());
|
||||||
|
|
||||||
|
$this->assertSame("https://example.org/feed.xml", $episodeActions[2]->getPodcast());
|
||||||
|
$this->assertSame("https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", $episodeActions[2]->getEpisode());
|
||||||
|
$this->assertSame("PLAY", $episodeActions[2]->getAction());
|
||||||
|
$this->assertSame("Tue May 18 23:45:14 GMT+02:00 2021", $episodeActions[2]->getTimestamp());
|
||||||
|
$this->assertSame(0, $episodeActions[2]->getStarted());
|
||||||
|
$this->assertSame(211, $episodeActions[2]->getPosition());
|
||||||
|
$this->assertSame(3121, $episodeActions[2]->getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue