Completed
Push — master ( 759854...e46a01 )
by Martin
13:10 queued 03:41
created

StandardFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B format() 0 26 2
A __construct() 0 3 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
    public function format(Record $record)
31
    {
32
        $contextString = '';
33
34
        if ($context = $record->getContext()) {
35
            $contextString = strtr(
36
              self::CONTEXT_FORMAT,
37
              [
38
                '%context%' => $this->dumper->dump($context),
39
              ]
40
            );
41
        }
42
43
        $string = strtr(
44
          self::FORMAT,
45
          [
46
            '%datetime%' => $record->getDatetime()->format(
47
              static::DATETIME_FORMAT
48
            ),
49
            '%level%' => $record->getLevel(),
50
            '%message%' => $record->getMessage(),
51
            '%contextPlaceholder%' => $contextString,
52
          ]
53
        );
54
55
        return $string;
56
    }
57
}
58