1
0
Fork 0
nc_ext_dav_push/lib/Service/WebPushSubscriptionService.php
Jonathan Treffler b6ba200281 fix #6
2024-08-12 20:11:19 +02:00

64 lines
No EOL
1.8 KiB
PHP

<?php
namespace OCA\DavPush\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\DavPush\Errors\WebPushSubscriptionNotFound;
use OCA\DavPush\Db\WebPushSubscription;
use OCA\DavPush\Db\WebPushSubscriptionMapper;
class WebPushSubscriptionService {
public function __construct(
private WebPushSubscriptionMapper $mapper
) {
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new WebPushSubscriptionNotFound($e->getMessage());
} else {
throw $e;
}
}
public function findBySubscriptionId(int $subscriptionId): ?WebPushSubscription {
try {
return $this->mapper->findBySubscriptionId($subscriptionId);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function findByPushResource(string $userId, string $collectionName, string $pushResource): ?WebPushSubscription {
try {
return $this->mapper->findByPushResource($userId, $collectionName, $pushResource);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function create(int $subscriptionId, string $pushResource): WebPushSubscription {
$webPushSubscription = new WebPushSubscription();
$webPushSubscription->setSubscriptionId($subscriptionId);
$webPushSubscription->setPushResource($pushResource);
$webPushSubscription = $this->mapper->insert($webPushSubscription);
return $webPushSubscription;
}
public function deleteBySubscriptionId(int $subscriptionId): ?WebPushSubscription {
try {
$webPushSubscription = $this->mapper->findBySubscriptionId($subscriptionId);
$this->mapper->delete($webPushSubscription);
return $webPushSubscription;
} catch (Exception $e) {
$this->handleException($e);
}
}
}