Completed
Push — master ( a673ca...8720a3 )
by Alexander
02:27
created

PathsRevisionLogPlugin   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 20
c 4
b 0
f 1
lcom 1
cbo 1
dl 0
loc 174
ccs 62
cts 62
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
B parse() 0 26 5
B find() 0 29 6
A getRevisionsData() 0 14 3
A getCollectedData() 0 7 1
A setCollectedData() 0 5 1
A getCacheInvalidator() 0 4 1
A getLastRevision() 0 10 2
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\RevisionLog;
12
13
14
class PathsRevisionLogPlugin extends AbstractRevisionLogPlugin
15
{
16
	const CACHE_FORMAT_VERSION = 1;
17
18
	/**
19
	 * Paths affected by specific revision.
20
	 *
21
	 * @var array
22
	 */
23
	private $_revisionPaths = array();
24
25
	/**
26
	 * Revisions affecting a specific path.
27
	 *
28
	 * @var array
29
	 */
30
	private $_pathRevisions = array();
31
32
	/**
33
	 * Returns plugin name.
34
	 *
35
	 * @return string
36
	 */
37 3
	public function getName()
38
	{
39 3
		return 'paths';
40
	}
41
42
	/**
43
	 * Parse log entries.
44
	 *
45
	 * @param \SimpleXMLElement $log Log.
46
	 *
47
	 * @return void
48
	 */
49 1
	public function parse(\SimpleXMLElement $log)
50
	{
51 1
		foreach ( $log->logentry as $log_entry ) {
52 1
			$revision = (int)$log_entry['revision'];
53 1
			$this->_revisionPaths[$revision] = array();
54
55 1
			foreach ( $log_entry->paths->path as $path_node ) {
56
				/** @var \SimpleXMLElement $path_node */
57 1
				$path = (string)$path_node;
58
59 1
				if ( !isset($this->_pathRevisions[$path]) ) {
60 1
					$this->_pathRevisions[$path] = array();
61 1
				}
62
63 1
				$this->_pathRevisions[$path][] = $revision;
64
65 1
				$path_data = array('path' => $path);
66
67 1
				foreach ( $path_node->attributes() as $attribute_name => $attribute_value ) {
68 1
					$path_data[$attribute_name] = (string)$attribute_value;
69 1
				}
70
71 1
				$this->_revisionPaths[$revision][] = $path_data;
72 1
			}
73 1
		}
74 1
	}
75
76
	/**
77
	 * Find revisions by collected data.
78
	 *
79
	 * @param array $criteria Criteria.
80
	 *
81
	 * @return array
82
	 */
83 5
	public function find(array $criteria)
84
	{
85 5
		if ( reset($criteria) === '' ) {
86
			// Include revisions from all paths.
87 1
			$path_revisions = $this->_revisionPaths;
88 1
		}
89
		else {
90
			// Include revisions from given sub-path only.
91 4
			$path_revisions = array();
92
93 4
			foreach ( $criteria as $path ) {
94 3
				$path_length = strlen($path);
95
96 3
				foreach ( $this->_pathRevisions as $test_path => $revisions ) {
97
					// FIXME: Fast, but does sub-match in inside a folder and "tags/stable" matches also "tags/stable2".
98 3
					if ( substr($test_path, 0, $path_length) == $path ) {
99 2
						foreach ( $revisions as $revision ) {
100 2
							$path_revisions[$revision] = true;
101 2
						}
102 2
					}
103 3
				}
104 4
			}
105
		}
106
107 5
		$path_revisions = array_keys($path_revisions);
108 5
		sort($path_revisions, SORT_NUMERIC);
109
110 5
		return $path_revisions;
111
	}
112
113
	/**
114
	 * Returns information about revisions.
115
	 *
116
	 * @param array $revisions Revisions.
117
	 *
118
	 * @return array
119
	 */
120 2
	public function getRevisionsData(array $revisions)
121
	{
122 2
		$results = array();
123
124 2
		foreach ( $revisions as $revision ) {
125 2
			if ( isset($this->_revisionPaths[$revision]) ) {
126 1
				$results[$revision] = $this->_revisionPaths[$revision];
127 1
			}
128 2
		}
129
130 2
		$this->assertNoMissingRevisions($revisions, $results);
131
132 1
		return $results;
133
	}
134
135
	/**
136
	 * Returns data, collected by plugin.
137
	 *
138
	 * @return array
139
	 */
140 1
	public function getCollectedData()
141
	{
142
		return array(
143 1
			'revision_paths' => $this->_revisionPaths,
144 1
			'path_revisions' => $this->_pathRevisions,
145 1
		);
146
	}
147
148
	/**
149
	 * Initializes plugin using previously collected data.
150
	 *
151
	 * @param array $collected_data Collected data.
152
	 *
153
	 * @return void
154
	 */
155 6
	public function setCollectedData(array $collected_data)
156
	{
157 6
		$this->_revisionPaths = $collected_data['revision_paths'];
158 6
		$this->_pathRevisions = $collected_data['path_revisions'];
159 6
	}
160
161
	/**
162
	 * Returns cache invalidator for this plugin data.
163
	 *
164
	 * @return string
165
	 */
166 2
	public function getCacheInvalidator()
167
	{
168 2
		return self::CACHE_FORMAT_VERSION;
169
	}
170
171
	/**
172
	 * Returns last known revision number.
173
	 *
174
	 * @return integer
175
	 */
176 2
	public function getLastRevision()
177
	{
178 2
		if ( !$this->_revisionPaths ) {
179 1
			return null;
180
		}
181
182 1
		end($this->_revisionPaths);
183
184 1
		return key($this->_revisionPaths);
185
	}
186
187
}
188