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.

LogglyBulkLogger::sendBulk()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

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