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 ( bdb42f...a6fda5 )
by Cees-Jan
03:27
created

AbstractLogglyLogger::createHttpClient()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 13
cp 0.8462
rs 9.536
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.0327
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\PSR3\Loggly;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\InvalidArgumentException;
7
use React\Dns\Config\Config;
8
use React\Dns\Resolver\Factory as ResolverFactory;
9
use React\EventLoop\LoopInterface;
10
use React\HttpClient\Client;
11
use React\HttpClient\Factory as HttpClientFactory;
12
use React\Socket\Connector;
13
use function WyriHaximus\PSR3\checkCorrectLogLevel;
14
use function WyriHaximus\PSR3\normalizeContext;
15
use function WyriHaximus\PSR3\processPlaceHolders;
16
17
abstract class AbstractLogglyLogger extends AbstractLogger
18
{
19 24
    public function log($level, $message, array $context = []): void
20
    {
21 24
        checkCorrectLogLevel($level);
22 22
        $this->send(
23 22
            $this->format($level, $message, $context)
24
        );
25 22
    }
26
27
    abstract protected function send(string $data);
28
29 22
    protected function format($level, $message, array $context): string
30
    {
31 22
        $message = (string)$message;
32 22
        $context = normalizeContext($context);
33 22
        $message = processPlaceHolders($message, $context);
34 22
        $json = \json_encode([
35 22
            'level'   => $level,
36 22
            'message' => $level . ' ' . $message,
37 22
            'context' => $context,
38
        ]);
39
40 22
        if ($json === false) {
41
            throw new InvalidArgumentException(\json_last_error_msg());
42
        }
43
44 22
        return $json;
45
    }
46
47 4
    protected static function createHttpClient(LoopInterface $loop): Client
48
    {
49 4
        $config = Config::loadSystemConfigBlocking();
50 4
        $server = $config->nameservers ? \reset($config->nameservers) : '1.1.1.1';
51
52 4
        $resolverFactory = new ResolverFactory();
53 4
        $resolver = $resolverFactory->createCached($server, $loop);
54
55 4
        if (\class_exists(HttpClientFactory::class)) {
56
            $factory = new HttpClientFactory();
57
58
            return $factory->create($loop, $resolver);
59
        }
60
61 4
        return new Client(
62 4
            $loop,
63 4
            new Connector(
64 4
                $loop,
65
                [
66 4
                    'dns' => $resolver,
67
                ]
68
            )
69
        );
70
    }
71
}
72