Passed
Push — master ( 738663...b5cb7b )
by butschster
25:04 queued 15:39
created

JobHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

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

3 Methods

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