Passed
Push — master ( dd20c7...9b85cd )
by Allan
02:33
created

Logger::writeNotice()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 14
rs 10
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches;
7
8
/**
9
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
10
 */
11
class Logger
12
{
13
    const TYPE_NONE = 'none';
14
    
15
    /**
16
     * @var \Composer\IO\IOInterface $appIO
17
     */
18
    private $appIO;
19
    
20
    /**
21
     * @var array
22
     */
23
    private $indentationStack = array();
24
    
25
    /**
26
     * @var int
27
     */
28
    private $muteDepth = 0;
29
    
30
    /**
31
     * @param \Composer\IO\IOInterface $appIO
32
     */
33
    public function __construct(
34
        \Composer\IO\IOInterface $appIO
35
    ) {
36
        $this->appIO = $appIO;
37
    }
38
39
    /**
40
     * @return \Composer\IO\IOInterface|\Composer\IO\ConsoleIO
41
     */
42
    public function getOutputInstance()
43
    {
44
        return $this->appIO;
45
    }
46
47
    public function isMuted()
48
    {
49
        return (bool)$this->muteDepth;
50
    }
51
    
52
    public function mute()
53
    {
54
        return $this->muteDepth++;
55
    }
56
57
    public function unMute($muteDepth = null)
58
    {
59
        $this->muteDepth = $muteDepth
60
            ? $muteDepth
61
            : max(--$this->muteDepth, 0);
62
    }
63
    
64
    public function writeRaw($message, array $args = array())
65
    {
66
        if ($this->muteDepth) {
67
            return;
68
        }
69
        
70
        $prefix = $this->getIndentationString();
71
72
        $lines = array_map(function ($line) use ($prefix) {
73
            return $prefix . $line;
74
        }, explode(PHP_EOL, $message));
75
        
76
        $this->appIO->write(
77
            vsprintf(implode(PHP_EOL, $lines), $args)
78
        );
79
    }
80
    
81
    public function write($type, $message, array $args = array())
82
    {
83
        $this->writeRaw($this->createTag($type, $message), $args);
84
    }
85
86
    public function writeNotice($type, $message, array $args = array())
87
    {
88
        if (!is_array($message)) {
89
            $message = array($message);
90
        }
91
92
        $length = 0;
93
        
94
        foreach ($message as $item) {
95
            $length = min(80, max(strlen($item), $length));
96
        }
97
        
98
        foreach ($message as $item) {
99
            $this->write($type, str_pad($item, $length, ' '), $args);
100
        }
101
    }
102
    
103
    public function writeVerbose($type, $message, array $args = array())
104
    {
105
        if (!$this->appIO->isVerbose()) {
106
            return;
107
        }
108
        
109
        $this->write($type, $message, $args);
110
    }
111
    
112
    public function writeException(\Exception $exception)
113
    {
114
        if (!$this->appIO->isVerbose()) {
115
            return;
116
        }
117
        
118
        $this->write('error', trim($exception->getMessage(), PHP_EOL . ' '));
119
        $this->write('', trim($exception->getTraceAsString(), PHP_EOL . ' '));
120
    }
121
122
    public function writeNewLine()
123
    {
124
        if ($this->muteDepth) {
125
            return;
126
        }
127
        
128
        $this->appIO->write('');
129
    }
130
    
131
    private function createTag($type, $contents)
132
    {
133
        if (!$type || $type == \Vaimo\ComposerPatches\Logger::TYPE_NONE) {
134
            return $contents;
135
        }
136
        
137
        return '<' . $type . '>' . $contents . '</' . $type . '>';
138
    }
139
    
140
    private function getIndentationString()
141
    {
142
        return str_pad('', count($this->indentationStack) * 2, ' ') . end($this->indentationStack);
143
    }
144
145
    public function writeIndentation()
146
    {
147
        if ($this->muteDepth) {
148
            return;
149
        }
150
        
151
        $this->appIO->write(
152
            $this->getIndentationString(),
153
            false
154
        );
155
    }
156
    
157
    public function reset($index = 0)
158
    {
159
        $this->indentationStack = array_slice($this->indentationStack, 0, $index);
160
    }
161
162
    public function push($prefix = '')
163
    {
164
        $index = count($this->indentationStack);
165
        
166
        $this->indentationStack[] = $prefix ? ($prefix . ' ') : '';
167
        
168
        return $index;
169
    }
170
171
    public function pop()
172
    {
173
        array_pop($this->indentationStack);
174
    }
175
}
176