2021-08-22 20:45:34 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\EpisodeAction;
|
|
|
|
|
2021-10-07 12:33:16 +00:00
|
|
|
use DateTime;
|
2021-08-22 20:45:34 +00:00
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
|
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
|
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
|
|
|
|
use OCP\DB\Exception;
|
|
|
|
|
|
|
|
class EpisodeActionSaver
|
|
|
|
{
|
|
|
|
|
2023-02-24 15:48:16 +00:00
|
|
|
private EpisodeActionRepository $episodeActionRepository;
|
|
|
|
private EpisodeActionWriter $episodeActionWriter;
|
|
|
|
private EpisodeActionReader $episodeActionReader;
|
|
|
|
|
|
|
|
private const DATETIME_FORMAT = 'Y-m-d\TH:i:s';
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
EpisodeActionRepository $episodeActionRepository,
|
|
|
|
EpisodeActionWriter $episodeActionWriter,
|
|
|
|
EpisodeActionReader $episodeActionReader
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
|
|
|
$this->episodeActionWriter = $episodeActionWriter;
|
|
|
|
$this->episodeActionReader = $episodeActionReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveEpisodeActions(array $episodeActionsArray, string $userId): array
|
|
|
|
{
|
|
|
|
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
|
|
|
|
|
|
|
|
$episodeActionEntities = [];
|
2021-10-06 17:11:50 +00:00
|
|
|
|
2021-08-22 23:11:16 +00:00
|
|
|
foreach ($episodeActions as $episodeAction) {
|
2023-02-24 15:48:16 +00:00
|
|
|
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
|
2021-08-22 20:45:34 +00:00
|
|
|
|
2023-02-24 15:48:16 +00:00
|
|
|
try {
|
2021-10-06 17:11:50 +00:00
|
|
|
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
|
2021-08-22 23:11:16 +00:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
2023-02-24 15:48:16 +00:00
|
|
|
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
|
|
|
|
}
|
2021-08-22 23:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-24 15:48:16 +00:00
|
|
|
return $episodeActionEntities;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convertTimestampToUnixEpoch(string $timestamp): string
|
|
|
|
{
|
|
|
|
return DateTime::createFromFormat(self::DATETIME_FORMAT, $timestamp)
|
|
|
|
->format("U");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function updateEpisodeAction(
|
|
|
|
EpisodeActionEntity $episodeActionEntity,
|
|
|
|
string $userId
|
|
|
|
): EpisodeActionEntity
|
|
|
|
{
|
|
|
|
$episodeActionToUpdate = $this->findEpisodeActionToUpdate($episodeActionEntity, $userId);
|
|
|
|
|
|
|
|
$episodeActionEntity->setId($episodeActionToUpdate->getId());
|
|
|
|
|
|
|
|
$this->ensureGuidDoesNotGetNulledWithOldData($episodeActionToUpdate, $episodeActionEntity);
|
|
|
|
|
2023-02-24 19:48:53 +00:00
|
|
|
try {
|
|
|
|
return $this->episodeActionWriter->update($episodeActionEntity);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
|
|
|
$this->deleteConflictingEpisodeAction($episodeActionEntity, $userId);
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 15:48:16 +00:00
|
|
|
return $this->episodeActionWriter->update($episodeActionEntity);
|
2023-02-24 19:48:53 +00:00
|
|
|
|
2023-02-24 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void
|
|
|
|
{
|
|
|
|
$existingGuid = $episodeActionToUpdate->getGuid();
|
|
|
|
if ($existingGuid !== null && $episodeActionEntity->getGuid() === null) {
|
|
|
|
$episodeActionEntity->setGuid($existingGuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string $userId): EpisodeActionEntity
|
|
|
|
{
|
|
|
|
$episodeActionEntity = new EpisodeActionEntity();
|
|
|
|
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
|
|
|
|
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
|
|
|
|
$episodeActionEntity->setGuid($episodeAction->getGuid());
|
|
|
|
$episodeActionEntity->setAction($episodeAction->getAction());
|
|
|
|
$episodeActionEntity->setPosition($episodeAction->getPosition());
|
|
|
|
$episodeActionEntity->setStarted($episodeAction->getStarted());
|
|
|
|
$episodeActionEntity->setTotal($episodeAction->getTotal());
|
|
|
|
$episodeActionEntity->setTimestampEpoch($this->convertTimestampToUnixEpoch($episodeAction->getTimestamp()));
|
|
|
|
$episodeActionEntity->setUserId($userId);
|
|
|
|
|
|
|
|
return $episodeActionEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findEpisodeActionToUpdate(EpisodeActionEntity $episodeActionEntity, string $userId): ?EpisodeAction
|
|
|
|
{
|
|
|
|
$episodeAction = null;
|
|
|
|
if ($episodeActionEntity->getGuid() !== null) {
|
|
|
|
$episodeAction = $this->episodeActionRepository->findByGuid(
|
|
|
|
$episodeActionEntity->getGuid(),
|
|
|
|
$userId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($episodeAction === null) {
|
|
|
|
$episodeAction = $this->episodeActionRepository->findByEpisodeUrl(
|
|
|
|
$episodeActionEntity->getEpisode(),
|
|
|
|
$userId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $episodeAction;
|
|
|
|
}
|
2023-02-24 19:48:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EpisodeActionEntity $episodeActionEntity
|
|
|
|
* @param string $userId
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function deleteConflictingEpisodeAction(EpisodeActionEntity $episodeActionEntity, string $userId): void
|
|
|
|
{
|
2024-01-08 20:54:07 +00:00
|
|
|
$collidingEpisodeActionId = $this->episodeActionRepository->findByEpisodeUrl($episodeActionEntity->getEpisode(), $userId)->getId();
|
2023-02-24 19:48:53 +00:00
|
|
|
if ($collidingEpisodeActionId !== $episodeActionEntity->getId()) {
|
2024-01-08 20:54:07 +00:00
|
|
|
$this->episodeActionRepository->deleteEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
|
2023-02-24 19:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-22 20:45:34 +00:00
|
|
|
}
|