Passed
Push — master ( 2afdeb...b846a2 )
by Nikolaos
06:23 queued 03:51
created

Line::__construct()   A

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
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * For the full copyright and license information, please view the LICENSE.md
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Phalcon\Logger\Formatter;
13
14
use Exception;
15
use Phalcon\Logger\Item;
16
17
/**
18
 * Phalcon\Logger\Formatter\Line
19
 *
20
 * Formats messages using an one-line string
21
 *
22
 * @property string $dateFormat
23
 * @property string $format
24
 */
25
class Line extends AbstractFormatter
26
{
27
    /**
28
     * Default date format
29
     *
30
     * @var string
31
     */
32
    protected $dateFormat;
33
34
    /**
35
     * Format applied to each message
36
     *
37
     * @var string
38
     */
39
    protected $format;
40
41
    /**
42
     * Line constructor.
43
     *
44
     * @param string $format
45
     * @param string $dateFormat
46
     */
47 37
    public function __construct(
48
        string $format = "[{date}][{type}] {message}",
49
        string $dateFormat = "c"
50
    ) {
51 37
        $this->format     = $format;
52 37
        $this->dateFormat = $dateFormat;
53 37
    }
54
55
    /**
56
     * @return string
57
     */
58 3
    public function getDateFormat(): string
59
    {
60 3
        return $this->dateFormat;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 2
    public function getFormat(): string
67
    {
68 2
        return $this->format;
69
    }
70
71
    /**
72
     * Applies a format to a message before sent it to the internal log
73
     *
74
     * @param Item $item
75
     *
76
     * @return string
77
     * @throws Exception
78
     */
79 24
    public function format(Item $item): string
80
    {
81
        $context = [
82 24
            'date'    => $this->getFormattedDate(),
83 24
            'type'    => $item->getName(),
84 24
            'message' => $this->interpolate(
85 24
                $item->getMessage(),
86 24
                $item->getContext()
87
            ),
88
        ];
89 24
        $context = array_merge($item->getContext(), $context);
90
91 24
        return $this->interpolate($this->format, $context);
92
    }
93
94
    /**
95
     * @param string $dateFormat
96
     *
97
     * @return Line
98
     */
99 1
    public function setDateFormat(string $dateFormat): Line
100
    {
101 1
        $this->dateFormat = $dateFormat;
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * @param string $format
108
     *
109
     * @return Line
110
     */
111 2
    public function setFormat(string $format): Line
112
    {
113 2
        $this->format = $format;
114
115 2
        return $this;
116
    }
117
}
118