Issues (61)

src/CommandInterceptor.php (1 issue)

Labels
Severity
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 readonly class CommandInterceptor implements MethodInterceptor
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 15 at column 6
Loading history...
16
{
17
    /** @param CommandInterface[] $commands */
18
    public function __construct(
19
        #[Commands]
20
        private array $commands,
21
    ) {
22
    }
23
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @throws ReturnValueIsNotResourceObjectException
28
     */
29
    #[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
        }
37
38
        if ($ro->code >= Code::BAD_REQUEST) {
39
            return $ro;
40
        }
41
42
        foreach ($this->commands as $command) {
43
            $command->command($invocation, $ro);
44
        }
45
46
        return $ro;
47
    }
48
}
49