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

Core   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 17
dl 0
loc 42
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchEvent() 0 9 1
A __construct() 0 4 1
A callAction() 0 16 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\Queue\Event\JobProcessed;
10
use Spiral\Queue\Event\JobProcessing;
11
use Spiral\Queue\HandlerRegistryInterface;
12
13
/**
14
 * @internal
15
 * @psalm-type TParameters = array{
16
 *     driver: non-empty-string,
17
 *     queue: non-empty-string,
18
 *     id: non-empty-string,
19
 *     payload: mixed,
20
 *     headers: array
21
 * }
22
 */
23
final class Core implements CoreInterface
24
{
25 12
    public function __construct(
26
        private readonly HandlerRegistryInterface $registry,
27
        private readonly ?EventDispatcherInterface $dispatcher = null
28
    ) {
29 12
    }
30
31
    /**
32
     * @param-assert TParameters $parameters
33
     */
34 10
    public function callAction(string $controller, string $action, array $parameters = []): mixed
35
    {
36 10
        \assert(\is_string($parameters['driver']));
37 10
        \assert(\is_string($parameters['queue']));
38 10
        \assert(\is_string($parameters['id']));
39
40 10
        $this->dispatchEvent(JobProcessing::class, $controller, $parameters);
41
42
        /** @psalm-suppress TooManyArguments */
43 10
        $this->registry
44 10
            ->getHandler($controller)
45 10
            ->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

45
            ->/** @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...
46
47 10
        $this->dispatchEvent(JobProcessed::class, $controller, $parameters);
48
49 10
        return null;
50
    }
51
52
    /**
53
     * @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...
54
     * @param-assert TParameters $parameters
55
     */
56 10
    private function dispatchEvent(string $event, string $name, array $parameters): void
57
    {
58 10
        $this->dispatcher?->dispatch(new $event(
59 10
            name: $name,
60 10
            driver: $parameters['driver'],
61 10
            queue: $parameters['queue'],
62 10
            id: $parameters['id'],
63 10
            payload: $parameters['payload'],
64 10
            headers: $parameters['headers'] ?? []
65 10
        ));
66
    }
67
}
68