2021-06-27 11:19:26 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\SubscriptionChange;
|
|
|
|
|
2021-08-21 18:20:24 +00:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2021-06-27 11:19:26 +00:00
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity;
|
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
|
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeWriter;
|
2021-07-11 20:52:30 +00:00
|
|
|
use OCP\DB\Exception;
|
2021-06-27 11:19:26 +00:00
|
|
|
|
2021-08-21 18:20:24 +00:00
|
|
|
class SubscriptionChangeSaver
|
|
|
|
{
|
2021-06-27 11:19:26 +00:00
|
|
|
private SubscriptionChangeRepository $subscriptionChangeRepository;
|
|
|
|
private SubscriptionChangeWriter $subscriptionChangeWriter;
|
|
|
|
private SubscriptionChangeRequestParser $subscriptionChangeRequestParser;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
SubscriptionChangeRequestParser $subscriptionChangeRequestParser,
|
|
|
|
SubscriptionChangeRepository $subscriptionChangeRepository,
|
|
|
|
SubscriptionChangeWriter $subscriptionChangeWriter
|
2021-08-21 18:20:24 +00:00
|
|
|
)
|
|
|
|
{
|
2021-06-27 11:19:26 +00:00
|
|
|
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
|
|
|
|
$this->subscriptionChangeWriter = $subscriptionChangeWriter;
|
|
|
|
$this->subscriptionChangeRequestParser = $subscriptionChangeRequestParser;
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:20:24 +00:00
|
|
|
public function saveSubscriptionChanges(array $urlsSubscribed, array $urlsUnsubscribed, string $userId): void
|
|
|
|
{
|
2021-06-27 11:19:26 +00:00
|
|
|
$subscriptionChanges = $this->subscriptionChangeRequestParser->createSubscriptionChangeList($urlsSubscribed, $urlsUnsubscribed);
|
|
|
|
foreach ($subscriptionChanges as $urlChangedSubscriptionStatus) {
|
|
|
|
$subscriptionChangeEntity = new SubscriptionChangeEntity();
|
|
|
|
$subscriptionChangeEntity->setUrl($urlChangedSubscriptionStatus->getUrl());
|
|
|
|
$subscriptionChangeEntity->setSubscribed($urlChangedSubscriptionStatus->isSubscribed());
|
|
|
|
$subscriptionChangeEntity->setUpdated((new \DateTime())->format("Y-m-d\TH:i:s"));
|
|
|
|
$subscriptionChangeEntity->setUserId($userId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->subscriptionChangeWriter->create($subscriptionChangeEntity);
|
2021-08-21 18:20:24 +00:00
|
|
|
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
|
|
|
|
$this->updateSubscription($subscriptionChangeEntity, $userId);
|
2021-08-22 21:23:02 +00:00
|
|
|
} catch (Exception $exception) {
|
2021-07-11 20:52:30 +00:00
|
|
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
2021-08-21 18:20:24 +00:00
|
|
|
$this->updateSubscription($subscriptionChangeEntity, $userId);
|
2021-07-11 20:52:30 +00:00
|
|
|
}
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:20:24 +00:00
|
|
|
/**
|
|
|
|
* @param SubscriptionChangeEntity $subscriptionChangeEntity
|
|
|
|
* @param string $userId
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function updateSubscription(SubscriptionChangeEntity $subscriptionChangeEntity, string $userId): void
|
|
|
|
{
|
|
|
|
$idEpisodeActionEntityToUpdate = $this->subscriptionChangeRepository->findByUrl($subscriptionChangeEntity->getUrl(), $userId)->getId();
|
|
|
|
$subscriptionChangeEntity->setId($idEpisodeActionEntityToUpdate);
|
|
|
|
$this->subscriptionChangeWriter->update($subscriptionChangeEntity);
|
|
|
|
}
|
|
|
|
|
2021-06-27 11:19:26 +00:00
|
|
|
|
|
|
|
}
|