Failed Conditions
Push — master ( 30a496...e276e6 )
by Alexander
02:31
created

MergesRevisionLogPlugin::getRevisionsData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 3
cts 3
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
class MergesRevisionLogPlugin extends AbstractRevisionLogPlugin
15
{
16
	const CACHE_FORMAT_VERSION = 1;
17
18
	/**
19
	 * List of merged revisions (key - merged revision, value - array of revisions in which it was merged).
20
	 *
21
	 * @var array
22
	 */
23
	private $_mergedRevisions = array();
24
25
	/**
26
	 * List of merge revisions (key - merge revision, value - array of revisions merged by this revision).
27
	 *
28
	 * @var array
29
	 */
30
	private $_mergeRevisions = array();
31
32
	/**
33
	 * Returns plugin name.
34
	 *
35
	 * @return string
36
	 */
37 2
	public function getName()
38
	{
39 2
		return 'merges';
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_merges = array();
53 1
			$revision = (int)$log_entry['revision'];
54
55 1
			foreach ( $log_entry->logentry as $merged_log_entry ) {
56 1
				$merged_revision = (int)$merged_log_entry['revision'];
57
58 1
				$revision_merges[] = $merged_revision;
59
60 1
				if ( !isset($this->_mergedRevisions[$merged_revision]) ) {
61 1
					$this->_mergedRevisions[$merged_revision] = array();
62 1
				}
63
64 1
				$this->_mergedRevisions[$merged_revision][] = $revision;
65 1
			}
66
67 1
			if ( $revision_merges ) {
68 1
				$this->_mergeRevisions[$revision] = $revision_merges;
69 1
			}
70 1
		}
71 1
	}
72
73
	/**
74
	 * Find revisions by collected data.
75
	 *
76
	 * @param array $criteria Criteria.
77
	 *
78
	 * @return array
79
	 * @throws \InvalidArgumentException When one of given merge revision wasn't found.
80
	 */
81 6
	public function find(array $criteria)
82
	{
83 6
		if ( !$criteria ) {
84 1
			return array();
85
		}
86
87 5
		$first_criteria = reset($criteria);
88
89 5
		if ( $first_criteria === 'all_merges' ) {
90 1
			return array_keys($this->_mergeRevisions);
91
		}
92 4
		elseif ( $first_criteria === 'all_merged' ) {
93 1
			return array_keys($this->_mergedRevisions);
94
		}
95
96 3
		$merged_revisions = array();
97
98 3
		foreach ( $criteria as $merge_revision ) {
99 3
			if ( !array_key_exists($merge_revision, $this->_mergeRevisions) ) {
100 1
				throw new \InvalidArgumentException('The merge revision ' . $merge_revision . ' not found.');
101
			}
102
103 2
			foreach ( $this->_mergeRevisions[$merge_revision] as $merged_revision ) {
104 2
				$merged_revisions[$merged_revision] = true;
105 2
			}
106 2
		}
107
108 2
		$merged_revisions = array_keys($merged_revisions);
109 2
		sort($merged_revisions, SORT_NUMERIC);
110
111 2
		return $merged_revisions;
112
	}
113
114
	/**
115
	 * Returns information about revision.
116
	 *
117
	 * @param integer $revision Revision.
118
	 *
119
	 * @return array
120
	 */
121 2
	public function getRevisionData($revision)
122
	{
123
		// When revision wasn't yet merged, the merge revisions list is empty.
124 2
		return isset($this->_mergedRevisions[$revision]) ? $this->_mergedRevisions[$revision] : array();
125
	}
126
127
	/**
128
	 * Returns information about revisions.
129
	 *
130
	 * @param array $revisions Revisions.
131
	 *
132 1
	 * @return array
133
	 */
134
	public function getRevisionsData(array $revisions)
135 1
	{
136 1
		$results = array();
137 1
138
		foreach ( $revisions as $revision ) {
139
			if ( isset($this->_mergedRevisions[$revision]) ) {
140
				$results[$revision] = $this->_mergedRevisions[$revision];
141
			}
142
		}
143
144
		return $this->addMissingResults($revisions, $results);
145
	}
146
147 7
	/**
148
	 * Returns data, collected by plugin.
149 7
	 *
150 7
	 * @return array
151 7
	 */
152
	public function getCollectedData()
153
	{
154
		return array(
155
			'merge_revisions' => $this->_mergeRevisions,
156
			'merged_revisions' => $this->_mergedRevisions,
157
		);
158 2
	}
159
160 2
	/**
161
	 * Initializes plugin using previously collected data.
162
	 *
163
	 * @param array $collected_data Collected data.
164
	 *
165
	 * @return void
166
	 */
167
	public function setCollectedData(array $collected_data)
168 2
	{
169
		$this->_mergeRevisions = $collected_data['merge_revisions'];
170 2
		$this->_mergedRevisions = $collected_data['merged_revisions'];
171 1
	}
172
173
	/**
174 1
	 * Returns cache invalidator for this plugin data.
175
	 *
176 1
	 * @return string
177
	 */
178
	public function getCacheInvalidator()
179
	{
180
		return self::CACHE_FORMAT_VERSION;
181
	}
182
183
	/**
184
	 * Returns last known revision number.
185
	 *
186
	 * @return integer
187
	 */
188
	public function getLastRevision()
189
	{
190
		if ( !$this->_mergeRevisions ) {
191
			return null;
192
		}
193
194
		end($this->_mergeRevisions);
195
196
		return key($this->_mergeRevisions);
197
	}
198
199
}
200