Skip to content

Commit

Permalink
Add delete entity method
Browse files Browse the repository at this point in the history
  • Loading branch information
lbacik committed Apr 24, 2024
1 parent 8d9a820 commit f7002b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/SDK/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public function updateEntity(Entity $entity, string $token): Entity
return $this->processRequestAndMapResponse($request, Entity::class);
}

public function deleteEntity(string $entityUuid, string $token): void
{
$request = $this->requestFactory->createDeleteEntityRequest($entityUuid, $token);

$this->processRequestAndMapResponse($request);
}

public function validateToken(string $token): bool
{
$request = $this->requestFactory->createValidateTokenRequest($token);
Expand Down Expand Up @@ -103,17 +110,22 @@ public function getLastQueryCount(): int

private function processRequestAndMapResponse(
RequestInterface $request,
string $responseClassMapper
): object {
string|null $responseClassMapper = null,
): object|null {
try {
$response = $this->httpClient->sendRequest($request);

match ($response->getStatusCode()) {
200, 201 => null,
200, 201, 204 => null,
default => throw new RuntimeException('Response status code', $response->getStatusCode()),
};

$body = $this->extractFromJsonLD($response->getBody()->getContents());

if ($responseClassMapper === null) {
return null;
}

return $this->mapperFactory
->getMapperFor($responseClassMapper)
->map($body);
Expand Down
9 changes: 9 additions & 0 deletions src/SDK/Client/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public function createUpdateEntityRequest(string $id, array $data, string $token
);
}

public function createDeleteEntityRequest(string $id, string $token): Request
{
return new Request(
'DELETE',
$this->urlFactory->getEntity($id),
$this->generateHeaders($token),
);
}

public function createValidateTokenRequest(string $token): Request
{
return new Request(
Expand Down

0 comments on commit f7002b2

Please sign in to comment.