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

JobHandler::getHandlerMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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