-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bad98dc
commit 3e782b8
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => [], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |