Skip to content

Commit

Permalink
WIP add collection action endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyzerner committed Sep 13, 2024
1 parent bad98dc commit 3e782b8
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/Endpoint/CollectionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Tobyz\JsonApiServer\Endpoint;

use Closure;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Exception\ForbiddenException;
use Tobyz\JsonApiServer\Exception\MethodNotAllowedException;
use Tobyz\JsonApiServer\OpenApi\OpenApiPathsProvider;
use Tobyz\JsonApiServer\Resource\Collection;
use Tobyz\JsonApiServer\Schema\Concerns\HasDescription;
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;

class CollectionAction implements Endpoint, OpenApiPathsProvider
{
use HasVisibility;
use HasDescription;

public string $method = 'POST';

public function __construct(public string $name, public Closure $handler)
{
}

public static function make(string $name, Closure $handler): static
{
return new static($name, $handler);
}

public function method(string $method): static
{
$this->method = strtoupper($method);

return $this;
}

public function handle(Context $context): ?ResponseInterface
{
$segments = explode('/', $context->path());

if (count($segments) !== 2 || $segments[1] !== $this->name) {
return null;
}

if ($context->request->getMethod() !== $this->method) {
throw new MethodNotAllowedException();
}

if (!$this->isVisible($context)) {
throw new ForbiddenException();
}

($this->handler)($context);

return new Response(204);
}

public function getOpenApiPaths(Collection $collection): array
{
return [
"/{$collection->name()}/$this->name" => [
'post' => [
'description' => $this->getDescription(),
'tags' => [$collection->name()],
'responses' => [
'204' => [],
],
],
],
];
}
}

0 comments on commit 3e782b8

Please sign in to comment.