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-08-23 11:30:18 +00:00
|
|
|
* @param string $data
|
2021-08-22 20:45:34 +00:00
|
|
|
*
|
2021-08-23 11:30:18 +00:00
|
|
|
* @return EpisodeActionEntity[]
|
2021-08-22 20:45:34 +00:00
|
|
|
*/
|
2021-08-24 21:19:21 +00:00
|
|
|
public function saveEpisodeAction(string $data, string $userId): array
|
2021-08-22 20:45:34 +00:00
|
|
|
{
|
2021-08-23 11:30:18 +00:00
|
|
|
$episodeActionEntities = [];
|
2021-08-22 23:11:16 +00:00
|
|
|
|
|
|
|
$episodeActions = $this->episodeActionReader->fromString($data);
|
|
|
|
|
|
|
|
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-08-23 11:30:18 +00:00
|
|
|
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
|
2021-08-22 23:11:16 +00:00
|
|
|
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
|
2021-08-27 10:51:29 +00:00
|
|
|
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
|
2021-08-22 23:11:16 +00:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
2021-08-27 10:51:29 +00:00
|
|
|
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
|
2021-08-22 23:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 11:30:18 +00:00
|
|
|
return $episodeActionEntities;
|
2021-08-22 20:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 EpisodeActionEntity $episodeActionEntity
|
|
|
|
*
|
|
|
|
* @return EpisodeActionEntity
|
|
|
|
*/
|
|
|
|
private function updateEpisodeAction(
|
|
|
|
EpisodeActionEntity $episodeActionEntity,
|
|
|
|
string $userId
|
|
|
|
): EpisodeActionEntity
|
|
|
|
{
|
2021-08-24 21:19:21 +00:00
|
|
|
$idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisodeIdentifier(
|
2021-08-27 10:51:29 +00:00
|
|
|
$episodeActionEntity->getEpisode(),
|
2021-08-22 20:45:34 +00:00
|
|
|
$userId
|
|
|
|
)->getId();
|
|
|
|
$episodeActionEntity->setId($idEpisodeActionEntityToUpdate);
|
|
|
|
|
|
|
|
return $this->episodeActionWriter->update($episodeActionEntity);
|
|
|
|
}
|
2021-08-27 10:51:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EpisodeAction $episodeAction
|
|
|
|
* @param string $userId
|
|
|
|
*
|
|
|
|
* @return EpisodeActionEntity
|
|
|
|
*/
|
|
|
|
private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string $userId): EpisodeActionEntity
|
|
|
|
{
|
|
|
|
$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);
|
|
|
|
|
|
|
|
return $episodeActionEntity;
|
|
|
|
}
|
2021-08-22 20:45:34 +00:00
|
|
|
}
|