InMemoryLogger::getMessages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\Logger;
6
7
use Psr\Log\AbstractLogger;
8
9
/**
10
 * @internal
11
 */
12
class InMemoryLogger extends AbstractLogger
13
{
14
    private $messages;
15
16 1
    public function log($level, $message, array $context = [])
17
    {
18 1
        $this->messages[] = $this->interpolate($message, $context);
19 1
    }
20
21 1
    public function getMessages(): array
22
    {
23 1
        return $this->messages;
24
    }
25
26 1
    private function interpolate($message, array $context = [])
27
    {
28
        // build a replacement array with braces around the context keys
29 1
        $replace = [];
30 1
        foreach ($context as $key => $val) {
31
            // check that the value can be casted to string
32 1
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
33 1
                $replace['{'.$key.'}'] = $val;
34
            }
35
        }
36
37
        // interpolate replacement values into the message and return
38 1
        return strtr($message, $replace);
39
    }
40
}
41