Completed
Push — refactor ( 79df24...a59ae4 )
by Akihito
04:36 queued 01:48
created

ExtraMethodInvoker::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 2
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
    /**
13
     * @var RenderInterface
14
     */
15
    private $optionsRenderer;
16
17
    /**
18
     * @Named("optionsRenderer=options")
19
     */
20
    public function __construct(RenderInterface $optionsRenderer)
21
    {
22
        $this->optionsRenderer = $optionsRenderer;
23
    }
24
25
    public function __invoke(AbstractRequest $request, InvokerInterface $invoker) : ResourceObject
26
    {
27
        if ($request->method === Request::OPTIONS) {
28
            $ro = $request->resourceObject;
29
            $ro->view = $this->optionsRenderer->render($request->resourceObject);
30
31
            return $ro;
32
        }
33
34
        if ($request->method === Request::HEAD) {
35
            $getRequest = clone $request;
36
            $getRequest->method = 'get';
37
            $ro = $invoker->invoke($getRequest);
38
            $ro->body = null;
39
40
            return $ro;
41
        }
42
43
        throw new MethodNotAllowedException(get_class($request->resourceObject) . "::{({$request->method}}()", 405);
44
    }
45
}
46