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

ErrorHandlerInterceptor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 51.85%

Importance

Changes 0
Metric Value
wmc 7
eloc 24
dl 0
loc 44
ccs 14
cts 27
cp 0.5185
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A intercept() 0 17 3
A process() 0 16 3
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