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

Invoker::noMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use function is_callable;
8
9
final class Invoker implements InvokerInterface
10
{
11
    /**
12
     * @var NamedParameterInterface
13
     */
14
    private $params;
15
16
    /**
17
     * @var ExtraMethodInvoker
18
     */
19
    private $extraMethod;
20
21
    public function __construct(NamedParameterInterface $params, ExtraMethodInvoker $extraMethod)
22
    {
23
        $this->params = $params;
24
        $this->extraMethod = $extraMethod;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29 95
     */
30
    public function invoke(AbstractRequest $request) : ResourceObject
31 95
    {
32 95
        $callable = [$request->resourceObject, 'on' . ucfirst($request->method)];
33 95
        if (! is_callable($callable)) {
34
            // OPTIONS or HEAD
35
            return ($this->extraMethod)($request, $this);
36
        }
37
        $params = $this->params->getParameters($callable, $request->query);
38
        $response = call_user_func_array($callable, $params);
39
        if (! $response instanceof ResourceObject) {
40 75
            $request->resourceObject->body = $response;
41
            $response = $request->resourceObject;
42 75
        }
43 75
44 64
        return $response;
45
    }
46
}
47