bearsunday /
BEAR.Resource
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\Resource; |
||
| 6 | |||
| 7 | use BEAR\Resource\Exception\MethodNotAllowedException; |
||
| 8 | use Ray\Di\Di\Named; |
||
| 9 | |||
| 10 | final readonly class ExtraMethodInvoker |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 11 | { |
||
| 12 | public function __construct( |
||
| 13 | #[Named('options')] |
||
| 14 | private RenderInterface $optionsRenderer, |
||
| 15 | ) { |
||
| 16 | } |
||
| 17 | |||
| 18 | public function __invoke(AbstractRequest $request, InvokerInterface $invoker): ResourceObject |
||
| 19 | { |
||
| 20 | if ($request->method === Request::OPTIONS) { |
||
| 21 | $ro = $request->resourceObject; |
||
| 22 | $ro->view = $this->optionsRenderer->render($request->resourceObject); |
||
| 23 | |||
| 24 | return $ro; |
||
| 25 | } |
||
| 26 | |||
| 27 | if ($request->method === Request::HEAD) { |
||
| 28 | $getRequest = clone $request; |
||
| 29 | $getRequest->method = 'get'; |
||
| 30 | $ro = $invoker->invoke($getRequest); |
||
| 31 | $ro->body = null; |
||
| 32 | |||
| 33 | return $ro; |
||
| 34 | } |
||
| 35 | |||
| 36 | throw new MethodNotAllowedException($request->resourceObject::class . "::{({$request->method}}()", 405); |
||
| 37 | } |
||
| 38 | } |
||
| 39 |