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

MergesRevisionLogPlugin::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 2
Bugs 0 Features 1
Metric Value
c 2
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
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 revisions.
116
	 *
117
	 * @param array $revisions Revisions.
118
	 *
119
	 * @return array
120
	 */
121 1
	public function getRevisionsData(array $revisions)
122
	{
123 1
		$results = array();
124
125 1
		foreach ( $revisions as $revision ) {
126 1
			if ( isset($this->_mergedRevisions[$revision]) ) {
127 1
				$results[$revision] = $this->_mergedRevisions[$revision];
128 1
			}
129 1
		}
130
131 1
		return $this->addMissingResults($revisions, $results);
132
	}
133
134
	/**
135
	 * Returns data, collected by plugin.
136
	 *
137
	 * @return array
138
	 */
139 1
	public function getCollectedData()
140
	{
141
		return array(
142 1
			'merge_revisions' => $this->_mergeRevisions,
143 1
			'merged_revisions' => $this->_mergedRevisions,
144 1
		);
145
	}
146
147
	/**
148
	 * Initializes plugin using previously collected data.
149
	 *
150
	 * @param array $collected_data Collected data.
151
	 *
152
	 * @return void
153
	 */
154 7
	public function setCollectedData(array $collected_data)
155
	{
156 7
		$this->_mergeRevisions = $collected_data['merge_revisions'];
157 7
		$this->_mergedRevisions = $collected_data['merged_revisions'];
158 7
	}
159
160
	/**
161
	 * Returns cache invalidator for this plugin data.
162
	 *
163
	 * @return string
164
	 */
165 2
	public function getCacheInvalidator()
166
	{
167 2
		return self::CACHE_FORMAT_VERSION;
168
	}
169
170
	/**
171
	 * Returns last known revision number.
172
	 *
173
	 * @return integer
174
	 */
175 2
	public function getLastRevision()
176
	{
177 2
		if ( !$this->_mergeRevisions ) {
178 1
			return null;
179
		}
180
181 1
		end($this->_mergeRevisions);
182
183 1
		return key($this->_mergeRevisions);
184
	}
185
186
}
187