Failed Conditions
Push — master ( dd969d...42da22 )
by Alexander
10:30
created

RepositoryCommandException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 25
c 0
b 0
f 0
dl 0
loc 64
ccs 22
cts 22
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseErrorOutput() 0 30 6
A __construct() 0 8 1
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Exception;
12
13
14
use ConsoleHelpers\ConsoleKit\Exception\AbstractException;
15
16
/**
17
 * Exception for representing failed Subversion command execution result.
18
 *
19
 * @link http://docs.sharpsvn.net/current/html/T_SharpSvn_SvnErrorCode.htm
20
 * @link https://subversion.apache.org/docs/api/1.8/svn__error__codes_8h_source.html
21
 * @link https://subversion.apache.org/docs/api/1.8/svn__error__codes_8h.html
22
 */
23
class RepositoryCommandException extends AbstractException
24
{
25
26
	const SVN_ERR_WC_UPGRADE_REQUIRED = 155036;
27
28
	const SVN_ERR_WC_NOT_WORKING_COPY = 155007;
29
30
	const SVN_ERR_FS_NOT_FOUND = 160013;
31
32
	const SVN_ERR_BASE = 200000;
33
34
	/**
35
	 * Creates instance of repository command execution exception.
36
	 *
37
	 * @param string $command Command.
38
	 * @param string $stderr  Output from STDERR.
39
	 */
40 15
	public function __construct($command, $stderr)
41
	{
42 15
		$command = \preg_replace('/(--username|--password) ([^\s]+)/', '$1 ********', $command);
43
44 15
		list ($code, $message) = $this->parseErrorOutput($stderr);
45 15
		$message = 'Command:' . PHP_EOL . $command . PHP_EOL . 'Error #' . $code . ':' . PHP_EOL . $message;
46
47 15
		parent::__construct($message, $code);
48 15
	}
49
50
	/**
51
	 * Parses error output.
52
	 *
53
	 * @param string $error_output Error output.
54
	 *
55
	 * @return array
56
	 */
57 15
	protected function parseErrorOutput($error_output)
58
	{
59 15
		$error_code = 0;
60 15
		$error_message = '';
61
62 15
		$lines = array_filter(explode(PHP_EOL, $error_output));
63
64 15
		foreach ( $lines as $line ) {
65 15
			if ( preg_match('/^svn\: E([\d]+)\: (.*)$/', $line, $regs) ) {
66
				// SVN 1.7+.
67 7
				$error_code = (int)$regs[1]; // Typecast, because HHVM's "Exception" class doesn't do this.
68 7
				$error_message .= PHP_EOL . $regs[2];
69
			}
70 9
			elseif ( preg_match('/^svn\: (.*)$/', $line, $regs) ) {
71
				// SVN 1.6-.
72 7
				if ( preg_match('/^\'(.*)\' is not a working copy$/', $regs[1]) ) {
73 1
					$error_code = self::SVN_ERR_WC_NOT_WORKING_COPY;
74
				}
75 6
				elseif ( preg_match('/^URL \'(.*)\' non-existent in that revision$/', $regs[1]) ) {
76 1
					$error_code = self::SVN_ERR_FS_NOT_FOUND;
77
				}
78
79 7
				$error_message .= PHP_EOL . $regs[1];
80
			}
81
			else {
82 3
				$error_message .= ' ' . $line;
83
			}
84
		}
85
86 15
		return array($error_code, trim($error_message));
87
	}
88
89
}
90