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 ( 9e28e0...0be46e )
by Cees-Jan
02:46
created

AbstractLogglyLogger   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.1%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 99
ccs 37
cts 42
cp 0.881
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
send() 0 1 ?
A log() 0 12 2
A format() 0 18 2
C processPlaceHolders() 0 23 8
A normalizeContext() 0 15 4
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 Psr\Log\LogLevel;
8
9
abstract class AbstractLogglyLogger extends AbstractLogger
10
{
11
    abstract protected function send(string $data);
12
13
    /**
14
     * Logging levels PSR-3 LogLevel enum
15
     *
16
     * @var array $levels Logging levels
17
     */
18
    const LOG_LEVELS = [
19
        LogLevel::DEBUG     => 'DEBUG',
20
        LogLevel::INFO      => 'INFO',
21
        LogLevel::NOTICE    => 'NOTICE',
22
        LogLevel::WARNING   => 'WARNING',
23
        LogLevel::ERROR     => 'ERROR',
24
        LogLevel::CRITICAL  => 'CRITICAL',
25
        LogLevel::ALERT     => 'ALERT',
26
        LogLevel::EMERGENCY => 'EMERGENCY',
27
    ];
28
29 26
    public function log($level, $message, array $context = [])
30
    {
31 26
        $levels = self::LOG_LEVELS;
32 26
        if (!isset($levels[$level])) {
33 2
            throw new InvalidArgumentException(
34 2
                'Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(self::LOG_LEVELS))
35
            );
36
        }
37
38 24
        $data = $this->format($level, $message, $context);
39 24
        $this->send($data);
40 24
    }
41
42 24
    protected function format($level, $message, array $context): string
43
    {
44 24
        $message = (string)$message;
45 24
        $context = $this->normalizeContext($context);
46 24
        $message = $this->processPlaceHolders($message, $context);
47 24
        $json = json_encode([
48 24
            'level'   => $level,
49 24
            'message' => $message,
50 24
            'level_message' => $level . ' ' . $message,
51 24
            'context' => $context,
52
        ]);
53
54 24
        if ($json === false) {
55
            throw new InvalidArgumentException(json_last_error_msg());
56
        }
57
58 24
        return $json;
59
    }
60
61
    /**
62
     * @param string $message
63
     * @param array $context
64
     * @return string
65
     *
66
     * Method copied from: https://github.com/Seldaek/monolog/blob/6e6586257d9fb231bf039563632e626cdef594e5/src/Monolog/Processor/PsrLogMessageProcessor.php
67
     */
68 24
    private function processPlaceHolders(string $message, array $context): string
69
    {
70 24
        if (false === strpos($message, '{')) {
71 6
            return $message;
72
        }
73
74 18
        $replacements = array();
75 18
        foreach ($context as $key => $val) {
76 18
            if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, '__toString'))) {
77 18
                $replacements['{'.$key.'}'] = $val;
78 18
                continue;
79
            }
80
81
            if (is_object($val)) {
82
                $replacements['{'.$key.'}'] = '[object '.get_class($val).']';
83
                continue;
84
            }
85
86
            $replacements['{'.$key.'}'] = '['.gettype($val).']';
87
        }
88
89 18
        return strtr($message, $replacements);
90
    }
91
92 24
    private function normalizeContext(array $context): array
93
    {
94 24
        foreach ($context as $index => $value) {
95 22
            if (is_array($value)) {
96 2
                $context[$index] = $this->normalizeContext($value);
97 2
                continue;
98
            }
99
100 22
            if (is_resource($value)) {
101 2
                $context[$index] = sprintf('[resource] (%s)', get_resource_type($value));
102 22
                continue;
103
            }
104
        }
105 24
        return $context;
106
    }
107
}
108