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
|
|
|
private function __construct(LoopInterface $loop, Client $httpClient, string $token, float $timeout) |
51
|
|
|
{ |
52
|
|
|
$this->loop = $loop; |
53
|
|
|
$this->httpClient = $httpClient; |
54
|
|
|
$this->token = $token; |
55
|
|
|
$this->timeout = $timeout; |
56
|
|
|
} |
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
|
|
|
public static function createFromHttpClient( |
66
|
|
|
LoopInterface $loop, |
67
|
|
|
Client $httpClient, |
68
|
|
|
string $token, |
69
|
|
|
float $timeout = 5.3 |
70
|
|
|
): self { |
71
|
|
|
return new self($loop, $httpClient, $token, $timeout); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function send(string $data): void |
75
|
|
|
{ |
76
|
|
|
$dataLength = \strlen($data . self::LF); |
77
|
|
|
if ($dataLength > self::MAX_LINE_LENGTH) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($this->bufferSize + $dataLength > self::MAX_BODY_LENGTH) { |
82
|
|
|
$this->sendBulk(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->buffer[] = $data; |
86
|
|
|
$this->bufferSize += $dataLength; |
87
|
|
|
$this->ensureTimer(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function ensureTimer(): void |
91
|
|
|
{ |
92
|
|
|
if ($this->timer instanceof TimerInterface) { |
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->timer = $this->loop->addTimer($this->timeout, function (): void { |
|
|
|
|
97
|
|
|
$this->timer = null; |
98
|
|
|
$this->sendBulk(); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function sendBulk(): void |
103
|
|
|
{ |
104
|
|
|
if ($this->timer instanceof TimerInterface) { |
|
|
|
|
105
|
|
|
$this->timer->cancel(); |
106
|
|
|
$this->timer = null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$data = \implode(self::LF, $this->buffer); |
110
|
|
|
|
111
|
|
|
$this->buffer = []; |
112
|
|
|
$this->bufferSize = 0; |
113
|
|
|
|
114
|
|
|
$this->httpClient->request( |
115
|
|
|
'POST', |
116
|
|
|
'https://logs-01.loggly.com/bulk/' . $this->token, |
117
|
|
|
[ |
118
|
|
|
'Content-Type' => 'application/json', |
119
|
|
|
'Content-Length' => \strlen($data), |
120
|
|
|
], |
121
|
|
|
'1.1' |
122
|
|
|
)->end($data); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.