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 — master ( 5eaa40...39ed7e )
by Cees-Jan
01:30
created

LogglyBulkLogger::send()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
nc 3
cc 3
eloc 9
nop 1
crap 3.072
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\PSR3\Loggly;
4
5
use React\EventLoop\LoopInterface;
6
use React\EventLoop\Timer\TimerInterface;
7
use React\HttpClient\Client;
8
9
final class LogglyBulkLogger extends AbstractLogglyLogger
10
{
11
    const LF = "\r\n";
12
    const MAX_BODY_LENGTH = 5242880;
13
    const MAX_LINE_LENGTH = 1048576;
14
15
    /**
16
     * @var LoopInterface
17
     */
18
    private $loop;
19
20
    /**
21
     * @var Client
22
     */
23
    private $httpClient;
24
25
    /**
26
     * @var string
27
     */
28
    private $token;
29
30
    /**
31
     * @var float
32
     */
33
    private $timeout;
34
35
    /**
36
     * @var string[]
37
     */
38
    private $buffer = [];
39
40
    /**
41
     * @var int
42
     */
43
    private $bufferSize = 0;
44
45
    /**
46
     * @var TimerInterface
47
     */
48
    private $timer;
49
50 12
    private function __construct(LoopInterface $loop, Client $httpClient, string $token, float $timeout)
51
    {
52 12
        $this->loop = $loop;
53 12
        $this->httpClient = $httpClient;
54 12
        $this->token = $token;
55 12
        $this->timeout = $timeout;
56 12
    }
57
58
    public static function create(LoopInterface $loop, string $token, float $timeout = 5.3): self
59
    {
60
        $httpClient = self::createHttpClient($loop);
61
62
        return new self($loop, $httpClient, $token, $timeout);
63
    }
64
65 12
    public static function createFromHttpClient(
66
        LoopInterface $loop,
67
        Client $httpClient,
68
        string $token,
69
        float $timeout = 5.3
70
    ): self {
71 12
        return new self($loop, $httpClient, $token, $timeout);
72
    }
73
74 12
    protected function send(string $data)
75
    {
76 12
        $dataLength = strlen($data . self::LF);
77 12
        if ($dataLength > self::MAX_LINE_LENGTH) {
78
            return;
79
        }
80
81 12
        if ($this->bufferSize + $dataLength > self::MAX_BODY_LENGTH) {
82
            $this->sendBulk();
83
        }
84
85 12
        $this->buffer[] = $data;
86 12
        $this->bufferSize += $dataLength;
87 12
        $this->ensureTimer();
88 12
    }
89
90 12
    private function ensureTimer()
91
    {
92 12
        if ($this->timer instanceof TimerInterface) {
93
            return;
94
        }
95
96 12
        $this->timer = $this->loop->addTimer($this->timeout, function () {
97 12
            $this->timer = null;
98 12
            $this->sendBulk();
99 12
        });
100 12
    }
101
102 12
    private function sendBulk()
103
    {
104 12
        if ($this->timer instanceof TimerInterface) {
105
            $this->timer->cancel();
106
            $this->timer = null;
107
        }
108
109 12
        $data = implode(self::LF, $this->buffer);
110
111 12
        $this->buffer = [];
112 12
        $this->bufferSize = 0;
113
114 12
        $this->httpClient->request(
115 12
            'POST',
116 12
            'https://logs-01.loggly.com/bulk/' . $this->token,
117
            [
118 12
                'Content-Type' => 'application/json',
119 12
                'Content-Length' => strlen($data),
120
            ],
121 12
            '1.1'
122 12
        )->end($data);
123 12
    }
124
}
125