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.
Passed
Push — develop ( 404db9...8cc0dc )
by Baptiste
03:06 queued 15s
created

Consumer::foreach()   B

Complexity

Conditions 9
Paths 59

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 9.2855

Importance

Changes 0
Metric Value
cc 9
eloc 47
nc 59
nop 1
dl 0
loc 69
ccs 39
cts 46
cp 0.8478
crap 9.2855
rs 7.6008
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQP\Client\Channel\Basic\Consumer;
5
6
use Innmind\AMQP\{
7
    Client\Channel\Basic\Consumer as ConsumerInterface,
8
    Model\Basic\Consume,
9
    Model\Basic\Ack,
10
    Model\Basic\Reject as RejectCommand,
11
    Model\Basic\Cancel as CancelCommand,
12
    Model\Basic\Message\Locked,
13
    Transport\Connection,
14
    Transport\Connection\MessageReader,
15
    Transport\Frame\Channel,
16
    Transport\Frame\Type,
17
    Exception\MessageFromAnotherConsumerReceived,
0 ignored issues
show
Bug introduced by
The type Innmind\AMQP\Exception\M...AnotherConsumerReceived was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
    Exception\Requeue,
19
    Exception\Reject,
20
    Exception\Cancel,
21
};
22
23
final class Consumer implements ConsumerInterface
24
{
25
    private $connection;
26
    private $command;
27
    private $channel;
28
    private $consumerTag;
29
    private $read;
30
    private $take;
31
    private $predicate;
32
    private $canceled = false;
33
34 7
    public function __construct(
35
        Connection $connection,
36
        Consume $command,
37
        Channel $channel,
38
        string $consumerTag
39
    ) {
40 7
        $this->connection = $connection;
41 7
        $this->command = $command;
42 7
        $this->channel = $channel;
43 7
        $this->consumerTag = $consumerTag;
44 7
        $this->read = new MessageReader;
45
        $this->predicate = static function(): bool {
46 6
            return true; //by default consume all messages
47
        };
48 7
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 7
    public function foreach(callable $consume): void
54
    {
55 7
        if ($this->canceled) {
56 2
            return;
57
        }
58
59 7
        while ($this->shouldConsume()) {
60
            try {
61 7
                $frame = $this->connection->wait('basic.deliver');
62 7
                $message = ($this->read)($this->connection);
63 7
                $message = new Locked($message);
64 7
                $consumerTag = (string) $frame->values()->first()->original();
65 7
                $deliveryTag = $frame->values()->get(1)->original()->value();
66 7
                $redelivered = $frame->values()->get(2)->original()->first();
67 7
                $exchange = (string) $frame->values()->get(3)->original();
68 7
                $routingKey = (string) $frame->values()->get(4)->original();
69
70 7
                if ($this->consumerTag !== $consumerTag) {
71
                    throw new MessageFromAnotherConsumerReceived(
72
                        $message,
73
                        $consumerTag,
74
                        $deliveryTag,
75
                        $redelivered,
76
                        $exchange,
77
                        $routingKey
78
                    );
79
                }
80
81 7
                $toProcess = ($this->predicate)(
82 7
                    $message,
83 7
                    $redelivered,
84 7
                    $exchange,
85 7
                    $routingKey
86
                );
87
88 7
                if (!$toProcess) {
89 1
                    throw new Requeue;
90
                }
91
92
                //flag before the consume call as the "take" applies to the number
93
                //of messages that match the predicate and the number of
94
                //successfully consumed messages
95 7
                $this->flagConsumed();
96
97 7
                $consume(
98 7
                    $message,
99 7
                    $redelivered,
100 7
                    $exchange,
101 7
                    $routingKey
102
                );
103
104 5
                $this->ack($deliveryTag);
105 5
            } catch (Reject $e) {
106 1
                $this->reject($deliveryTag);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $deliveryTag does not seem to be defined for all execution paths leading up to this point.
Loading history...
107 4
            } catch (Requeue $e) {
108 2
                $this->requeue($deliveryTag);
109 2
            } catch (Cancel $e) {
110 1
                $this->ack($deliveryTag);
111
112 1
                break;
113 1
            } catch (\Throwable $e) {
114 1
                $this->requeue($deliveryTag);
115 1
                $this->cancel();
116
117 1
                throw $e;
118
            }
119
        }
120
121 6
        $this->cancel();
122 6
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 5
    public function take(int $count): ConsumerInterface
128
    {
129 5
        $this->take = $count;
130
131 5
        return $this;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 1
    public function filter(callable $predicate): ConsumerInterface
138
    {
139 1
        $this->predicate = $predicate;
140
141 1
        return $this;
142
    }
143
144 7
    private function shouldConsume(): bool
145
    {
146 7
        if ($this->canceled()) {
147
            return false;
148
        }
149
150 7
        if (\is_null($this->take)) {
151 2
            return true;
152
        }
153
154 5
        return $this->take > 0;
155
    }
156
157 7
    private function flagConsumed(): void
158
    {
159 7
        if (\is_null($this->take)) {
160 2
            return;
161
        }
162
163 5
        --$this->take;
164 5
    }
165
166 5
    private function ack(int $deliveryTag): void
167
    {
168 5
        if ($this->command->shouldAutoAcknowledge()) {
169
            return;
170
        }
171
172 5
        $this->connection->send(
173 5
            $this->connection->protocol()->basic()->ack(
174 5
                $this->channel,
175 5
                new Ack($deliveryTag)
176
            )
177
        );
178 5
    }
179
180 1
    private function reject(int $deliveryTag): void
181
    {
182 1
        $this->connection->send(
183 1
            $this->connection->protocol()->basic()->reject(
184 1
                $this->channel,
185 1
                new RejectCommand($deliveryTag)
186
            )
187
        );
188 1
    }
189
190 5
    private function requeue(int $deliveryTag): void
191
    {
192 5
        $this->connection->send(
193 5
            $this->connection->protocol()->basic()->reject(
194 5
                $this->channel,
195 5
                RejectCommand::requeue($deliveryTag)
196
            )
197
        );
198 5
    }
199
200 7
    private function canceled(): bool
201
    {
202 7
        if ($this->canceled) {
203
            return true;
204
        }
205
206 7
        return $this->connection->closed();
207
    }
208
209 7
    private function cancel(): void
210
    {
211 7
        if ($this->canceled()) {
212
            return;
213
        }
214
215 7
        $this->connection->send($this->connection->protocol()->basic()->cancel(
216 7
            $this->channel,
217 7
            new CancelCommand($this->consumerTag)
218
        ));
219
220 7
        $deliver = $this->connection->protocol()->method('basic.deliver');
221 7
        $expected = $this->connection->protocol()->method('basic.cancel-ok');
222
223
        do {
224 7
            $frame = $this->connection->wait();
225
226
            if (
227 7
                $frame->type() === Type::method() &&
228 7
                $frame->is($deliver)
229
            ) {
230
                //requeue all the messages sent right before the cancel method
231 5
                $message = ($this->read)($this->connection);
0 ignored issues
show
Unused Code introduced by
The assignment to $message is dead and can be removed.
Loading history...
232 5
                $this->requeue($frame->values()->get(1)->original()->value());
233
            }
234 7
        } while (!$frame->is($expected));
235
236 7
        $this->canceled = true;
237 7
    }
238
}
239