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 ( 64d161...1ef8bd )
by Cees-Jan
04:30
created

AbstractLogglyLogger::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 3
crap 2.003
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' => $level . ' ' . $message,
50 24
            'context' => $context,
51
        ]);
52
53 24
        if ($json === false) {
54
            throw new InvalidArgumentException(json_last_error_msg());
55
        }
56
57 24
        return $json;
58
    }
59
60
    /**
61
     * @param string $message
62
     * @param array $context
63
     * @return string
64
     *
65
     * Method copied from: https://github.com/Seldaek/monolog/blob/6e6586257d9fb231bf039563632e626cdef594e5/src/Monolog/Processor/PsrLogMessageProcessor.php
66
     */
67 24
    private function processPlaceHolders(string $message, array $context): string
68
    {
69 24
        if (false === strpos($message, '{')) {
70 6
            return $message;
71
        }
72
73 18
        $replacements = [];
74 18
        foreach ($context as $key => $value) {
75 18
            $replacements['{'.$key.'}'] = $this->formatValue($value);
76
        }
77
78 18
        return strtr($message, $replacements);
79
    }
80
81 18
    private function formatValue($value)
82
    {
83 18
        if (is_null($value) || is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
84 18
            return $value;
85
        }
86
87
        if (is_object($value)) {
88
            return '[object '.get_class($value).']';
89
        }
90
91
        return '['.gettype($value).']';
92
    }
93
94 24
    private function normalizeContext(array $context): array
95
    {
96 24
        foreach ($context as $index => $value) {
97 22
            if (is_array($value)) {
98 2
                $context[$index] = $this->normalizeContext($value);
99 2
                continue;
100
            }
101
102 22
            if (is_resource($value)) {
103 2
                $context[$index] = sprintf('[resource] (%s)', get_resource_type($value));
104 22
                continue;
105
            }
106
        }
107 24
        return $context;
108
    }
109
}
110