Passed
Pull Request — master (#1095)
by Aleksei
13:52 queued 04:04
created

Core::callAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 26
c 2
b 0
f 0
ccs 16
cts 16
cp 1
rs 9.7998
cc 4
nc 4
nop 3
crap 4
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\Interceptors\Context\CallContext;
10
use Spiral\Interceptors\HandlerInterface;
11
use Spiral\Queue\Options;
12
use Spiral\Queue\OptionsInterface;
13
use Spiral\Queue\QueueInterface;
14
use Spiral\Telemetry\NullTracer;
15
use Spiral\Telemetry\TracerInterface;
16
17
/**
18
 * @internal
19
 * @psalm-type TParameters = array{options: ?OptionsInterface, payload: mixed}
20
 */
21
final class Core implements CoreInterface, HandlerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Spiral\Core\CoreInterface has been deprecated: Use {@see HandlerInterface} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
final class Core implements /** @scrutinizer ignore-deprecated */ CoreInterface, HandlerInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
22
{
23 10
    public function __construct(
24
        private readonly QueueInterface $connection,
25
    ) {
26 10
    }
27
28
    /**
29
     * @param-assert TParameters $parameters
30
     * @deprecated
31
     */
32 6
    public function callAction(
33
        string $controller,
34
        string $action,
35
        array $parameters = ['options' => null, 'payload' => []],
36
    ): string {
37 6
        if ($parameters['options'] === null) {
38 4
            $parameters['options'] = new Options();
39
        }
40
41 6
        $tracer = $this->getTracer();
42
43 6
        if (\method_exists($parameters['options'], 'withHeader')) {
44 5
            foreach ($tracer->getContext() as $key => $data) {
45 1
                $parameters['options'] = $parameters['options']->withHeader($key, $data);
46
            }
47
        }
48
49 6
        return $tracer->trace(
50 6
            name: \sprintf('Job push [%s]', $controller),
51 6
            callback: fn (): string => $this->connection->push(
52 6
                name: $controller,
53 6
                payload: $parameters['payload'],
54 6
                options: $parameters['options'],
55 6
            ),
56 6
            attributes: [
57 6
                'queue.handler' => $controller,
58 6
            ],
59 6
        );
60
    }
61
62 2
    public function handle(CallContext $context): mixed
63
    {
64 2
        $args = $context->getArguments();
65 2
        $controller = $context->getTarget()->getPath()[0];
66 2
        $action = $context->getTarget()->getPath()[1];
67
68 2
        return $this->callAction($controller, $action, $args);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Queue\Interceptor\Push\Core::callAction() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

68
        return /** @scrutinizer ignore-deprecated */ $this->callAction($controller, $action, $args);
Loading history...
69
    }
70
71 6
    private function getTracer(): TracerInterface
72
    {
73
        try {
74 6
            return ContainerScope::getContainer()->get(TracerInterface::class);
75 5
        } catch (\Throwable $e) {
76 5
            return new NullTracer();
77
        }
78
    }
79
}
80