Completed
Push — master ( aff38e...dfd432 )
by Alexander
03:13
created

WorkingCopyResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 113
ccs 31
cts 31
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getWorkingCopyUrl() 0 10 2
A getWorkingCopyPath() 0 16 4
B resolvePath() 0 20 5
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\Repository;
12
13
14
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
15
16
class WorkingCopyResolver
17
{
18
19
	/**
20
	 * Repository connector.
21
	 *
22
	 * @var Connector
23
	 */
24
	private $_repositoryConnector;
25
26
	/**
27
	 * Working copy paths.
28
	 *
29
	 * @var array
30
	 */
31
	private $_workingCopyPaths = array();
32
33
	/**
34
	 * Mapping between working copy paths and their urls.
35
	 *
36
	 * @var array
37
	 */
38
	private $_workingCopyUrlMapping = array();
39
40
	/**
41
	 * Mapping between path to working copy specified by user to one, that is actually used.
42
	 *
43
	 * @var array
44
	 */
45
	private $_resolvedPathMapping = array();
46
47
	/**
48
	 * Creates working copy resolver instance.
49
	 *
50
	 * @param Connector $repository_connector Repository connector.
51
	 */
52 7
	public function __construct(Connector $repository_connector)
53
	{
54 7
		$this->_repositoryConnector = $repository_connector;
55 7
	}
56
57
	/**
58
	 * Returns URL to the working copy.
59
	 *
60
	 * @param string $raw_path Raw path.
61
	 *
62
	 * @return string
63
	 */
64 2
	public function getWorkingCopyUrl($raw_path)
65
	{
66 2
		$wc_path = $this->getWorkingCopyPath($raw_path);
67
68 2
		if ( !isset($this->_workingCopyUrlMapping[$wc_path]) ) {
69 2
			$this->_workingCopyUrlMapping[$wc_path] = $this->_repositoryConnector->getWorkingCopyUrl($wc_path);
70 2
		}
71
72 2
		return $this->_workingCopyUrlMapping[$wc_path];
73
	}
74
75
	/**
76
	 * Return working copy path.
77
	 *
78
	 * @param string $raw_path Raw path.
79
	 *
80
	 * @return string
81
	 * @throws \LogicException When folder isn't a working copy.
82
	 */
83 6
	public function getWorkingCopyPath($raw_path)
84
	{
85 6
		$path = $this->resolvePath($raw_path);
86
87 6
		if ( !in_array($path, $this->_workingCopyPaths) ) {
88 6
			if ( !$this->_repositoryConnector->isUrl($path)
89 6
				&& !$this->_repositoryConnector->isWorkingCopy($path)
90 6
			) {
91 1
				throw new \LogicException('The "' . $path . '" isn\'t a working copy.');
92
			}
93
94 5
			$this->_workingCopyPaths[] = $path;
95 5
		}
96
97 5
		return $path;
98
	}
99
100
	/**
101
	 * Returns absolute path to working copy from given raw path.
102
	 *
103
	 * @param string $raw_path Raw path.
104
	 *
105
	 * @return string
106
	 */
107 6
	protected function resolvePath($raw_path)
108
	{
109 6
		$path = $raw_path;
110
111 6
		if ( !isset($this->_resolvedPathMapping[$path]) ) {
112 6
			if ( $this->_repositoryConnector->isUrl($path) ) {
113 2
				$this->_resolvedPathMapping[$path] = $this->_repositoryConnector->removeCredentials($path);
114 2
			}
115
			else {
116
				// When deleted path is specified, then use it's existing parent path.
117 4
				if ( !file_exists($path) && file_exists(dirname($path)) ) {
118 1
					$path = dirname($path);
119 1
				}
120
121 4
				$this->_resolvedPathMapping[$path] = realpath($path);
122
			}
123 6
		}
124
125 6
		return $this->_resolvedPathMapping[$path];
126
	}
127
128
}
129