nextcloud-gpodder/lib/Core/EpisodeAction/EpisodeActionSaver.php

120 lines
4.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Core\EpisodeAction;
use DateTime;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
use OCP\DB\Exception;
class EpisodeActionSaver
{
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;
}
/**
* @param array $episodeActionsArray
* @param string $userId
2021-10-06 17:11:50 +00:00
* @return EpisodeActionEntity[]
*/
public function saveEpisodeActions(array $episodeActionsArray, string $userId): array
{
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
2021-10-06 17:11:50 +00:00
$episodeActionEntities = [];
foreach ($episodeActions as $episodeAction) {
2021-08-27 10:51:29 +00:00
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
2021-08-27 10:51:29 +00:00
try {
2021-10-06 17:11:50 +00:00
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
}
}
}
2021-10-06 17:11:50 +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
{
$identifier = $episodeActionEntity->getGuid() ?? $episodeActionEntity->getEpisode();
$episodeActionToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
$identifier,
$userId
);
if ($episodeActionToUpdate === null && $episodeActionEntity->getGuid() !== null) {
$episodeActionToUpdate = $this->getOldEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
}
$episodeActionEntity->setId($episodeActionToUpdate->getId());
$this->ensureGuidDoesNotGetNulledWithOldData($episodeActionToUpdate, $episodeActionEntity);
return $this->episodeActionWriter->update($episodeActionEntity);
}
2021-08-27 10:51:29 +00:00
private function getOldEpisodeActionByEpisodeUrl(string $episodeUrl, string $userId): ?EpisodeAction
{
return $this->episodeActionRepository->findByEpisodeIdentifier(
$episodeUrl,
$userId
);
}
private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void
{
$existingGuid = $episodeActionToUpdate->getGuid();
if ($existingGuid !== null && $episodeActionEntity->getGuid() === null) {
$episodeActionEntity->setGuid($existingGuid);
}
}
2021-08-27 10:51:29 +00:00
private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string $userId): EpisodeActionEntity
{
$episodeActionEntity = new EpisodeActionEntity();
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
$episodeActionEntity->setGuid($episodeAction->getGuid());
2021-08-27 10:51:29 +00:00
$episodeActionEntity->setAction($episodeAction->getAction());
$episodeActionEntity->setPosition($episodeAction->getPosition());
$episodeActionEntity->setStarted($episodeAction->getStarted());
$episodeActionEntity->setTotal($episodeAction->getTotal());
$episodeActionEntity->setTimestampEpoch($this->convertTimestampToUnixEpoch($episodeAction->getTimestamp()));
2021-08-27 10:51:29 +00:00
$episodeActionEntity->setUserId($userId);
return $episodeActionEntity;
}
}