Passed
Push — master ( 7349d0...85c6cc )
by Nikolaos
04:18
created

Line::format()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 8
nop 1
dl 0
loc 32
ccs 14
cts 15
cp 0.9333
crap 4.0047
rs 9.7998
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 Phalcon\Logger\Item;
15
16
use function date;
17
use function is_array;
18
use function str_replace;
19
use function strpos;
20
21
/**
22
 * Phalcon\Logger\Formatter\Line
23
 *
24
 * Formats messages using an one-line string
25
 *
26
 * @property string $dateFormat
27
 * @property string $format
28
 */
29
class Line extends AbstractFormatter
30
{
31
    /**
32
     * Default date format
33
     *
34
     * @var string
35
     */
36
    protected $dateFormat;
37
38
    /**
39
     * Format applied to each message
40
     *
41
     * @var string
42
     */
43
    protected $format;
44
45
    /**
46
     * Line constructor.
47
     *
48
     * @param string $format
49
     * @param string $dateFormat
50
     */
51 36
    public function __construct(
52
        string $format = "[%date%][%type%] %message%",
53
        string $dateFormat = "c"
54
    ) {
55 36
        $this->format     = $format;
56 36
        $this->dateFormat = $dateFormat;
57 36
    }
58
59
    /**
60
     * @return string
61
     */
62 3
    public function getDateFormat(): string
63
    {
64 3
        return $this->dateFormat;
65
    }
66
67
    /**
68
     * @return string
69
     */
70 2
    public function getFormat(): string
71
    {
72 2
        return $this->format;
73
    }
74
75
    /**
76
     * Applies a format to a message before sent it to the internal log
77
     *
78
     * @param Item $item
79
     *
80
     * @return string
81
     */
82 23
    public function format(Item $item): string
83
    {
84 23
        $format = $this->format;
85
86
        /**
87
         * Check if the format has the %date% placeholder
88
         */
89 23
        if (false !== strpos($format, "%date%")) {
90 23
            $format = str_replace(
91 23
                "%date%",
92 23
                $this->getFormattedDate(),
93 23
                $format
94
            );
95
        }
96
97
        /**
98
         * Check if the format has the %type% placeholder
99
         */
100 23
        if (false !== strpos($format, "%type%")) {
101 23
            $format = str_replace("%type%", $item->getName(), $format);
102
        }
103
104 23
        $format = str_replace("%message%", $item->getMessage(), $format);
105
106 23
        if (is_array($item->getContext())) {
0 ignored issues
show
introduced by
The condition is_array($item->getContext()) is always true.
Loading history...
107 23
            return $this->interpolate(
108 23
                $format,
109 23
                $item->getContext()
110
            );
111
        }
112
113
        return $format;
114
    }
115
116
    /**
117
     * @param string $dateFormat
118
     *
119
     * @return Line
120
     */
121 1
    public function setDateFormat(string $dateFormat): Line
122
    {
123 1
        $this->dateFormat = $dateFormat;
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param string $format
130
     *
131
     * @return Line
132
     */
133 1
    public function setFormat(string $format): Line
134
    {
135 1
        $this->format = $format;
136
137 1
        return $this;
138
    }
139
}
140