CreatedResourceInterceptor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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