CommandInterceptor::invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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