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

99 lines
3.3 KiB
PHP
Raw Normal View History

<?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;
}
/**
* @param $data
*
* @return array
*/
public function saveEpisodeAction($data, string $userId): array
{
$response = array();
$episodeActions = $this->episodeActionReader->fromString($data);
foreach ($episodeActions as $episodeAction) {
$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 {
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;
}
/**
* @param string $timestamp
*
* @return string
*/
private function convertTimestampToDbDateTimeString(string $timestamp): string
{
return \DateTime::createFromFormat('D F d H:i:s T Y', $timestamp)
->setTimezone(new DateTimeZone('UTC'))
->format("Y-m-d\TH:i:s");
}
/**
* @param EpisodeAction $episodeAction
* @param EpisodeActionEntity $episodeActionEntity
*
* @return EpisodeActionEntity
*/
private function updateEpisodeAction(
EpisodeAction $episodeAction,
EpisodeActionEntity $episodeActionEntity,
string $userId
): EpisodeActionEntity
{
$idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode(
$episodeAction->getEpisode(),
$userId
)->getId();
$episodeActionEntity->setId($idEpisodeActionEntityToUpdate);
return $this->episodeActionWriter->update($episodeActionEntity);
}
}