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

RefsRevisionLogPlugin::setCollectedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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\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 12
	public function __construct(Connector $repository_connector)
47
	{
48 12
		$this->_repositoryConnector = $repository_connector;
49 12
	}
50
51
	/**
52
	 * Returns plugin name.
53
	 *
54
	 * @return string
55
	 */
56 2
	public function getName()
57
	{
58 2
		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 revisions.
136
	 *
137
	 * @param array $revisions Revisions.
138
	 *
139
	 * @return array
140
	 */
141 1
	public function getRevisionsData(array $revisions)
142
	{
143 1
		$results = array();
144
145 1
		foreach ( $revisions as $revision ) {
146 1
			if ( isset($this->_revisionRefs[$revision]) ) {
147 1
				$results[$revision] = $this->_revisionRefs[$revision];
148 1
			}
149 1
		}
150
151 1
		return $this->addMissingResults($revisions, $results);
152
	}
153
154
	/**
155
	 * Returns data, collected by plugin.
156
	 *
157
	 * @return array
158
	 */
159 1
	public function getCollectedData()
160
	{
161
		return array(
162 1
			'revision_refs' => $this->_revisionRefs,
163 1
			'ref_revisions' => $this->_refRevisions,
164 1
		);
165
	}
166
167
	/**
168
	 * Initializes plugin using previously collected data.
169
	 *
170
	 * @param array $collected_data Collected data.
171
	 *
172
	 * @return void
173
	 */
174 6
	public function setCollectedData(array $collected_data)
175
	{
176 6
		$this->_revisionRefs = $collected_data['revision_refs'];
177 6
		$this->_refRevisions = $collected_data['ref_revisions'];
178 6
	}
179
180
	/**
181
	 * Returns cache invalidator for this plugin data.
182
	 *
183
	 * @return string
184
	 */
185 2
	public function getCacheInvalidator()
186
	{
187 2
		return self::CACHE_FORMAT_VERSION;
188
	}
189
190
	/**
191
	 * Returns last known revision number.
192
	 *
193
	 * @return integer
194
	 */
195 2
	public function getLastRevision()
196
	{
197 2
		if ( !$this->_revisionRefs ) {
198 1
			return null;
199
		}
200
201 1
		end($this->_revisionRefs);
202
203 1
		return key($this->_revisionRefs);
204
	}
205
206
}
207