Completed
Push — 1.x ( c86fd3...4b8bd5 )
by Akihito
14s queued 12s
created

CreatedResourceInterceptor::invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Representation;
6
7
use BEAR\Resource\ResourceObject;
8
use Ray\Aop\MethodInterceptor;
9
use Ray\Aop\MethodInvocation;
10
11
use function assert;
12
13
class CreatedResourceInterceptor implements MethodInterceptor
14
{
15
    /** @var CreatedResourceRenderer */
16
    private $renderer;
17
18 7
    public function __construct(CreatedResourceRenderer $renderer)
19
    {
20 7
        $this->renderer = $renderer;
21 7
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 6
    public function invoke(MethodInvocation $invocation)
27
    {
28
        $ro = $invocation->proceed();
29 6
        assert($ro instanceof ResourceObject);
30 6
        $isCreated = $ro->code === 201 && isset($ro->headers['Location']);
31 6
        if (! $isCreated) {
32 1
            return $ro;
33
        }
34 5
35
        $ro->setRenderer($this->renderer);
36 5
37
        return $ro;
38
    }
39
}
40