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

AbstractFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 36
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A interpolate() 0 12 4
A getFormattedDate() 0 6 1
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 function is_array;
15
16
/**
17
 * Class AbstractFormatter
18
 *
19
 * @package Phalcon\Logger\Formatter
20
 */
21
abstract class AbstractFormatter implements FormatterInterface
22
{
23
    /**
24
     * Interpolates context values into the message placeholders
25
     *
26
     * @see http://www.php-fig.org/psr/psr-3/ Section 1.2 Message
27
     *
28
     * @param string     $message
29
     * @param array|null $context
30
     *
31
     * @return string
32
     */
33 30
    public function interpolate(string $message, $context = null): string
34
    {
35 30
        if (is_array($context) && count($context) > 0) {
36 4
            $replace = [];
37 4
            foreach ($context as $key => $value) {
38 4
                $replace["{" . $key . "}"] = $value;
39
            }
40
41 4
            return strtr($message, $replace);
42
        }
43
44 26
        return $message;
45
    }
46
    /**
47
     * Returns the date formatted for the logger.
48
     * @todo Not using the set time from the Item since we have interface
49
     * misalignment which will break semver This will change in the future
50
     */
51 27
    protected function getFormattedDate(): string
52
    {
53 27
        $timezone = date_default_timezone_get();
54 27
        $date = new \DateTimeImmutable("now", new \DateTimeZone($timezone));
55
56 27
        return $date->format($this->dateFormat);
57
    }
58
}
59