Helper::getArrayValueByKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 6
1
<?php
2
3
/**
4
 * ownCloud - Documents App
5
 *
6
 * @author Victor Dubiniuk
7
 * @copyright 2013 Victor Dubiniuk [email protected]
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later.
11
 */
12
13
namespace OCA\Documents;
14
15
class Helper {
16
17
	const APP_ID = 'documents';
18
19 1
	public static function getNewFileName($view, $path, $prepend = ' '){
20 1
		$fileNum = 1;
21
22 1
		while ($view->file_exists($path)){
23
			$fileNum += 1;
24
			$path = preg_replace('/(\.odt|' . $prepend . '\(\d+\)\.odt)$/', $prepend . '(' . $fileNum . ').odt', $path);
25
		};
26
27 1
		return $path;
28
	}
29
	
30
	public static function getArrayValueByKey($array, $key, $default=''){
31
		if (array_key_exists($key, $array)){
32
			return $array[$key];
33
		}
34
		return $default;
35
	}
36
37
	public static function isVersionsEnabled(){
38
		return \OCP\App::isEnabled('files_versions');
39
	}
40
41
	public static function getRandomColor(){
42
		$str = dechex(floor(rand(0, 16777215)));
43
		return '#' . str_pad($str, 6, "0", STR_PAD_LEFT);
44
	}
45
46
	/**
47
	 * @param string $name
48
	 * @return string
49
	 */
50
	public static function getMemberColor($name){
51
		$hash = md5($name);
52
		$maxRange = hexdec('ffffffffffffffffffffffffffffffff');
53
		$hue = hexdec($hash) / $maxRange * 256;
54
		return '#' . self::convertHSLToRGB($hue, 90, 60);
55
	}
56
57
	/**
58
	 * @param integer $iH
59
	 * @param integer $iS
60
	 * @param integer $iV
61
	 * @return string
62
	 */
63
	protected static function convertHSLToRGB($iH, $iS, $iV){
64
		if ($iH < 0){
65
			$iH = 0;   // Hue:
66
		}
67
		if ($iH > 360){
68
			$iH = 360; //   0-360
69
		}
70
		if ($iS < 0){
71
			$iS = 0;   // Saturation:
72
		}
73
		if ($iS > 100){
74
			$iS = 100; //   0-100
75
		}
76
		if ($iV < 0){
77
			$iV = 0;   // Lightness:
78
		}
79
		if ($iV > 100){
80
			$iV = 100; //   0-100
81
		}
82
83
		$dS = $iS / 100.0; // Saturation: 0.0-1.0
84
		$dV = $iV / 100.0; // Lightness:  0.0-1.0
85
		$dC = $dV * $dS;   // Chroma:     0.0-1.0
86
		$dH = $iH / 60.0;  // H-Prime:    0.0-6.0
87
		$dT = $dH;	   // Temp variable
88
89
		while ($dT >= 2.0)
90
			$dT -= 2.0; // php modulus does not work with float
91
		$dX = $dC * (1 - abs($dT - 1));	 // as used in the Wikipedia link
92
93
		switch ($dH){
94 View Code Duplication
			case($dH >= 0.0 && $dH < 1.0):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
				$dR = $dC;
96
				$dG = $dX;
97
				$dB = 0.0;
98
				break;
99 View Code Duplication
			case($dH >= 1.0 && $dH < 2.0):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
				$dR = $dX;
101
				$dG = $dC;
102
				$dB = 0.0;
103
				break;
104 View Code Duplication
			case($dH >= 2.0 && $dH < 3.0):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
				$dR = 0.0;
106
				$dG = $dC;
107
				$dB = $dX;
108
				break;
109 View Code Duplication
			case($dH >= 3.0 && $dH < 4.0):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
				$dR = 0.0;
111
				$dG = $dX;
112
				$dB = $dC;
113
				break;
114 View Code Duplication
			case($dH >= 4.0 && $dH < 5.0):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
				$dR = $dX;
116
				$dG = 0.0;
117
				$dB = $dC;
118
				break;
119 View Code Duplication
			case($dH >= 5.0 && $dH < 6.0):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
				$dR = $dC;
121
				$dG = 0.0;
122
				$dB = $dX;
123
				break;
124
			default:
125
				$dR = 0.0;
126
				$dG = 0.0;
127
				$dB = 0.0;
128
				break;
129
		}
130
131
		$dM = $dV - $dC;
132
		$dR += $dM;
133
		$dG += $dM;
134
		$dB += $dM;
135
		$dR *= 255;
136
		$dG *= 255;
137
		$dB *= 255;
138
139
		$dR = str_pad(dechex(round($dR)), 2, "0", STR_PAD_LEFT);
140
		$dG = str_pad(dechex(round($dG)), 2, "0", STR_PAD_LEFT);
141
		$dB = str_pad(dechex(round($dB)), 2, "0", STR_PAD_LEFT);
142
		return $dR.$dG.$dB;
143
	}
144
	
145
	public static function findOpenOffice(){
146
		$config = \OC::$server->getConfig();
147
		$cmd = '';
148
		if (is_string($config->getSystemValue('preview_libreoffice_path', null))){
149
			$cmd = $config->getSystemValue('preview_libreoffice_path', null);
150
		}
151
152
		$whichLibreOffice = shell_exec('which libreoffice');
153
		if ($cmd === '' && !empty($whichLibreOffice)){
154
			$cmd = 'libreoffice';
155
		}
156
157
		$whichOpenOffice = shell_exec('which openoffice');
158
		if ($cmd === '' && !empty($whichOpenOffice)){
159
			$cmd = 'openoffice';
160
		}
161
		
162
		if (empty($cmd)){
163
			\OC::$server->getLogger()->warning(
164
				'Pure configuration issue. Missing open office binary that is mandatory for conversion.',
165
				['app' => 'documents']
166
				
167
			);
168
			\OC::$server->getLogger()->debug(
169
				'If openoffice or libreoffice is already installed please specify the path to it using preview_libreoffice_path config. Refer to admin manual for details.',
170
				['app' => 'documents']
171
				
172
			);
173
			throw new \RuntimeException('Missing open office binary that is mandatory for conversion.');
174
		}
175
		
176
		return $cmd;
177
	}
178
179
}
180