diff --git a/lib/Service/ConsumertService.php b/lib/Service/ConsumertService.php new file mode 100644 index 0000000..b72da51 --- /dev/null +++ b/lib/Service/ConsumertService.php @@ -0,0 +1,128 @@ +setUuid(Uuid::v4()->toString()); + $consumer->setName($data['name']); + $consumer->setDescription($data['description'] ?? ''); + $consumer->setDomains($data['domains'] ?? []); + $consumer->setIps($data['ips'] ?? []); + $consumer->setAuthorizationType($data['authorizationType'] ?? 'none'); + $consumer->setAuthorizationConfiguration($data['authorizationConfiguration'] ?? null); + $consumer->setUserId($userId); + $consumer->setCreated(new DateTime()); + $consumer->setUpdated(new DateTime()); + + // Persist the consumer + return $this->mapper->insert($consumer); + + } catch (Exception $e) { + $this->logger->error('Failed to establish consumer: ' . $e->getMessage(), [ + 'exception' => $e, + 'data' => $data + ]); + throw new Exception('Failed to establish consumer: ' . $e->getMessage()); + } + } + + /** + * Get a consumer by its UUID + * + * @param string $uuid + * @param string $userId + * @return Consumer + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + */ + public function getConsumerByUuid(string $uuid, string $userId): Consumer { + return $this->mapper->findAll(filters: [ + 'uuid' => $uuid, + 'userId' => $userId + ])[0] ?? throw new DoesNotExistException('Consumer not found'); + } + + /** + * Get all consumers for a user + * + * @param string $userId + * @param array|null $filters Additional filters + * @return Consumer[] + */ + public function getUserConsumers(string $userId, ?array $filters = []): array { + $filters['userId'] = $userId; + return $this->mapper->findAll(filters: $filters); + } + + /** + * Delete a consumer + * + * @param string $uuid + * @param string $userId + * @return void + * @throws DoesNotExistException + * @throws Exception + */ + public function deleteConsumer(string $uuid, string $userId): void { + try { + $consumer = $this->getConsumerByUuid($uuid, $userId); + $this->mapper->delete($consumer); + } catch (Exception $e) { + $this->logger->error('Failed to delete consumer: ' . $e->getMessage(), [ + 'exception' => $e, + 'uuid' => $uuid + ]); + throw new Exception('Failed to delete consumer: ' . $e->getMessage()); + } + } +}