HtmlOutputFormatter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 107
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDecorated() 0 3 1
A isDecorated() 0 3 1
A setStyle() 0 3 1
A hasStyle() 0 3 1
A getStyle() 0 3 1
A format() 0 7 1
A replaceFormat() 0 13 3
1
<?php
2
3
/**
4
 * @author Victor Dubiniuk <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace Owncloud\Updater\Formatter;
24
25
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
26
use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
27
28
/**
29
 * Class HtmlOutputFormatter
30
 *
31
 * @package Owncloud\Updater\Formatter
32
 */
33
class HtmlOutputFormatter implements OutputFormatterInterface {
34
35
	const PATTERN = "/\[(([\d+];?)*)m(.*?)\[(([\d+];?)*)m/i";
36
37
	static private $styles = [
38
		'30' => 'color:rgba(0,0,0,1)',
39
		'31' => 'color:rgba(230,50,50,1)',
40
		'32' => 'color:rgba(50,230,50,1)',
41
		'33' => 'color:rgba(230,230,50,1)',
42
		'34' => 'color:rgba(50,50,230,1)',
43
		'35' => 'color:rgba(230,50,150,1)',
44
		'36' => 'color:rgba(50,230,230,1)',
45
		'37' => 'color:rgba(250,250,250,1)',
46
		'40' => 'color:rgba(0,0,0,1)',
47
		'41' => 'background-color:rgba(230,50,50,1)',
48
		'42' => 'background-color:rgba(50,230,50,1)',
49
		'43' => 'background-color:rgba(230,230,50,1)',
50
		'44' => 'background-color:rgba(50,50,230,1)',
51
		'45' => 'background-color:rgba(230,50,150,1)',
52
		'46' => 'background-color:rgba(50,230,230,1)',
53
		'47' => 'background-color:rgba(250,250,250,1)',
54
		'1' => 'font-weight:bold',
55
		'4' => 'text-decoration:underline',
56
		'8' => 'visibility:hidden',
57
	];
58
	private $formatter;
59
60
	/**
61
	 * HtmlOutputFormatter constructor.
62
	 *
63
	 * @param $formatter
64
	 */
65
	public function __construct($formatter){
66
		$this->formatter = $formatter;
67
	}
68
69
	/**
70
	 * @param bool $decorated
71
	 * @return mixed
72
	 */
73
	public function setDecorated($decorated){
74
		return $this->formatter->setDecorated($decorated);
75
	}
76
77
	/**
78
	 * @return mixed
79
	 */
80
	public function isDecorated(){
81
		return $this->formatter->isDecorated();
82
	}
83
84
	/**
85
	 * @param string $name
86
	 * @param OutputFormatterStyleInterface $style
87
	 * @return mixed
88
	 */
89
	public function setStyle($name, OutputFormatterStyleInterface $style){
90
		return $this->formatter->setStyle($name, $style);
91
	}
92
93
	/**
94
	 * @param string $name
95
	 * @return mixed
96
	 */
97
	public function hasStyle($name){
98
		return $this->formatter->hasStyle($name);
99
	}
100
101
	/**
102
	 * @param string $name
103
	 * @return mixed
104
	 */
105
	public function getStyle($name){
106
		return $this->formatter->getStyle($name);
107
	}
108
109
	/**
110
	 * @param string $message
111
	 * @return mixed
112
	 */
113
	public function format($message){
114
		$formatted = $this->formatter->format($message);
115
		$escaped = htmlspecialchars($formatted, ENT_QUOTES, 'UTF-8');
116
		$converted = preg_replace_callback(self::PATTERN, [$this, 'replaceFormat'], $escaped);
117
118
		return $converted;
119
	}
120
121
	/**
122
	 * @param $matches
123
	 * @return string
124
	 */
125
	protected function replaceFormat($matches){
126
		$text = $matches[3];
127
		$styles = explode(';', $matches[1]);
128
		$css = [];
129
130
		foreach ($styles as $style){
131
			if (isset(self::$styles[$style])){
132
				$css[] = self::$styles[$style];
133
			}
134
		}
135
136
		return sprintf('<span style="%s">%s</span>', implode(';', $css), $text);
137
	}
138
139
}
140