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

CreatedResourceInterceptor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
c 0
b 0
f 0
dl 0
loc 25
ccs 10
cts 10
cp 1
rs 10

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
    /** @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