Passed
Push — develop ( 5fc28f...93d639 )
by Michał
16:38
created

DefaultMessage::buildContext()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 8.8571
ccs 9
cts 9
cp 1
crap 5
1
<?php
2
3
namespace SimpleLog\Message;
4
5
use Psr\Log\InvalidArgumentException;
6
7
class DefaultMessage implements MessageInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $message = '';
13
14
    /**
15
     * @param string|array|object $message
16
     * @param array $context
17
     * @return $this
18
     * @throws \Psr\Log\InvalidArgumentException
19
     */
20 19
    public function createMessage($message, array $context)
21
    {
22 19
        $this->buildMessage($message)
23 16
            ->wrapMessage()
24 16
            ->buildContext($context);
25
26 16
        return $this;
27
    }
28
29
    /**
30
     * @param array $context
31
     * @return $this
32
     */
33 20
    protected function buildContext(array $context)
34
    {
35 20
        $replace = [];
36
37 20
        foreach ($context as $key => $val) {
38 4
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
39 4
                $replace['{' . $key . '}'] = $val;
40 4
            }
41 20
        }
42
43 20
        $this->message = strtr($this->message, $replace);
44
45 20
        return $this;
46
    }
47
48
    /**
49
     * @return $this
50
     */
51 12
    protected function wrapMessage()
52
    {
53 12
        $this->message = strftime('%d-%m-%Y - %H:%M:%S', time())
54
            . PHP_EOL
55 12
            . $this->message
56 12
            . '-----------------------------------------------------------'
57 12
            . PHP_EOL;
58
59 12
        return $this;
60
    }
61
62
    /**
63
     * recurrent function to convert array into message
64
     *
65
     * @param string|array|object $message
66
     * @param string $indent
67
     * @return $this
68
     * @throws \Psr\Log\InvalidArgumentException
69
     */
70 19
    protected function buildMessage($message, $indent = '')
71
    {
72 19
        switch (true) {
73 19
            case is_array($message):
74 6
                $this->buildArrayMessage($message, $indent);
75 6
                break;
76
77 13
            case method_exists($message, '__toString'):
78 13
            case is_string($message):
79 10
                $this->message = $message . PHP_EOL;
1 ignored issue
show
Bug introduced by
Are you sure $message of type object|string can be used in concatenation? ( Ignorable by Annotation )

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

79
                $this->message = /** @scrutinizer ignore-type */ $message . PHP_EOL;
Loading history...
80 10
                break;
81
82 3
            default:
83 3
                throw new InvalidArgumentException(
84
                    'Incorrect message type. Must be string, array or object with __toString method.'
85 3
                );
86
                break;
87 3
        }
88
89 16
        return $this;
90
    }
91
92
    /**
93
     * @param array $message
94
     * @param string $indent
95
     */
96 6
    protected function buildArrayMessage(array $message, $indent)
97
    {
98 6
        foreach ($message as $key => $value) {
99 6
            $this->processMessage($key, $value, $indent);
100 6
        }
101 6
    }
102
103
    /**
104
     * @param string|int $key
105
     * @param mixed $value
106
     * @param string $indent
107
     * @return $this
108
     */
109 4
    protected function processMessage($key, $value, $indent)
110
    {
111 4
        $row = '- ';
112
113 4
        if (!is_int($key)) {
114 4
            $row .= $key . ':';
115 4
        }
116
117 4
        if (is_array($value)) {
118 2
            $indent .= '    ';
119 2
            $this->message .= $row . PHP_EOL;
120 2
            $this->buildMessage($value, $indent);
121 2
        } else {
122 4
            $this->message .= $indent
123
                . $row
124 4
                . (!$key ? '' : ' ')
125 4
                . $value
126 4
                . PHP_EOL;
127
        }
128
129 4
        return $this;
130
    }
131
132
    /**
133
     * @return string
134
     */
135 16
    public function getMessage()
136
    {
137 16
        return $this->message;
138
    }
139
}
140