GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#226)
by Marco
06:53
created

Consumer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 17
Bugs 1 Features 6
Metric Value
wmc 20
c 17
b 1
f 6
lcom 1
cbo 7
dl 0
loc 182
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A shutdown() 0 4 1
A pause() 0 4 1
A resume() 0 4 1
A __construct() 0 5 1
A consume() 0 10 2
B tick() 0 30 6
A invoke() 0 18 3
A configure() 0 10 2
A bind() 0 8 1
A rejectDispatch() 0 12 2
1
<?php
2
3
namespace Bernard;
4
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use Bernard\Event\EnvelopeEvent;
7
use Bernard\Event\PingEvent;
8
use Bernard\Event\RejectEnvelopeEvent;
9
10
/**
11
 * @package Consumer
12
 */
13
class Consumer
14
{
15
    protected $router;
16
    protected $dispatcher;
17
    protected $shutdown = false;
18
    protected $pause = false;
19
    protected $configured = false;
20
    protected $options = [
21
        'max-runtime' => PHP_INT_MAX,
22
        'max-messages' => null,
23
        'stop-when-empty' => false,
24
        'stop-on-error' => false,
25
    ];
26
27
    /**
28
     * @param Router                   $router
29
     * @param EventDispatcherInterface $dispatcher
30
     */
31
    public function __construct(Router $router, EventDispatcherInterface $dispatcher)
32
    {
33
        $this->router = $router;
34
        $this->dispatcher = $dispatcher;
35
    }
36
37
    /**
38
     * Starts an infinite loop calling Consumer::tick();
39
     *
40
     * @param Queue $queue
41
     * @param array $options
42
     */
43
    public function consume(Queue $queue, array $options = [])
44
    {
45
        declare (ticks = 1);
46
47
        $this->bind();
48
49
        while ($this->tick($queue, $options)) {
50
            // NO op
51
        }
52
    }
53
54
    /**
55
     * Returns true do indicate it should be run again or false to indicate
56
     * it should not be run again.
57
     *
58
     * @param Queue $queue
59
     * @param array $options
60
     *
61
     * @return bool
62
     */
63
    public function tick(Queue $queue, array $options = [])
64
    {
65
        $this->configure($options);
66
67
        if ($this->shutdown) {
68
            return false;
69
        }
70
71
        if (microtime(true) > $this->options['max-runtime']) {
72
            return false;
73
        }
74
75
        if ($this->pause) {
76
            return true;
77
        }
78
79
        $this->dispatcher->dispatch(BernardEvents::PING, new PingEvent($queue));
80
81
        if (!$envelope = $queue->dequeue()) {
82
            return !$this->options['stop-when-empty'];
83
        }
84
85
        $this->invoke($envelope, $queue);
86
87
        if (null === $this->options['max-messages']) {
88
            return true;
89
        }
90
91
        return (boolean) --$this->options['max-messages'];
92
    }
93
94
    /**
95
     * Mark Consumer as shutdown
96
     */
97
    public function shutdown()
98
    {
99
        $this->shutdown = true;
100
    }
101
102
    /**
103
     * Pause consuming
104
     */
105
    public function pause()
106
    {
107
        $this->pause = true;
108
    }
109
110
    /**
111
     * Resume consuming
112
     */
113
    public function resume()
114
    {
115
        $this->pause = false;
116
    }
117
118
    /**
119
     * Until there is a real extension point to doing invoked stuff, this can be used
120
     * by wrapping the invoke method.
121
     *
122
     * @param Envelope $envelope
123
     * @param Queue    $queue
124
     *
125
     * @throws \Exception
126
     * @throws \Throwable
127
     */
128
    public function invoke(Envelope $envelope, Queue $queue)
129
    {
130
        try {
131
            $this->dispatcher->dispatch(BernardEvents::INVOKE, new EnvelopeEvent($envelope, $queue));
132
133
            // for 5.3 support where a function name is not a callable
134
            call_user_func($this->router->map($envelope), $envelope->getMessage());
135
136
            // We successfully processed the message.
137
            $queue->acknowledge($envelope);
138
139
            $this->dispatcher->dispatch(BernardEvents::ACKNOWLEDGE, new EnvelopeEvent($envelope, $queue));
140
        } catch (\Throwable $error) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
141
            $this->rejectDispatch($error, $envelope, $queue);
142
        } catch (\Exception $exception) {
143
            $this->rejectDispatch($exception, $envelope, $queue);
144
        }
145
    }
146
147
    /**
148
     * @param array $options
149
     */
150
    protected function configure(array $options)
151
    {
152
        if ($this->configured) {
153
            return $this->options;
154
        }
155
156
        $this->options = array_filter($options) + $this->options;
157
        $this->options['max-runtime'] += microtime(true);
158
        $this->configured = true;
159
    }
160
161
    /**
162
     * Setup signal handlers for unix signals.
163
     */
164
    protected function bind()
165
    {
166
        pcntl_signal(SIGTERM, [$this, 'shutdown']);
167
        pcntl_signal(SIGINT,  [$this, 'shutdown']);
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
168
        pcntl_signal(SIGQUIT, [$this, 'shutdown']);
169
        pcntl_signal(SIGUSR2, [$this, 'pause']);
170
        pcntl_signal(SIGCONT, [$this, 'resume']);
171
    }
172
173
    /**
174
     * @param \Throwable|\Exception $exception note that the type-hint is missing due to PHP 5.x compat
175
     *
176
     * @param Envelope              $envelope
177
     * @param Queue                 $queue
178
     *
179
     * @throws \Exception
180
     * @throws \Throwable
181
     */
182
    private function rejectDispatch($exception, Envelope $envelope, Queue $queue)
183
    {
184
        // Make sure the exception is not interfering.
185
        // Previously failing jobs handling have been moved to a middleware.
186
        //
187
        // Emit an event to let others log that exception
188
        $this->dispatcher->dispatch(BernardEvents::REJECT, new RejectEnvelopeEvent($envelope, $queue, $exception));
189
190
        if ($this->options['stop-on-error']) {
191
            throw $exception;
192
        }
193
    }
194
}
195