2021-08-22 20:45:34 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\EpisodeAction;
|
|
|
|
|
|
|
|
use DateTimeZone;
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
EpisodeActionRepository $episodeActionRepository,
|
|
|
|
EpisodeActionWriter $episodeActionWriter,
|
|
|
|
EpisodeActionReader $episodeActionReader
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
|
|
|
$this->episodeActionWriter = $episodeActionWriter;
|
|
|
|
$this->episodeActionReader = $episodeActionReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-05 12:57:49 +00:00
|
|
|
* @param array $episodeActionsArray
|
2021-08-22 20:45:34 +00:00
|
|
|
*
|
2021-10-05 12:57:49 +00:00
|
|
|
* @return void
|
2021-08-22 20:45:34 +00:00
|
|
|
*/
|
2021-10-05 12:57:49 +00:00
|
|
|
public function saveEpisodeActions($episodeActionsArray, string $userId): void
|
2021-08-22 20:45:34 +00:00
|
|
|
{
|
2021-10-05 12:57:49 +00:00
|
|
|
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
|
2021-08-22 23:11:16 +00:00
|
|
|
|
|
|
|
foreach ($episodeActions as $episodeAction) {
|
2021-08-27 10:51:29 +00:00
|
|
|
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
|
2021-08-22 20:45:34 +00:00
|
|
|
|
2021-08-27 10:51:29 +00:00
|
|
|
try {
|
2021-10-05 12:57:49 +00:00
|
|
|
$this->episodeActionWriter->save($episodeActionEntity);
|
2021-08-22 23:11:16 +00:00
|
|
|
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
|
2021-10-05 12:57:49 +00:00
|
|
|
$this->updateEpisodeAction($episodeActionEntity, $userId);
|
2021-08-22 23:11:16 +00:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
2021-10-05 12:57:49 +00:00
|
|
|
$this->updateEpisodeAction($episodeActionEntity, $userId);
|
2021-08-22 23:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 20:45:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 18:45:06 +00:00
|
|
|
private function convertTimestampToUnixEpoch(string $timestamp): string
|
2021-08-22 20:45:34 +00:00
|
|
|
{
|
|
|
|
return \DateTime::createFromFormat('D F d H:i:s T Y', $timestamp)
|
|
|
|
->setTimezone(new DateTimeZone('UTC'))
|
2021-10-05 18:45:06 +00:00
|
|
|
->format("U");
|
2021-08-22 20:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function updateEpisodeAction(
|
|
|
|
EpisodeActionEntity $episodeActionEntity,
|
|
|
|
string $userId
|
|
|
|
): EpisodeActionEntity
|
|
|
|
{
|
2021-08-25 17:33:00 +00:00
|
|
|
$identifier = $episodeActionEntity->getGuid() ?? $episodeActionEntity->getEpisode();
|
2021-10-05 18:45:06 +00:00
|
|
|
$episodeActionToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
|
2021-08-25 17:33:00 +00:00
|
|
|
$identifier,
|
2021-08-22 20:45:34 +00:00
|
|
|
$userId
|
2021-08-25 17:33:00 +00:00
|
|
|
);
|
|
|
|
|
2021-10-05 18:45:06 +00:00
|
|
|
if ($episodeActionToUpdate === null && $episodeActionEntity->getGuid() !== null) {
|
|
|
|
$episodeActionToUpdate = $this->getOldEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
|
2021-08-25 17:33:00 +00:00
|
|
|
}
|
2021-08-25 18:25:08 +00:00
|
|
|
|
2021-10-05 18:45:06 +00:00
|
|
|
$episodeActionEntity->setId($episodeActionToUpdate->getId());
|
2021-08-22 20:45:34 +00:00
|
|
|
|
2021-10-05 18:45:06 +00:00
|
|
|
$this->ensureGuidDoesNotGetNulledWithOldData($episodeActionToUpdate, $episodeActionEntity);
|
2021-08-25 20:41:15 +00:00
|
|
|
|
2021-08-22 20:45:34 +00:00
|
|
|
return $this->episodeActionWriter->update($episodeActionEntity);
|
|
|
|
}
|
2021-08-27 10:51:29 +00:00
|
|
|
|
2021-10-05 18:45:06 +00:00
|
|
|
private function getOldEpisodeActionByEpisodeUrl(string $episodeUrl, string $userId): ?EpisodeAction
|
2021-08-25 18:25:08 +00:00
|
|
|
{
|
|
|
|
return $this->episodeActionRepository->findByEpisodeIdentifier(
|
|
|
|
$episodeUrl,
|
|
|
|
$userId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:45:06 +00:00
|
|
|
private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void
|
2021-08-25 20:41:15 +00:00
|
|
|
{
|
2021-10-05 18:45:06 +00:00
|
|
|
$existingGuid = $episodeActionToUpdate->getGuid();
|
2021-08-25 20:41:15 +00:00
|
|
|
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());
|
2021-08-25 17:33:00 +00:00
|
|
|
$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());
|
2021-10-05 18:45:06 +00:00
|
|
|
$episodeActionEntity->setTimestampEpoch($this->convertTimestampToUnixEpoch($episodeAction->getTimestamp()));
|
2021-08-27 10:51:29 +00:00
|
|
|
$episodeActionEntity->setUserId($userId);
|
|
|
|
|
|
|
|
return $episodeActionEntity;
|
|
|
|
}
|
2021-08-22 20:45:34 +00:00
|
|
|
}
|