Passed
Pull Request — master (#464)
by Kirill
12:51
created

Consumer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serve() 0 15 4
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Jobs;
13
14
use Spiral\RoadRunner\Worker;
15
16
/***
17
 * @codeCoverageIgnore handled on Golang end.
18
 */
19
final class Consumer
20
{
21
    /** @var HandlerRegistryInterface */
22
    private $registry;
23
24
    /**
25
     * @codeCoverageIgnore
26
     * @param HandlerRegistryInterface $registry
27
     */
28
    public function __construct(HandlerRegistryInterface $registry)
29
    {
30
        $this->registry = $registry;
31
    }
32
33
    /**
34
     * @codeCoverageIgnore
35
     * @param Worker        $worker
36
     * @param callable|null $finalize
37
     */
38
    public function serve(Worker $worker, callable $finalize = null): void
39
    {
40
        while ($body = $worker->receive($context)) {
0 ignored issues
show
Bug introduced by
The method receive() does not exist on Spiral\RoadRunner\Worker. ( Ignorable by Annotation )

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

40
        while ($body = $worker->/** @scrutinizer ignore-call */ receive($context)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Comprehensibility Best Practice introduced by
The variable $context does not seem to be defined for all execution paths leading up to this point.
Loading history...
41
            try {
42
                $context = json_decode($context, true);
43
                $handler = $this->registry->getHandler($context['job']);
44
45
                $handler->handle($context['job'], $context['id'], $body);
46
47
                $worker->send('ok');
48
            } catch (\Throwable $e) {
49
                $worker->error((string)$e->getMessage());
50
            } finally {
51
                if ($finalize !== null) {
52
                    call_user_func($finalize, $e ?? null);
53
                }
54
            }
55
        }
56
    }
57
}
58