Completed
Push — master ( 3c513e...852a18 )
by Alexander
03:56
created

RefsRevisionLogPlugin   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 189
ccs 62
cts 62
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
C parse() 0 34 7
B find() 0 23 5
A getRevisionData() 0 9 2
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
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
15
16
class RefsRevisionLogPlugin implements IRevisionLogPlugin
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 13
	public function __construct(Connector $repository_connector)
47
	{
48 13
		$this->_repositoryConnector = $repository_connector;
49 13
	}
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 data, collected by plugin.
154
	 *
155
	 * @return array
156
	 */
157 1
	public function getCollectedData()
158
	{
159
		return array(
160 1
			'revision_refs' => $this->_revisionRefs,
161 1
			'ref_revisions' => $this->_refRevisions,
162 1
		);
163
	}
164
165
	/**
166
	 * Initializes plugin using previously collected data.
167
	 *
168
	 * @param array $collected_data Collected data.
169
	 *
170
	 * @return void
171
	 */
172 6
	public function setCollectedData(array $collected_data)
173
	{
174 6
		$this->_revisionRefs = $collected_data['revision_refs'];
175 6
		$this->_refRevisions = $collected_data['ref_revisions'];
176 6
	}
177
178
	/**
179
	 * Returns cache invalidator for this plugin data.
180
	 *
181
	 * @return string
182
	 */
183 2
	public function getCacheInvalidator()
184
	{
185 2
		return self::CACHE_FORMAT_VERSION;
186
	}
187
188
	/**
189
	 * Returns last known revision number.
190
	 *
191
	 * @return integer
192
	 */
193 2
	public function getLastRevision()
194
	{
195 2
		if ( !$this->_revisionRefs ) {
196 1
			return null;
197
		}
198
199 1
		end($this->_revisionRefs);
200
201 1
		return key($this->_revisionRefs);
202
	}
203
204
}
205