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

ExtraMethodInvoker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 20 3
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