Completed
Push — master ( adc8b1...5898c8 )
by Bjørn
34:12
created

BufferLogger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 17.65%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 50
ccs 6
cts 34
cp 0.1765
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 3 1
A getHtml() 0 19 5
A ln() 0 3 1
A getText() 0 15 4
1
<?php
2
3
namespace WebPConvert\Loggers;
4
5
use WebPConvert\Loggers\BaseLogger;
6
7
class BufferLogger extends BaseLogger
8
{
9
    public $entries = array();
10
11 3
    public function log($msg, $style = '')
12
    {
13 3
        $this->entries[] = [$msg, $style];
14 3
    }
15
16 3
    public function ln()
17
    {
18 3
        $this->entries[] = '';
19 3
    }
20
21
    public function getHtml()
22
    {
23
        $html = '';
24
        foreach ($this->entries as $entry) {
25
            if ($entry == '') {
26
                $html .= '<br>';
27
            } else {
28
                list($msg, $style) = $entry;
29
                $msg = htmlspecialchars($msg);
30
                if ($style == 'bold') {
31
                    $html .= '<b>' . $msg . '</b>';
32
                } elseif ($style == 'italic') {
33
                    $html .= '<i>' . $msg . '</i>';
34
                } else {
35
                    $html .= $msg;
36
                }
37
            }
38
        }
39
        return $html;
40
    }
41
42
    public function getText($newLineChar = ' ')
43
    {
44
        $text = '';
45
        foreach ($this->entries as $entry) {
46
            if ($entry == '') {  // empty string means new line
47
                if (substr($text, -2) != '.' . $newLineChar) {
48
                    $text .= '.' . $newLineChar;
49
                }
50
            } else {
51
                list($msg, $style) = $entry;
52
                $text .= $msg;
53
            }
54
        }
55
56
        return $text;
57
    }
58
}
59