Completed
Pull Request — 1.x (#177)
by Akihito
04:59 queued 03:33
created

Invoker::noMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
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 100
    }
26
27 100
    /**
28 100
     * {@inheritdoc}
29 100
     */
30
    public function invoke(AbstractRequest $request) : ResourceObject
31
    {
32
        $callable = [$request->resourceObject, 'on' . ucfirst($request->method)];
33
        if (! is_callable($callable)) {
34 79
            // OPTIONS or HEAD
35
            return ($this->extraMethod)($request, $this);
36 79
        }
37 79
        $params = $this->params->getParameters($callable, $request->query);
38 11
        $response = call_user_func_array($callable, $params);
39
        if (! $response instanceof ResourceObject) {
40 68
            $request->resourceObject->body = $response;
41 64
            $response = $request->resourceObject;
42 61
        }
43 35
44 35
        return $response;
45
    }
46
}
47