2024-07-23 02:05:38 +02:00
|
|
|
<?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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 20:11:19 +02:00
|
|
|
public function findByPushResource(string $userId, string $collectionName, string $pushResource): ?WebPushSubscription {
|
2024-07-23 02:05:38 +02:00
|
|
|
try {
|
2024-08-12 20:11:19 +02:00
|
|
|
return $this->mapper->findByPushResource($userId, $collectionName, $pushResource);
|
2024-07-23 02:05:38 +02:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|