Failed Conditions
Push — master ( 305b4a...bd53b9 )
by Alexander
02:57
created

RefsRevisionLogPlugin::getCollectedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

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 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
class RefsRevisionLogPlugin implements IRevisionLogPlugin
15
{
16
	const CACHE_FORMAT_VERSION = 1;
17
18
	/**
19
	 * Refs affected by specific revision.
20
	 *
21
	 * @var array
22
	 */
23
	private $_revisionRefs = array();
24
25
	/**
26
	 * Revisions affecting a specific ref.
27
	 *
28
	 * @var array
29
	 */
30
	private $_refRevisions = array();
31
32
	/**
33
	 * Returns plugin name.
34
	 *
35
	 * @return string
36
	 */
37 3
	public function getName()
38
	{
39 3
		return 'refs';
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_refs = array();
53 1
			$revision = (int)$log_entry['revision'];
54
55 1
			foreach ( $log_entry->paths->path as $path_node ) {
56
				/** @var \SimpleXMLElement $path_node */
57 1
				$path = (string)$path_node;
58
59 1
				if ( !preg_match('#^.*?/(trunk|branches/[^/]*|tags/[^/]*|releases/[^/]*).*$#', $path, $regs) ) {
60 1
					continue;
61
				}
62
63 1
				$revision_refs[$regs[1]] = true;
64 1
			}
65
66
			// Path in commit is in unknown format.
67 1
			if ( !$revision_refs ) {
68 1
				continue;
69
			}
70
71 1
			$this->_revisionRefs[$revision] = array();
72
73 1
			foreach ( array_keys($revision_refs) as $ref ) {
74 1
				if ( !isset($this->_refRevisions[$ref]) ) {
75 1
					$this->_refRevisions[$ref] = array();
76 1
				}
77
78 1
				$this->_refRevisions[$ref][] = $revision;
79 1
				$this->_revisionRefs[$revision][] = $ref;
80 1
			}
81 1
		}
82 1
	}
83
84
	/**
85
	 * Find revisions by collected data.
86
	 *
87
	 * @param array $criteria Criteria.
88
	 *
89
	 * @return array
90
	 */
91 5
	public function find(array $criteria)
92
	{
93 5
		if ( reset($criteria) === 'all_refs' ) {
94 1
			return array_keys($this->_refRevisions);
95
		}
96
97 4
		$ref_revisions = array();
98
99 4
		foreach ( $criteria as $ref ) {
100 3
			if ( !isset($this->_refRevisions[$ref]) ) {
101 1
				continue;
102
			}
103
104 2
			foreach ( $this->_refRevisions[$ref] as $revision ) {
105 2
				$ref_revisions[$revision] = true;
106 2
			}
107 4
		}
108
109 4
		$ref_revisions = array_keys($ref_revisions);
110 4
		sort($ref_revisions, SORT_NUMERIC);
111
112 4
		return $ref_revisions;
113
	}
114
115
	/**
116
	 * Returns information about revision.
117
	 *
118
	 * @param integer $revision Revision.
119
	 *
120
	 * @return array
121
	 * @throws \InvalidArgumentException When revision is not found.
122
	 */
123 2
	public function getRevisionData($revision)
124
	{
125 2
		if ( !isset($this->_revisionRefs[$revision]) ) {
126 1
			$error_msg = 'Revision "%s" not found by "%s" plugin.';
127 1
			throw new \InvalidArgumentException(sprintf($error_msg, $revision, $this->getName()));
128
		}
129
130 1
		return $this->_revisionRefs[$revision];
131
	}
132
133
	/**
134
	 * Returns data, collected by plugin.
135
	 *
136
	 * @return array
137
	 */
138 1
	public function getCollectedData()
139
	{
140
		return array(
141 1
			'revision_refs' => $this->_revisionRefs,
142 1
			'ref_revisions' => $this->_refRevisions,
143 1
		);
144
	}
145
146
	/**
147
	 * Initializes plugin using previously collected data.
148
	 *
149
	 * @param array $collected_data Collected data.
150
	 *
151
	 * @return void
152
	 */
153 6
	public function setCollectedData(array $collected_data)
154
	{
155 6
		$this->_revisionRefs = $collected_data['revision_refs'];
156 6
		$this->_refRevisions = $collected_data['ref_revisions'];
157 6
	}
158
159
	/**
160
	 * Returns cache invalidator for this plugin data.
161
	 *
162
	 * @return string
163
	 */
164 2
	public function getCacheInvalidator()
165
	{
166 2
		return self::CACHE_FORMAT_VERSION;
167
	}
168
169
	/**
170
	 * Returns last known revision number.
171
	 *
172
	 * @return integer
173
	 */
174 2
	public function getLastRevision()
175
	{
176 2
		if ( !$this->_revisionRefs ) {
177 1
			return null;
178
		}
179
180 1
		end($this->_revisionRefs);
181
182 1
		return key($this->_revisionRefs);
183
	}
184
185
}
186