CommandInterceptor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 32
rs 10
ccs 9
cts 9
cp 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 18 4
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