Test Failed
Pull Request — master (#1095)
by Aleksei
10:19
created

Core   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 21
c 0
b 0
f 0
dl 0
loc 52
rs 10
ccs 21
cts 21
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchEvent() 0 9 1
A callAction() 0 16 1
A handle() 0 7 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue\Interceptor\Consume;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Spiral\Core\CoreInterface;
9
use Spiral\Interceptors\Context\CallContext;
10
use Spiral\Interceptors\HandlerInterface;
11
use Spiral\Queue\Event\JobProcessed;
12
use Spiral\Queue\Event\JobProcessing;
13
use Spiral\Queue\HandlerRegistryInterface;
14
15
/**
16
 * @internal
17
 * @psalm-type TParameters = array{
18
 *     driver: non-empty-string,
19
 *     queue: non-empty-string,
20
 *     id: non-empty-string,
21
 *     payload: mixed,
22
 *     headers: array
23
 * }
24
 */
25 12
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

25
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...
26
{
27
    public function __construct(
28
        private readonly HandlerRegistryInterface $registry,
29 12
        private readonly ?EventDispatcherInterface $dispatcher = null
30
    ) {
31
    }
32
33
    /**
34 10
     * @param-assert TParameters $parameters
35
     * @deprecated
36 10
     */
37 10
    public function callAction(string $controller, string $action, array $parameters = []): mixed
38 10
    {
39
        \assert(\is_string($parameters['driver']));
40 10
        \assert(\is_string($parameters['queue']));
41
        \assert(\is_string($parameters['id']));
42
43 10
        $this->dispatchEvent(JobProcessing::class, $controller, $parameters);
44 10
45 10
        /** @psalm-suppress TooManyArguments */
46
        $this->registry
47 10
            ->getHandler($controller)
48
            ->handle($controller, $parameters['id'], $parameters['payload'], $parameters['headers'] ?? []);
0 ignored issues
show
Unused Code introduced by
The call to Spiral\Queue\HandlerInterface::handle() has too many arguments starting with $parameters['headers'] ?? array(). ( Ignorable by Annotation )

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

48
            ->/** @scrutinizer ignore-call */ handle($controller, $parameters['id'], $parameters['payload'], $parameters['headers'] ?? []);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49 10
50
        $this->dispatchEvent(JobProcessed::class, $controller, $parameters);
51
52
        return null;
53
    }
54
55
    public function handle(CallContext $context): mixed
56 10
    {
57
        $args = $context->getArguments();
58 10
        $controller = $context->getTarget()->getPath()[0];
59 10
        $action = $context->getTarget()->getPath()[1];
60 10
61 10
        return $this->callAction($controller, $action, $args);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Queue\Interceptor...sume\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

61
        return /** @scrutinizer ignore-deprecated */ $this->callAction($controller, $action, $args);
Loading history...
62 10
    }
63 10
64 10
    /**
65 10
     * @param class-string $event
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
66
     * @param-assert TParameters $parameters
67
     */
68
    private function dispatchEvent(string $event, string $name, array $parameters): void
69
    {
70
        $this->dispatcher?->dispatch(new $event(
71
            name: $name,
72
            driver: $parameters['driver'],
73
            queue: $parameters['queue'],
74
            id: $parameters['id'],
75
            payload: $parameters['payload'],
76
            headers: $parameters['headers'] ?? []
77
        ));
78
    }
79
}
80