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 ( 0be46e...64d161 )
by Cees-Jan
03:22
created

AbstractLogglyLogger::processPlaceHolders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
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 = [];
75 18
        foreach ($context as $key => $value) {
76 18
            $replacements['{'.$key.'}'] = $this->formatValue($value);
77
        }
78
79 18
        return strtr($message, $replacements);
80
    }
81
82 18
    private function formatValue($value)
83
    {
84 18
        if (is_null($value) || is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
85 18
            return $value;
86
        }
87
88
        if (is_object($value)) {
89
            return '[object '.get_class($value).']';
90
        }
91
92
        return '['.gettype($value).']';
93
    }
94
95 24
    private function normalizeContext(array $context): array
96
    {
97 24
        foreach ($context as $index => $value) {
98 22
            if (is_array($value)) {
99 2
                $context[$index] = $this->normalizeContext($value);
100 2
                continue;
101
            }
102
103 22
            if (is_resource($value)) {
104 2
                $context[$index] = sprintf('[resource] (%s)', get_resource_type($value));
105 22
                continue;
106
            }
107
        }
108 24
        return $context;
109
    }
110
}
111