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 ( 55046d...5bc21e )
by Cees-Jan
02:05
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\Dns\Resolver\Factory as ResolverFactory;
6
use React\EventLoop\LoopInterface;
7
use React\EventLoop\Timer\TimerInterface;
8
use React\HttpClient\Client;
9
use React\HttpClient\Factory as HttpClientFactory;
10
11
final class LogglyBulkLogger extends AbstractLogglyLogger
12
{
13
    const LF = "\r\n";
14
    const MAX_BODY_LENGTH = 5242880;
15
    const MAX_LINE_LENGTH = 1048576;
16
17
    /**
18
     * @var LoopInterface
19
     */
20
    private $loop;
21
22
    /**
23
     * @var Client
24
     */
25
    private $httpClient;
26
27
    /**
28
     * @var string
29
     */
30
    private $token;
31
32
    /**
33
     * @var float
34
     */
35
    private $timeout;
36
37
    /**
38
     * @var string[]
39
     */
40
    private $buffer = [];
41
42
    /**
43
     * @var int
44
     */
45
    private $bufferSize = 0;
46
47
    /**
48
     * @var TimerInterface
49
     */
50
    private $timer;
51
52 View Code Duplication
    public static function create(LoopInterface $loop, string $token, float $timeout = 5): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $resolverFactory = new ResolverFactory();
55
        $resolver = $resolverFactory->create('8.8.8.8', $loop);
56
57
        $factory = new HttpClientFactory();
58
        $httpClient = $factory->create($loop, $resolver);
59
60
        return new self($loop, $httpClient, $token, $timeout);
61
    }
62
63 12
    public static function createFromHttpClient(
64
        LoopInterface $loop,
65
        Client $httpClient,
66
        string $token,
67
        float $timeout = 5
68
    ): self {
69 12
        return new self($loop, $httpClient, $token, $timeout);
70
    }
71
72 12
    private function __construct(LoopInterface $loop, Client $httpClient, string $token, float $timeout)
73
    {
74 12
        $this->loop = $loop;
75 12
        $this->httpClient = $httpClient;
76 12
        $this->token = $token;
77 12
        $this->timeout = $timeout;
78 12
    }
79
80 12
    protected function send(string $data)
81
    {
82 12
        $dataLength = strlen($data . self::LF);
83 12
        if ($dataLength > self::MAX_LINE_LENGTH) {
84
            return;
85
        }
86
87 12
        if ($this->bufferSize + $dataLength > self::MAX_BODY_LENGTH) {
88
            $this->sendBulk();
89
        }
90
91 12
        $this->buffer[] = $data;
92 12
        $this->bufferSize += $dataLength;
93 12
        $this->ensureTimer();
94 12
    }
95
96 12
    private function ensureTimer()
97
    {
98 12
        if ($this->timer instanceof TimerInterface) {
99
            return;
100
        }
101
102 12
        $this->timer = $this->loop->addTimer($this->timeout, function () {
103 12
            $this->timer = null;
104 12
            $this->sendBulk();
105 12
        });
106 12
    }
107
108 12
    private function sendBulk()
109
    {
110 12
        if ($this->timer instanceof TimerInterface) {
111
            $this->timer->cancel();
112
            $this->timer = null;
113
        }
114
115 12
        $data = implode(self::LF, $this->buffer);
116
117 12
        $this->buffer = [];
118 12
        $this->bufferSize = 0;
119
120 12
        $this->httpClient->request(
121 12
            'POST',
122 12
            'https://logs-01.loggly.com/bulk/' . $this->token,
123
            [
124 12
                'Content-Type' => 'application/json',
125 12
                'Content-Length' => strlen($data),
126
            ],
127 12
            '1.1'
128 12
        )->end($data);
129 12
    }
130
}
131