CommandInterceptor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
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 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