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
Push — develop ( 1664f7...42df07 )
by Baptiste
02:19
created

Connection   C

Complexity

Total Complexity 25

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 29

Test Coverage

Coverage 88.6%

Importance

Changes 0
Metric Value
dl 0
loc 215
c 0
b 0
f 0
wmc 25
lcom 1
cbo 29
ccs 101
cts 114
cp 0.886
rs 5

13 Methods

Rating   Name   Duplication   Size   Complexity  
A protocol() 0 4 1
A send() 0 22 3
A wait() 0 16 4
A __construct() 0 19 1
A maxFrameSize() 0 4 1
A close() 0 12 2
A opened() 0 4 2
A __destruct() 0 4 1
A buildSocket() 0 8 1
A open() 0 12 2
B start() 0 42 4
B handshake() 0 32 2
A openVHost() 0 8 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQP\Transport;
5
6
use Innmind\AMQP\{
7
    Transport\Connection\FrameReader,
8
    Transport\Protocol\Version,
9
    Transport\Frame\Value\UnsignedOctet,
10
    Model\Connection\StartOk,
11
    Model\Connection\SecureOk,
12
    Model\Connection\TuneOk,
13
    Model\Connection\Open,
14
    Model\Connection\Close,
15
    Model\Connection\MaxChannels,
16
    Model\Connection\MaxFrameSize,
17
    Exception\FrameChannelExceedAllowedChannelNumber,
18
    Exception\FrameExceedAllowedSize,
19
    Exception\UnexpectedFrame,
20
    Exception\NoFrameDetected
21
};
22
use Innmind\Socket\{
23
    Internet\Transport,
24
    Client\Internet as Socket
25
};
26
use Innmind\Stream\Select;
27
use Innmind\Url\{
28
    UrlInterface,
29
    Authority\NullUserInformation
30
};
31
use Innmind\TimeContinuum\ElapsedPeriod;
32
use Innmind\Immutable\Str;
33
34
final class Connection
35
{
36
    private $transport;
37
    private $authority;
38
    private $vhost;
39
    private $protocol;
40
    private $socket;
41
    private $timeout;
42
    private $select;
43
    private $read;
44
    private $opened = false;
45
    private $maxChannels;
46
    private $maxFrameSize;
47
    private $heartbeat;
48
49 17
    public function __construct(
50
        Transport $transport,
51
        UrlInterface $server,
52
        Protocol $protocol,
53
        ElapsedPeriod $timeout
54
    ) {
55 17
        $this->transport = $transport;
56 17
        $this->authority = $server->authority();
57 17
        $this->vhost = $server->path();
58 17
        $this->protocol = $protocol;
59 17
        $this->timeout = $timeout;
60 17
        $this->buildSocket();
61 17
        $this->read = new FrameReader;
62 17
        $this->maxChannels = new MaxChannels(0);
63 17
        $this->maxFrameSize = new MaxFrameSize(0);
64 17
        $this->heartbeat = $timeout;
65
66 17
        $this->open();
67 17
    }
68
69 15
    public function protocol(): Protocol
70
    {
71 15
        return $this->protocol;
72
    }
73
74 17
    public function send(Frame $frame): self
75
    {
76 17
        if (!$this->maxChannels->allows($frame->channel()->toInt())) {
77
            throw new FrameChannelExceedAllowedChannelNumber(
78
                $frame->channel(),
79
                $this->maxChannels
80
            );
81
        }
82
83 17
        $frame = (new Str((string) $frame))->toEncoding('ASCII');
84
85 17
        if (!$this->maxFrameSize->allows($frame->length())) {
86
            throw new FrameExceedAllowedSize(
87
                $frame->length(),
88
                $this->maxFrameSize
89
            );
90
        }
91
92 17
        $this->socket->write($frame);
93
94 17
        return $this;
95
    }
96
97 17
    public function wait(string ...$names): Frame
98
    {
99
        do {
100 17
            $streams = ($this->select)();
101 17
        } while (!$streams->get('read')->contains($this->socket));
102
103 17
        $frame = ($this->read)($this->socket, $this->protocol);
104
105 17
        foreach ($names as $name) {
106 17
            if ($this->protocol->method($name)->equals($frame->method())) {
107 17
                return $frame;
108
            }
109
        }
110
111 1
        throw new UnexpectedFrame($frame->method(), ...$names);
112
    }
113
114 1
    public function maxFrameSize(): MaxFrameSize
115
    {
116 1
        return $this->maxFrameSize;
117
    }
118
119 17
    public function close(): void
120
    {
121 17
        if (!$this->opened()) {
122 1
            return;
123
        }
124
125
        $this
126 17
            ->send($this->protocol->connection()->close(new Close))
127 17
            ->wait('connection.close-ok');
128 17
        $this->socket->close();
129 17
        $this->opened = false;
130 17
    }
131
132 17
    public function opened(): bool
133
    {
134 17
        return $this->opened && !$this->socket->closed();
135
    }
136
137 4
    public function __destruct()
138
    {
139 4
        $this->close();
140 4
    }
141
142 17
    private function buildSocket(): void
143
    {
144 17
        $this->socket = new Socket(
145 17
            $this->transport,
146 17
            $this->authority->withUserInformation(new NullUserInformation)
147
        );
148 17
        $this->select = (new Select($this->timeout))->forRead($this->socket);
149 17
    }
150
151 17
    private function open(): void
152
    {
153 17
        if ($this->opened()) {
154
            return;
155
        }
156
157 17
        $this->start();
158 17
        $this->handshake();
159 17
        $this->openVHost();
160
161 17
        $this->opened = true;
162 17
    }
163
164 17
    private function start(): void
165
    {
166 17
        $this->socket->write(
167 17
            new Str((string) $this->protocol->version())
168
        );
169
170
        try {
171 17
            $frame = $this->wait('connection.start');
0 ignored issues
show
Unused Code introduced by
$frame is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
172 1
        } catch (NoFrameDetected $e) {
173 1
            $content = $e->content()->toEncoding('ASCII');
174
175
            if (
176 1
                $content->length() !== 8 ||
177 1
                !$content->matches('/^AMQP/')
178
            ) {
179
                throw $e;
180
            }
181
182
            $version = $content
183 1
                ->substring(5, 8)
184 1
                ->chunk();
185 1
            $this->protocol->use(
186 1
                new Version(
187 1
                    UnsignedOctet::fromString($version->get(0))->original()->value(),
188 1
                    UnsignedOctet::fromString($version->get(1))->original()->value(),
189 1
                    UnsignedOctet::fromString($version->get(2))->original()->value()
190
                )
191
            );
192
            //socket rebuilt as the server close the connection on version mismatch
193 1
            $this->buildSocket();
194 1
            $this->start();
195
196 1
            return;
197
        }
198
199 17
        $this->send($this->protocol->connection()->startOk(
200 17
            new StartOk(
201 17
                $this->authority->userInformation()->user(),
202 17
                $this->authority->userInformation()->password()
203
            )
204
        ));
205 17
    }
206
207 17
    private function handshake(): void
208
    {
209 17
        $frame = $this->wait('connection.secure', 'connection.tune');
210
211 17
        if ($this->protocol->method('connection.secure')->equals($frame->method())) {
212
            $this->send($this->protocol->connection()->secureOk(
213
                new SecureOk(
214
                    $this->authority->userInformation()->user(),
215
                    $this->authority->userInformation()->password()
216
                )
217
            ));
218
            $frame = $this->wait('connection.tune');
219
        }
220
221 17
        $this->maxChannels = new MaxChannels(
222 17
            $frame->values()->get(0)->original()->value()
223
        );
224 17
        $this->maxFrameSize = new MaxFrameSize(
225 17
            $frame->values()->get(1)->original()->value()
226
        );
227 17
        $this->heartbeat = new ElapsedPeriod(
228 17
            $frame->values()->get(2)->original()->value()
229
        );
230 17
        $this->select = (new Select($this->heartbeat))->forRead($this->socket);
231 17
        $this->send($this->protocol->connection()->tuneOk(
232 17
            new TuneOk(
233 17
                $this->maxChannels,
234 17
                $this->maxFrameSize,
235 17
                $this->heartbeat
236
            )
237
        ));
238 17
    }
239
240 17
    private function openVHost(): void
241
    {
242
        $this
243 17
            ->send($this->protocol->connection()->open(
244 17
                new Open($this->vhost)
245
            ))
246 17
            ->wait('connection.open-ok');
247 17
    }
248
}
249