Passed
Pull Request — master (#816)
by butschster
06:28
created

Core   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
eloc 20
c 2
b 0
f 1
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 10

3 Methods

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