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

CreatedResourceInterceptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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