Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Episode upload as array #30

Closed
wants to merge 9 commits into from
13 changes: 8 additions & 5 deletions lib/Core/EpisodeAction/EpisodeActionSaver.php
Expand Up @@ -33,25 +33,28 @@ public function __construct(
/**
* @param array $episodeActionsArray
*
* @return void
* @return EpisodeActionEntity[]
*/
public function saveEpisodeActions($episodeActionsArray, string $userId): void
public function saveEpisodeActions($episodeActionsArray, string $userId): array
{
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);

$episodeActionEntities = [];

foreach ($episodeActions as $episodeAction) {
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);

try {
$this->episodeActionWriter->save($episodeActionEntity);
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
$this->updateEpisodeAction($episodeActionEntity, $userId);
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->updateEpisodeAction($episodeActionEntity, $userId);
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
}
}
}
return $episodeActionEntities;
JonOfUs marked this conversation as resolved.
Show resolved Hide resolved
}

private function convertTimestampToUnixEpoch(string $timestamp): string
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/EpisodeActionRepositoryTest.php
Expand Up @@ -31,8 +31,8 @@ public function testTimestampOutputIsUTCHumandReadable() : void
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");

$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
self::USER_ID_0
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

self::assertSame(1629676736, $savedEpisodeActionEntity->getTimestampEpoch());
Expand Down
Expand Up @@ -32,12 +32,12 @@ public function testUpdateWithoutGuidDoesNotNullGuid() : void
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");

$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

$savedEpisodeActionEntityWithoutGuidFromOldDevice = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/EpisodeActionSaverGuidMigrationTest.php
Expand Up @@ -32,12 +32,12 @@ public function testCreateEpisodeActionWithoutGuidThenCreateAgainWithGuid() : vo
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");

$savedEpisodeActionEntityWithoutGuid = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

$savedEpisodeActionEntityWithGuid = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

Expand All @@ -53,13 +53,13 @@ public function testCreateEpisodeActionWithGuidThenCreateAgainWithGuidButDiffere
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");

$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

$savedEpisodeActionEntityWithDifferentEpisodeUrl = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}_different', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
self::USER_ID_0
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl . "_different", "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

self::assertSame($savedEpisodeActionEntity->getId(), $savedEpisodeActionEntityWithDifferentEpisodeUrl->getId());
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Migration/TimestampMigrationTest.php
Expand Up @@ -64,7 +64,7 @@ public function testTimestampConversionRepairStep()
$episodeActionEntity->setPosition(5);
$episodeActionEntity->setStarted(0);
$episodeActionEntity->setTotal(123);
$episodeActionEntity->setTimestamp("Sun Aug 22 23:58:56 GMT+00:00 2021");
$episodeActionEntity->setTimestamp("2021-08-22T23:58:56");
$episodeActionEntity->setUserId(self::ADMIN);
$guid = uniqid("self::TEST_GUID_1234");
$episodeActionEntity->setGuid($guid);
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/Core/EpisodeAction/EpisodeActionReaderTest.php
Expand Up @@ -9,12 +9,12 @@
class EpisodeActionReaderTest extends TestCase {
public function testCreateFromArray(): void {
$reader = new EpisodeActionReader();
$episodeActions = $reader->fromArray([["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "action" => "PLAY", "timestamp" => "Sun Oct 03 14:03:17 GMT+02:00 2021", "started" => 0, "position" => 50, "total"=> 3422]]);
$episodeActions = $reader->fromArray([["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "action" => "PLAY", "timestamp" => "2021-10-03T12:03:17", "started" => 0, "position" => 50, "total"=> 3422]]);

$this->assertSame("https://example.org/feed.xml", $episodeActions[0]->getPodcast());
$this->assertSame("https://example.org/episode1.mp3", $episodeActions[0]->getEpisode());
$this->assertSame("PLAY", $episodeActions[0]->getAction());
$this->assertSame("Sun Oct 03 14:03:17 GMT+02:00 2021", $episodeActions[0]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[0]->getTimestamp());
$this->assertSame(0, $episodeActions[0]->getStarted());
$this->assertSame(50, $episodeActions[0]->getPosition());
$this->assertSame(3422, $episodeActions[0]->getTotal());
Expand All @@ -23,16 +23,16 @@ public function testCreateFromArray(): void {
public function testCreateFromMultipleEpisodesArray(): void {
$reader = new EpisodeActionReader();
$episodeActions = $reader->fromArray([
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "guid" => "episode1", "action" => "PLAY", "timestamp" => "Sun Oct 03 14:03:17 GMT+02:00 2021", "started" => 0, "position" => 50, "total"=> 3422],
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "DOWNLOAD", "timestamp" => "Sat Oct 02 11:06:28 GMT+02:00 2021", "started" => -1, "position" => -1, "total"=> -1],
["podcast" => "https://example.com/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", "guid" => "EPISODE-001-EXAMPLE-COM", "action" => "PLAY", "timestamp" => "Sun Oct 03 14:07:15 GMT+02:00 2021", "started" => 50, "position" => 221, "total"=> 450]
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "guid" => "episode1", "action" => "PLAY", "timestamp" => "2021-10-03T12:03:17", "started" => 0, "position" => 50, "total"=> 3422],
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "download", "timestamp" => "2021-10-03T12:03:17"],
["podcast" => "https://example.com/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", "guid" => "EPISODE-001-EXAMPLE-COM", "action" => "PLAY", "timestamp" => "2021-10-03T12:03:17", "started" => 50, "position" => 221, "total"=> 450]
]);

$this->assertSame("https://example.org/feed.xml", $episodeActions[0]->getPodcast());
$this->assertSame("https://example.org/episode1.mp3", $episodeActions[0]->getEpisode());
$this->assertSame("episode1", $episodeActions[0]->getGuid());
$this->assertSame("PLAY", $episodeActions[0]->getAction());
$this->assertSame("Sun Oct 03 14:03:17 GMT+02:00 2021", $episodeActions[0]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[0]->getTimestamp());
$this->assertSame(0, $episodeActions[0]->getStarted());
$this->assertSame(50, $episodeActions[0]->getPosition());
$this->assertSame(3422, $episodeActions[0]->getTotal());
Expand All @@ -41,7 +41,7 @@ public function testCreateFromMultipleEpisodesArray(): void {
$this->assertSame("https://example.org/episode2.mp3", $episodeActions[1]->getEpisode());
$this->assertSame("episode2", $episodeActions[1]->getGuid());
$this->assertSame("DOWNLOAD", $episodeActions[1]->getAction());
$this->assertSame("Sat Oct 02 11:06:28 GMT+02:00 2021", $episodeActions[1]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[1]->getTimestamp());
$this->assertSame(-1, $episodeActions[1]->getStarted());
$this->assertSame(-1, $episodeActions[1]->getPosition());
$this->assertSame(-1, $episodeActions[1]->getTotal());
Expand All @@ -50,7 +50,7 @@ public function testCreateFromMultipleEpisodesArray(): void {
$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("EPISODE-001-EXAMPLE-COM", $episodeActions[2]->getGuid());
$this->assertSame("PLAY", $episodeActions[2]->getAction());
$this->assertSame("Sun Oct 03 14:07:15 GMT+02:00 2021", $episodeActions[2]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[2]->getTimestamp());
$this->assertSame(50, $episodeActions[2]->getStarted());
$this->assertSame(221, $episodeActions[2]->getPosition());
$this->assertSame(450, $episodeActions[2]->getTotal());
Expand Down

This file was deleted.