Test Failed
Push — master ( 089ede...0c94d3 )
by Michał
14:02 queued 07:53
created

DefaultMessage::dateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleLog\Message;
6
7
use Psr\Log\InvalidArgumentException;
8
9
class DefaultMessage implements MessageInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $message = '';
15
16
    /**
17
     * @param string|array|object $message
18
     * @param array $context
19
     * @return $this
20 22
     * @throws InvalidArgumentException
21
     */
22 22
    public function createMessage($message, array $context): MessageInterface
23 19
    {
24 19
        $this->buildMessage($message)
25
            ->wrapMessage()
0 ignored issues
show
Bug introduced by
The method wrapMessage() does not exist on SimpleLog\Message\MessageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to SimpleLog\Message\MessageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            ->/** @scrutinizer ignore-call */ wrapMessage()
Loading history...
26 19
            ->buildContext($context);
27
28
        return $this;
29
    }
30
31
    /**
32
     * @param array $context
33 23
     * @return $this
34
     */
35 23
    protected function buildContext(array $context): MessageInterface
36
    {
37 23
        $replace = [];
38 4
39 4
        foreach ($context as $key => $val) {
40 4
            if (!\is_array($val) && (!\is_object($val) || \method_exists($val, '__toString'))) {
41 23
                $replace['{' . $key . '}'] = $val;
42
            }
43 23
        }
44
45 23
        $this->message = \strtr($this->message, $replace);
46
47
        return $this;
48
    }
49
50
    /**
51 14
     * @return $this
52
     */
53 14
    protected function wrapMessage(): MessageInterface
54
    {
55 14
        $this->message = $this->dateTime()
56 14
            . PHP_EOL
57 14
            . $this->message
58
            . '-----------------------------------------------------------'
59 14
            . PHP_EOL;
60
61
        return $this;
62
    }
63
64
    /**
65
     * recurrent function to convert array into message
66
     *
67
     * @param string|array|object $message
68
     * @param string $indent
69
     * @return $this
70 22
     * @throws InvalidArgumentException
71
     */
72 22
    protected function buildMessage($message, string $indent = ''): MessageInterface
73 22
    {
74 6
        switch (true) {
75 6
            case \is_array($message):
76
                $this->buildArrayMessage($message, $indent);
77 16
                break;
78 16
79 13
            case \method_exists($message, '__toString'):
80 13
            case \is_string($message):
81
                $this->message = $message . PHP_EOL;
82 3
                break;
83 3
84
            default:
85 3
                throw new InvalidArgumentException(
86
                    'Incorrect message type. Must be string, array or object with __toString method.'
87 3
                );
88
        }
89 19
90
        return $this;
91
    }
92
93
    /**
94
     * @param array $message
95
     * @param string $indent
96 6
     */
97
    protected function buildArrayMessage(array $message, string $indent): void
98 6
    {
99 6
        foreach ($message as $key => $value) {
100 6
            $this->processMessage($key, $value, $indent);
101 6
        }
102
    }
103
104
    /**
105
     * @param string|int $key
106
     * @param mixed $value
107
     * @param string $indent
108
     * @return $this
109 4
     */
110
    protected function processMessage($key, $value, string $indent): MessageInterface
111 4
    {
112
        $row = '- ';
113 4
114 4
        if (!\is_int($key)) {
115 4
            $row .= $key . ':';
116
        }
117 4
118 2
        if (\is_array($value)) {
119 2
            $indent .= '    ';
120 2
            $this->message .= $row . PHP_EOL;
121 2
            $this->buildMessage($value, $indent);
122 4
        } else {
123
            $this->message .= $indent
124 4
                . $row
125 4
                . (!$key ? '' : ' ')
126 4
                . $value
127
                . PHP_EOL;
128
        }
129 4
130
        return $this;
131
    }
132
133
    /**
134
     * @return array
135 19
     */
136
    protected function dateTimeSplit(): array
137 19
    {
138
        $dateTime = new \DateTime();
139
        $date = $dateTime->format(self::DATE_FORMAT);
140
        $time = $dateTime->format(self::TIME_FORMAT);
141
142
        return [$date, $time];
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    protected function dateTime(): string
149
    {
150
        $dateTime = new \DateTime();
151
152
        return $dateTime->format(self::DATE_TIME_FORMAT);
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getMessage(): string
159
    {
160
        return $this->message;
161
    }
162
}
163