Passed
Pull Request — master (#1104)
by Maxim
12:14
created

JobHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 31
ccs 8
cts 12
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 13 3
A getHandlerMethod() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue;
6
7
use Spiral\Core\Attribute\Proxy;
8
use Spiral\Core\InvokerInterface;
9
use Spiral\Queue\Exception\JobException;
10
11
/**
12
 * Handler which can invoke itself.
13
 */
14
abstract class JobHandler implements HandlerInterface
15
{
16
    /**
17
     * Default function with method injection.
18
     */
19
    protected const HANDLE_FUNCTION = 'invoke';
20
21 5
    public function __construct(
22
        #[Proxy] protected InvokerInterface $invoker,
23
    ) {
24 5
    }
25
26 1
    public function handle(string $name, string $id, mixed $payload, array $headers = []): void
27
    {
28
        try {
29 1
            $params = ['payload' => $payload, 'id' => $id, 'headers' => $headers];
30
31 1
            if (\is_array($payload)) {
32
                $params = \array_merge($params, $payload);
33
            }
34
35 1
            $this->invoker->invoke([$this, $this->getHandlerMethod()], $params);
36
        } catch (\Throwable $e) {
37
            $message = \sprintf('[%s] %s', $this::class, $e->getMessage());
38
            throw new JobException($message, (int)$e->getCode(), $e);
39
        }
40
    }
41
42 1
    protected function getHandlerMethod(): string
43
    {
44 1
        return static::HANDLE_FUNCTION;
45
    }
46
}
47