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

JobHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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