ExtraMethodInvoker::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 class ExtraMethodInvoker
11
{
12
    public function __construct(
13
        #[Named('options')]
14
        private readonly 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