Passed
Push — master ( 7063d6...9e7aeb )
by Matias
04:08
created

CompressionService::bunzip2()   B

Complexity

Conditions 11
Paths 7

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 13.594

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 17
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 25
ccs 13
cts 18
cp 0.7221
crap 13.594
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @copyright Copyright (c) 2019-2023 Matias De lellis <[email protected]>
6
 *
7
 * @author Matias De lellis <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\FaceRecognition\Service;
27
28
class CompressionService {
29
30
	/**
31
	 * Uncompressing the file with the bzip2-extension
32
	 *
33
	 * @param string $inputFile
34
	 * @param string $outputFile
35
	 *
36
	 * @throws \Exception
37
	 *
38
	 * @return void
39
	 */
40 1
	public function bunzip2(string $inputFile, string $outputFile): void {
41 1
		if (!file_exists ($inputFile) || !is_readable ($inputFile))
42
			throw new \Exception('The file ' . $inputFile . ' not exists or is not readable');
43
44 1
		if ((!file_exists($outputFile) && !is_writeable(dirname($outputFile))) ||
45 1
		    (file_exists($outputFile) && !is_writable($outputFile)))
46
			throw new \Exception('The file ' . $outputFile . ' exists or is not writable');
47
48 1
		$in_file = bzopen ($inputFile, "r");
49 1
		$out_file = fopen ($outputFile, "w");
50
51 1
		if ($out_file === false)
52
			throw new \Exception('Could not open the file to write: ' . $outputFile);
53
54 1
		while ($buffer = bzread ($in_file, 4096)) {
55 1
			if($buffer === false)
56
				throw new \Exception('Read problem: ' . bzerrstr($in_file));
57 1
			if(bzerrno($in_file) !== 0)
58
				throw new \Exception('Compression problem: '. bzerrstr($in_file));
59
60 1
			fwrite ($out_file, $buffer, 4096);
61
		}
62
63 1
		bzclose ($in_file);
64 1
		fclose ($out_file);
65
	}
66
67
}
68