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 ( dc595a...ae55c1 )
by Baptiste
02:15
created

Connection   C

Complexity

Total Complexity 24

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 29

Test Coverage

Coverage 88.39%

Importance

Changes 0
Metric Value
dl 0
loc 210
c 0
b 0
f 0
wmc 24
lcom 1
cbo 29
ccs 99
cts 112
cp 0.8839
rs 5

12 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
B start() 0 42 4
B handshake() 0 32 2
A openVHost() 0 8 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
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 7
    public function __construct(
50
        Transport $transport,
51
        UrlInterface $server,
52
        Protocol $protocol,
53
        ElapsedPeriod $timeout
54
    ) {
55 7
        $this->transport = $transport;
56 7
        $this->authority = $server->authority();
57 7
        $this->vhost = $server->path();
58 7
        $this->protocol = $protocol;
59 7
        $this->timeout = $timeout;
60 7
        $this->buildSocket();
61 7
        $this->read = new FrameReader;
62 7
        $this->maxChannels = new MaxChannels(0);
63 7
        $this->maxFrameSize = new MaxFrameSize(0);
64 7
        $this->heartbeat = $timeout;
65
66 7
        $this->open();
67 7
    }
68
69 5
    public function protocol(): Protocol
70
    {
71 5
        return $this->protocol;
72
    }
73
74 7
    public function send(Frame $frame): self
75
    {
76 7
        if (!$this->maxChannels->allows($frame->channel()->toInt())) {
77
            throw new FrameChannelExceedAllowedChannelNumber(
78
                $frame->channel(),
79
                $this->maxChannels
80
            );
81
        }
82
83 7
        $frame = (new Str((string) $frame))->toEncoding('ASCII');
84
85 7
        if (!$this->maxFrameSize->allows($frame->length())) {
86
            throw new FrameExceedAllowedSize(
87
                $frame->length(),
88
                $this->maxFrameSize
89
            );
90
        }
91
92 7
        $this->socket->write($frame);
93
94 7
        return $this;
95
    }
96
97 7
    public function wait(string ...$names): Frame
98
    {
99
        do {
100 7
            $streams = ($this->select)();
101 7
        } while (!$streams->get('read')->contains($this->socket));
102
103 7
        $frame = ($this->read)($this->socket, $this->protocol);
104
105 7
        foreach ($names as $name) {
106 7
            if ($this->protocol->method($name)->equals($frame->method())) {
107 7
                return $frame;
108
            }
109
        }
110
111 1
        throw new UnexpectedFrame($frame->method(), ...$names);
112
    }
113
114 7
    public function close(): void
115
    {
116 7
        if (!$this->opened()) {
117 1
            return;
118
        }
119
120
        $this
121 7
            ->send($this->protocol->connection()->close(new Close))
122 7
            ->wait('connection.close-ok');
123 7
        $this->socket->close();
124 7
        $this->opened = false;
125 7
    }
126
127 7
    public function opened(): bool
128
    {
129 7
        return $this->opened && !$this->socket->closed();
130
    }
131
132 4
    public function __destruct()
133
    {
134 4
        $this->close();
135 4
    }
136
137 7
    private function buildSocket(): void
138
    {
139 7
        $this->socket = new Socket(
140 7
            $this->transport,
141 7
            $this->authority->withUserInformation(new NullUserInformation)
142
        );
143 7
        $this->select = (new Select($this->timeout))->forRead($this->socket);
144 7
    }
145
146 7
    private function open(): void
147
    {
148 7
        if ($this->opened()) {
149
            return;
150
        }
151
152 7
        $this->start();
153 7
        $this->handshake();
154 7
        $this->openVHost();
155
156 7
        $this->opened = true;
157 7
    }
158
159 7
    private function start(): void
160
    {
161 7
        $this->socket->write(
162 7
            new Str((string) $this->protocol->version())
163
        );
164
165
        try {
166 7
            $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...
167 1
        } catch (NoFrameDetected $e) {
168 1
            $content = $e->content()->toEncoding('ASCII');
169
170
            if (
171 1
                $content->length() !== 8 ||
172 1
                !$content->matches('/^AMQP/')
173
            ) {
174
                throw $e;
175
            }
176
177
            $version = $content
178 1
                ->substring(5, 8)
179 1
                ->chunk();
180 1
            $this->protocol->use(
181 1
                new Version(
182 1
                    UnsignedOctet::fromString($version->get(0))->original()->value(),
183 1
                    UnsignedOctet::fromString($version->get(1))->original()->value(),
184 1
                    UnsignedOctet::fromString($version->get(2))->original()->value()
185
                )
186
            );
187
            //socket rebuilt as the server close the connection on version mismatch
188 1
            $this->buildSocket();
189 1
            $this->start();
190
191 1
            return;
192
        }
193
194 7
        $this->send($this->protocol->connection()->startOk(
195 7
            new StartOk(
196 7
                $this->authority->userInformation()->user(),
197 7
                $this->authority->userInformation()->password()
198
            )
199
        ));
200 7
    }
201
202 7
    private function handshake(): void
203
    {
204 7
        $frame = $this->wait('connection.secure', 'connection.tune');
205
206 7
        if ($this->protocol->method('connection.secure')->equals($frame->method())) {
207
            $this->send($this->protocol->connection()->secureOk(
208
                new SecureOk(
209
                    $this->authority->userInformation()->user(),
210
                    $this->authority->userInformation()->password()
211
                )
212
            ));
213
            $frame = $this->wait('connection.tune');
214
        }
215
216 7
        $this->maxChannels = new MaxChannels(
217 7
            $frame->values()->get(0)->original()->value()
218
        );
219 7
        $this->maxFrameSize = new MaxFrameSize(
220 7
            $frame->values()->get(1)->original()->value()
221
        );
222 7
        $this->heartbeat = new ElapsedPeriod(
223 7
            $frame->values()->get(2)->original()->value()
224
        );
225 7
        $this->select = (new Select($this->heartbeat))->forRead($this->socket);
226 7
        $this->send($this->protocol->connection()->tuneOk(
227 7
            new TuneOk(
228 7
                $this->maxChannels,
229 7
                $this->maxFrameSize,
230 7
                $this->heartbeat
231
            )
232
        ));
233 7
    }
234
235 7
    private function openVHost(): void
236
    {
237
        $this
238 7
            ->send($this->protocol->connection()->open(
239 7
                new Open($this->vhost)
240
            ))
241 7
            ->wait('connection.open-ok');
242 7
    }
243
}
244