Completed
Push — master ( 8eb698...a673ca )
by Alexander
02:26
created

RefsRevisionLogPlugin::getRevisionsData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
15
16
class RefsRevisionLogPlugin extends AbstractRevisionLogPlugin
17
{
18
	const CACHE_FORMAT_VERSION = 1;
19
20
	/**
21
	 * Refs affected by specific revision.
22
	 *
23
	 * @var array
24
	 */
25
	private $_revisionRefs = array();
26
27
	/**
28
	 * Revisions affecting a specific ref.
29
	 *
30
	 * @var array
31
	 */
32
	private $_refRevisions = array();
33
34
	/**
35
	 * Repository connector.
36
	 *
37
	 * @var Connector
38
	 */
39
	private $_repositoryConnector;
40
41
	/**
42
	 * Create refs revision log plugin.
43
	 *
44
	 * @param Connector $repository_connector Repository connector.
45
	 */
46 14
	public function __construct(Connector $repository_connector)
47
	{
48 14
		$this->_repositoryConnector = $repository_connector;
49 14
	}
50
51
	/**
52
	 * Returns plugin name.
53
	 *
54
	 * @return string
55
	 */
56 3
	public function getName()
57
	{
58 3
		return 'refs';
59
	}
60
61
	/**
62
	 * Parse log entries.
63
	 *
64
	 * @param \SimpleXMLElement $log Log.
65
	 *
66
	 * @return void
67
	 */
68 1
	public function parse(\SimpleXMLElement $log)
69
	{
70 1
		foreach ( $log->logentry as $log_entry ) {
71 1
			$revision_refs = array();
72 1
			$revision = (int)$log_entry['revision'];
73
74 1
			foreach ( $log_entry->paths->path as $path_node ) {
75
				/** @var \SimpleXMLElement $path_node */
76 1
				$path = (string)$path_node;
77
78 1
				$ref = $this->_repositoryConnector->getRefByPath($path);
79
80 1
				if ( $ref !== false ) {
81 1
					$revision_refs[$ref] = true;
82 1
				}
83 1
			}
84
85
			// Path in commit is in unknown format.
86 1
			if ( !$revision_refs ) {
87 1
				continue;
88
			}
89
90 1
			$this->_revisionRefs[$revision] = array();
91
92 1
			foreach ( array_keys($revision_refs) as $ref ) {
93 1
				if ( !isset($this->_refRevisions[$ref]) ) {
94 1
					$this->_refRevisions[$ref] = array();
95 1
				}
96
97 1
				$this->_refRevisions[$ref][] = $revision;
98 1
				$this->_revisionRefs[$revision][] = $ref;
99 1
			}
100 1
		}
101 1
	}
102
103
	/**
104
	 * Find revisions by collected data.
105
	 *
106
	 * @param array $criteria Criteria.
107
	 *
108
	 * @return array
109
	 */
110 5
	public function find(array $criteria)
111
	{
112 5
		if ( reset($criteria) === 'all_refs' ) {
113 1
			return array_keys($this->_refRevisions);
114
		}
115
116 4
		$ref_revisions = array();
117
118 4
		foreach ( $criteria as $ref ) {
119 3
			if ( !isset($this->_refRevisions[$ref]) ) {
120 1
				continue;
121
			}
122
123 2
			foreach ( $this->_refRevisions[$ref] as $revision ) {
124 2
				$ref_revisions[$revision] = true;
125 2
			}
126 4
		}
127
128 4
		$ref_revisions = array_keys($ref_revisions);
129 4
		sort($ref_revisions, SORT_NUMERIC);
130
131 4
		return $ref_revisions;
132
	}
133
134
	/**
135
	 * Returns information about revision.
136
	 *
137
	 * @param integer $revision Revision.
138
	 *
139
	 * @return array
140
	 * @throws \InvalidArgumentException When revision is not found.
141
	 */
142 2
	public function getRevisionData($revision)
143
	{
144 2
		if ( !isset($this->_revisionRefs[$revision]) ) {
145 1
			$error_msg = 'Revision "%s" not found by "%s" plugin.';
146 1
			throw new \InvalidArgumentException(sprintf($error_msg, $revision, $this->getName()));
147
		}
148
149 1
		return $this->_revisionRefs[$revision];
150
	}
151
152
	/**
153
	 * Returns information about revisions.
154
	 *
155
	 * @param array $revisions Revisions.
156
	 *
157
	 * @return array
158
	 */
159 1
	public function getRevisionsData(array $revisions)
160
	{
161 1
		$results = array();
162
163 1
		foreach ( $revisions as $revision ) {
164 1
			if ( isset($this->_revisionRefs[$revision]) ) {
165 1
				$results[$revision] = $this->_revisionRefs[$revision];
166 1
			}
167 1
		}
168
169 1
		return $this->addMissingResults($revisions, $results);
170
	}
171
172
	/**
173
	 * Returns data, collected by plugin.
174
	 *
175
	 * @return array
176
	 */
177 1
	public function getCollectedData()
178
	{
179
		return array(
180 1
			'revision_refs' => $this->_revisionRefs,
181 1
			'ref_revisions' => $this->_refRevisions,
182 1
		);
183
	}
184
185
	/**
186
	 * Initializes plugin using previously collected data.
187
	 *
188
	 * @param array $collected_data Collected data.
189
	 *
190
	 * @return void
191
	 */
192 7
	public function setCollectedData(array $collected_data)
193
	{
194 7
		$this->_revisionRefs = $collected_data['revision_refs'];
195 7
		$this->_refRevisions = $collected_data['ref_revisions'];
196 7
	}
197
198
	/**
199
	 * Returns cache invalidator for this plugin data.
200
	 *
201
	 * @return string
202
	 */
203 2
	public function getCacheInvalidator()
204
	{
205 2
		return self::CACHE_FORMAT_VERSION;
206
	}
207
208
	/**
209
	 * Returns last known revision number.
210
	 *
211
	 * @return integer
212
	 */
213 2
	public function getLastRevision()
214
	{
215 2
		if ( !$this->_revisionRefs ) {
216 1
			return null;
217
		}
218
219 1
		end($this->_revisionRefs);
220
221 1
		return key($this->_revisionRefs);
222
	}
223
224
}
225