CreatedResourceInterceptor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 8
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 13 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Representation;
6
7
use BEAR\Resource\ResourceObject;
8
use Override;
9
use Ray\Aop\MethodInterceptor;
10
use Ray\Aop\MethodInvocation;
11
12
use function assert;
13
14
final class CreatedResourceInterceptor implements MethodInterceptor
15
{
16
    public function __construct(
17
        private CreatedResourceRenderer $renderer,
18
    ) {
19
    }
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    #[Override]
25
    public function invoke(MethodInvocation $invocation)
26
    {
27
        $ro = $invocation->proceed();
28
        assert($ro instanceof ResourceObject);
29
        $isCreated = $ro->code === 201 && isset($ro->headers['Location']);
30
        if (! $isCreated) {
31
            return $ro;
32
        }
33
34
        $ro->setRenderer($this->renderer);
35
36
        return $ro;
37
    }
38
}
39