Passed
Push — master ( 70c101...2ac467 )
by Martin
01:56
created

StandardFormatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Yep\WorkflowLogger\Formatter;
4
5
use Yep\WorkflowLogger\ContextDumper\DumperInterface;
6
use Yep\WorkflowLogger\Record;
7
8
/**
9
 * Class StandardFormatter
10
 *
11
 * @package Yep\WorkflowLogger\Formatter
12
 * @author  Martin Zeman (Zemistr) <[email protected]>
13
 */
14
class StandardFormatter implements FormatterInterface
15
{
16
    const FORMAT = "[%datetime%] %level%: %message%\n%contextPlaceholder%";
17
    const CONTEXT_FORMAT = "Context:\n%context%\n";
18
    const DATETIME_FORMAT = 'Y-m-d H:i:s.u';
19
20
    /**
21
     * @var DumperInterface
22
     */
23
    protected $dumper;
24
25
    public function __construct(DumperInterface $dumper)
26
    {
27
        $this->dumper = $dumper;
28
    }
29
30
    protected function prepareContextPart(Record $record)
31
    {
32
        $context = $record->getContext();
33
34
        if (empty($context)) {
35
            return '';
36
        }
37
38
        return strtr(
39
          static::CONTEXT_FORMAT,
40
          [
41
            '%context%' => $this->dumper->dump($context),
42
          ]
43
        );
44
    }
45
46
    public function format(Record $record)
47
    {
48
        $string = strtr(
49
          static::FORMAT,
50
          [
51
            '%datetime%' => $record->getDatetime()->format(
52
              static::DATETIME_FORMAT
53
            ),
54
            '%level%' => $record->getLevel(),
55
            '%message%' => $record->getMessage(),
56
            '%contextPlaceholder%' => $this->prepareContextPart($record),
57
          ]
58
        );
59
60
        return $string;
61
    }
62
}
63