CreatedResourceInterceptor::invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 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
    public function __construct(CreatedResourceRenderer $renderer)
19
    {
20
        $this->renderer = $renderer;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function invoke(MethodInvocation $invocation)
27
    {
28
        $ro = $invocation->proceed();
29
        assert($ro instanceof ResourceObject);
30
        $isCreated = $ro->code === 201 && isset($ro->headers['Location']);
31
        if (! $isCreated) {
32
            return $ro;
33
        }
34
35
        $ro->setRenderer($this->renderer);
36
37
        return $ro;
38
    }
39
}
40