Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

ErrorHandlerInterceptor::intercept()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue\Interceptor\Consume;
6
7
use Spiral\Core\CoreInterceptorInterface as LegacyInterceptor;
8
use Spiral\Core\CoreInterface;
9
use Spiral\Interceptors\Context\CallContextInterface;
10
use Spiral\Interceptors\HandlerInterface;
11
use Spiral\Interceptors\InterceptorInterface;
12
use Spiral\Queue\Exception\StateException;
13
use Spiral\Queue\Failed\FailedJobHandlerInterface;
14
15
final class ErrorHandlerInterceptor implements LegacyInterceptor, InterceptorInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Spiral\Core\CoreInterceptorInterface has been deprecated: Use {@see InterceptorInterface} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

15
final class ErrorHandlerInterceptor implements /** @scrutinizer ignore-deprecated */ LegacyInterceptor, InterceptorInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
16
{
17 7
    public function __construct(
18
        private readonly FailedJobHandlerInterface $handler
19
    ) {
20 7
    }
21
22
    /** @psalm-suppress ParamNameMismatch */
23 3
    public function process(string $name, string $action, array $parameters, CoreInterface $core): mixed
24
    {
25
        try {
26 3
            return $core->callAction($name, $action, $parameters);
27 1
        } catch (\Throwable $e) {
28 1
            if (!$e instanceof StateException) {
29 1
                $this->handler->handle(
30 1
                    $parameters['driver'],
31 1
                    $parameters['queue'],
32 1
                    $name,
33 1
                    $parameters['payload'],
34 1
                    $e
35 1
                );
36
            }
37
38 1
            throw $e;
39
        }
40
    }
41
42
    public function intercept(CallContextInterface $context, HandlerInterface $handler): mixed
43
    {
44
        try {
45
            return $handler->handle($context);
46
        } catch (\Throwable $e) {
47
            $args = $context->getArguments();
48
            if (!$e instanceof StateException) {
49
                $this->handler->handle(
50
                    $args['driver'],
51
                    $args['queue'],
52
                    $context->getTarget()->getPath()[0],
53
                    $args['payload'],
54
                    $e,
55
                );
56
            }
57
58
            throw $e;
59
        }
60
    }
61
}
62