LogDecorator::translateLogLevel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Amesplash\CampaignMonitorLog;
5
6
use Psr\Log\LogLevel;
7
use Psr\Log\LoggerTrait;
8
use Psr\Log\LoggerInterface;
9
use Amesplash\CampaignMonitorLog\Exception\InvalidArgumentException;
10
11
final class LogDecorator implements LoggerInterface
12
{
13
    use LoggerTrait;
14
15
    /**
16
     * @var LoggerInterface
17
     */
18
    private $log;
19
20
    /**
21
     * @var array
22
     */
23
    private $context = [];
24
25
    /**
26
     * @var string
27
     */
28
    private $logLevel;
0 ignored issues
show
introduced by
The private property $logLevel is not used, and could be removed.
Loading history...
29
30
    /**
31
     * CS_REST_LOG_VERBOSE = 1000,
32
     * CS_REST_LOG_WARNING = 500,
33
     * CS_REST_LOG_ERROR = 250
34
     * As defined by CS_Rest_Log class.
35
     *
36
     * @var array
37
     */
38
    private $campaignMonitorLogLevels = [
39
        1000 => LogLevel::DEBUG,
40
        500 => LogLevel::WARNING,
41
        250 => LogLevel::ERROR,
42
    ];
43
44
    /**
45
     * New Log Adapter instance.
46
     *
47
     * @param LoggerInterface $log
48
     * @param array           $context The default context
49
     */
50 7
    public function __construct(LoggerInterface $log, array $context = [])
51
    {
52 7
        $this->log = $log;
53 7
        $this->context = $context;
54 7
    }
55
56
    /**
57
     * {@inheritdoc}
58
    */
59 2
    public function log($level, $message, array $context = array())
60
    {
61 2
        $this->log->log($level, $message, $this->context + $context);
62 2
    }
63
64
    /**
65
     * The CS_Rest_Log log message method.
66
     * I know it sucks but nothing we can do!
67
     *
68
     * @var string $message
69
     * @var string $module
70
     * @var int $level CS_Rest_Log level
71
     * @return void
72
    */
73 7
    public function log_message($message, $module, $level) : void
74
    {
75 7
        $level = $this->translateLogLevel($level);
76 2
        $this->log($level, $message, ['module' => $module]);
77 2
    }
78
79
    /**
80
     * Translates CM_REST_Log levels to PSR-3 Log levels
81
     *
82
     * @param  mixed $level
83
     * @return string
84
     * @throws InvalidArgumentException
85
     */
86 7
    private function translateLogLevel($level) : string
87
    {
88 7
        if (!array_key_exists($level, $this->campaignMonitorLogLevels)) {
89 5
            throw InvalidArgumentException::fromInvalidLogLevel($level);
90
        }
91
92 2
        return $this->campaignMonitorLogLevels[$level];
93
    }
94
}
95