CreatedResourceInterceptor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 7
dl 0
loc 22
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 12 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 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