Passed
Push — master ( e7d330...e6cdc9 )
by Aleksei
11:08 queued 15s
created

Core::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
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\Interceptor\Push;
6
7
use Spiral\Core\ContainerScope;
8
use Spiral\Core\CoreInterface;
9
use Spiral\Queue\Options;
10
use Spiral\Queue\OptionsInterface;
11
use Spiral\Queue\QueueInterface;
12
use Spiral\Telemetry\NullTracer;
13
use Spiral\Telemetry\TracerInterface;
14
15
/**
16
 * @internal
17
 * @psalm-type TParameters = array{options: ?OptionsInterface, payload: mixed}
18
 */
19
final class Core implements CoreInterface
20
{
21 10
    public function __construct(
22
        private readonly QueueInterface $connection,
23
    ) {
24 10
    }
25
26
    /**
27
     * @param-assert TParameters $parameters
28
     */
29 6
    public function callAction(
30
        string $controller,
31
        string $action,
32
        array $parameters = ['options' => null, 'payload' => []],
33
    ): string {
34 6
        if ($parameters['options'] === null) {
35 4
            $parameters['options'] = new Options();
36
        }
37
38 6
        $tracer = $this->getTracer();
39
40 6
        if (\method_exists($parameters['options'], 'withHeader')) {
41 5
            foreach ($tracer->getContext() as $key => $data) {
42 1
                $parameters['options'] = $parameters['options']->withHeader($key, $data);
43
            }
44
        }
45
46 6
        return $tracer->trace(
47 6
            name: \sprintf('Job push [%s]', $controller),
48 6
            callback: fn (): string => $this->connection->push(
49 6
                name: $controller,
50 6
                payload: $parameters['payload'],
51 6
                options: $parameters['options'],
52 6
            ),
53 6
            attributes: [
54 6
                'queue.handler' => $controller,
55 6
            ],
56 6
        );
57
    }
58
59 6
    private function getTracer(): TracerInterface
60
    {
61
        try {
62 6
            return ContainerScope::getContainer()->get(TracerInterface::class);
63 5
        } catch (\Throwable $e) {
64 5
            return new NullTracer();
65
        }
66
    }
67
}
68