CommandInterceptor::invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\QueryRepository\Exception\ReturnValueIsNotResourceObjectException;
8
use BEAR\RepositoryModule\Annotation\Commands;
9
use BEAR\Resource\Code;
10
use BEAR\Resource\ResourceObject;
11
use Ray\Aop\MethodInterceptor;
12
use Ray\Aop\MethodInvocation;
13
14
final class CommandInterceptor implements MethodInterceptor
15
{
16
    /** @param CommandInterface[] $commands */
17
    public function __construct(
18
        #[Commands]
19
        private readonly array $commands,
20
    ) {
21
    }
22
23
    /**
24
     * {@inheritDoc}
25
     *
26 13
     * @throws ReturnValueIsNotResourceObjectException
27
     */
28 13
    public function invoke(MethodInvocation $invocation)
29 13
    {
30
        /** @psalm-suppress MixedAssignment */
31
        $ro = $invocation->proceed();
32
        if (! $ro instanceof ResourceObject) {
33
            throw new ReturnValueIsNotResourceObjectException($invocation->getThis()::class);
34
        }
35
36 10
        if ($ro->code >= Code::BAD_REQUEST) {
37
            return $ro;
38 10
        }
39 10
40 1
        foreach ($this->commands as $command) {
41
            $command->command($invocation, $ro);
42
        }
43 9
44 2
        return $ro;
45
    }
46
}
47