MonologFormatterAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Yep\WorkflowLogger;
4
5
use Monolog\Formatter\FormatterInterface;
6
7
/**
8
 * Class MonologFormatterAdapter
9
 *
10
 * @package Yep\WorkflowLogger\Formatter
11
 * @author  Martin Zeman (Zemistr) <[email protected]>
12
 */
13
class MonologFormatterAdapter implements FormatterInterface
14
{
15
    /**
16
     * @var mixed
17
     */
18
    protected $newLevel;
19
20
    /**
21
     * @var FormatterInterface
22
     */
23
    protected $formatter;
24
25 2
    public function __construct(
26
      FormatterInterface $formatter,
27
      $newLevel = Logger::DEBUG
28
    ) {
29 2
        $this->formatter = $formatter;
30 2
        $this->newLevel = $newLevel;
31 2
    }
32
33
    /**
34
     * Formats a log record.
35
     *
36
     * @param  array $record A record to format
37
     * @return mixed The formatted record
38
     */
39 1
    public function format(array $record)
40
    {
41 1
        $record = $this->fixRecord($record);
42
43 1
        return $this->formatter->format($record);
44
    }
45
46
    /**
47
     * @param array $record
48
     * @return array
49
     */
50 2
    protected function fixRecord(array $record)
51
    {
52 2
        if (isset($record['level']) && $record['level'] === Logger::WORKFLOW) {
53 2
            $record['level'] = $this->newLevel;
54
        }
55
56 2
        return $record;
57
    }
58
59
    /**
60
     * Formats a set of log records.
61
     *
62
     * @param  array $records A set of records to format
63
     * @return mixed The formatted set of records
64
     */
65 1
    public function formatBatch(array $records)
66
    {
67 1
        $records = array_map([$this, 'fixRecord'], $records);
68
69 1
        return $this->formatter->formatBatch($records);
70
    }
71
}
72