Passed
Push — logger ( 576894 )
by Arnaud
11:17
created

PrintLogger::log()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 18
rs 9.9332
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Logger;
12
13
use Cecil\Builder;
14
use Psr\Log\AbstractLogger;
15
use Psr\Log\InvalidArgumentException;
16
use Psr\Log\LogLevel;
17
18
class PrintLogger extends AbstractLogger
19
{
20
    private $verbosityLevelMap = [
21
        LogLevel::EMERGENCY => Builder::VERBOSITY_NORMAL,
22
        LogLevel::ALERT     => Builder::VERBOSITY_NORMAL,
23
        LogLevel::CRITICAL  => Builder::VERBOSITY_NORMAL,
24
        LogLevel::ERROR     => Builder::VERBOSITY_NORMAL,
25
        LogLevel::WARNING   => Builder::VERBOSITY_NORMAL,
26
        LogLevel::NOTICE    => Builder::VERBOSITY_VERBOSE,
27
        LogLevel::INFO      => Builder::VERBOSITY_VERBOSE,
28
        LogLevel::DEBUG     => Builder::VERBOSITY_DEBUG,
29
    ];
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @return void
35
     */
36
    public function log($level, $message, array $context = [])
37
    {
38
        if (!isset($this->verbosityLevelMap[$level])) {
39
            throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
40
        }
41
42
        if (array_key_exists('progress', $context)) {
43
            printf(
44
                " (%s/%s) %s\n",
45
                $context['progress'][0],
46
                $context['progress'][1],
47
                $this->interpolate($message, $context)
48
            );
49
50
            return;
51
        }
52
53
        printf("%s\n", $this->interpolate($message, $context));
54
    }
55
56
    /**
57
     * Interpolates context values into the message placeholders.
58
     *
59
     * @author PHP Framework Interoperability Group
60
     */
61
    private function interpolate(string $message, array $context): string
62
    {
63
        if (false === strpos($message, '{')) {
64
            return $message;
65
        }
66
67
        $replacements = [];
68
        foreach ($context as $key => $val) {
69
            if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
70
                $replacements["{{$key}}"] = $val;
71
            } elseif ($val instanceof \DateTimeInterface) {
72
                $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
73
            } elseif (\is_object($val)) {
74
                $replacements["{{$key}}"] = '[object '.\get_class($val).']';
75
            } else {
76
                $replacements["{{$key}}"] = '['.\gettype($val).']';
77
            }
78
        }
79
80
        return strtr($message, $replacements);
81
    }
82
}
83