Completed
Push — master ( 8eb698...a673ca )
by Alexander
02:26
created

SummaryRevisionLogPlugin::getCacheInvalidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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 SummaryRevisionLogPlugin extends AbstractRevisionLogPlugin
15
{
16
	const CACHE_FORMAT_VERSION = 1;
17
18
	/**
19
	 * Bugs affected by specific revision.
20
	 *
21
	 * @var array
22
	 */
23
	private $_revisionSummary = array();
24
25
	/**
26
	 * Revisions made by specific author.
27
	 *
28
	 * @var array
29
	 */
30
	private $_authorRevisions = array();
31
32
	/**
33
	 * Returns plugin name.
34
	 *
35
	 * @return string
36
	 */
37 6
	public function getName()
38
	{
39 6
		return 'summary';
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 = (int)$log_entry['revision'];
53 1
			$author = (string)$log_entry->author;
54
55 1
			if ( !isset($this->_authorRevisions[$author]) ) {
56 1
				$this->_authorRevisions[$author] = array();
57 1
			}
58
59 1
			$this->_authorRevisions[$author][] = $revision;
60
61 1
			$this->_revisionSummary[$revision] = array(
62 1
				'author' => $author,
63 1
				'date' => strtotime($log_entry->date),
64 1
				'msg' => (string)$log_entry->msg,
65
			);
66 1
		}
67 1
	}
68
69
	/**
70
	 * Find revisions by collected data.
71
	 *
72
	 * @param array $criteria Criteria.
73
	 *
74
	 * @return array
75
	 * @throws \InvalidArgumentException When unsupported search field given.
76
	 * @throws \InvalidArgumentException When malformed criterion given (e.g. no field name).
77
	 */
78 5
	public function find(array $criteria)
79
	{
80 5
		$summary_revisions = array();
81
82 5
		foreach ( $criteria as $criterion ) {
83 4
			if ( strpos($criterion, ':') === false ) {
84 1
				$error_msg = 'Each criterion of "%s" plugin must be in "%s" format.';
85 1
				throw new \InvalidArgumentException(sprintf($error_msg, $this->getName(), 'field:value'));
86
			}
87
88 3
			list ($field, $value) = explode(':', $criterion, 2);
89
90 3
			if ( $field === 'author' ) {
91 2
				if ( !array_key_exists($value, $this->_authorRevisions) ) {
92 1
					continue;
93
				}
94
95 1
				foreach ( $this->_authorRevisions[$value] as $revision ) {
96 1
					$summary_revisions[$revision] = true;
97 1
				}
98 1
			}
99
			else {
100 1
				$error_msg = 'Searching by "%s" is not supported by "%s" plugin.';
101 1
				throw new \InvalidArgumentException(sprintf($error_msg, $field, $this->getName()));
102
			}
103 3
		}
104
105 3
		$summary_revisions = array_keys($summary_revisions);
106 3
		sort($summary_revisions, SORT_NUMERIC);
107
108 3
		return $summary_revisions;
109
	}
110
111
	/**
112
	 * Returns information about revision.
113
	 *
114
	 * @param integer $revision Revision.
115
	 *
116
	 * @return array
117
	 * @throws \InvalidArgumentException When revision is not found.
118
	 */
119 2
	public function getRevisionData($revision)
120
	{
121 2
		if ( !isset($this->_revisionSummary[$revision]) ) {
122 1
			$error_msg = 'Revision "%s" not found by "%s" plugin.';
123 1
			throw new \InvalidArgumentException(sprintf($error_msg, $revision, $this->getName()));
124
		}
125
126 1
		return $this->_revisionSummary[$revision];
127
	}
128
129
	/**
130
	 * Returns information about revisions.
131
	 *
132
	 * @param array $revisions Revisions.
133
	 *
134
	 * @return array
135
	 */
136 2
	public function getRevisionsData(array $revisions)
137
	{
138 2
		$results = array();
139
140 2
		foreach ( $revisions as $revision ) {
141 2
			if ( isset($this->_revisionSummary[$revision]) ) {
142 1
				$results[$revision] = $this->_revisionSummary[$revision];
143 1
			}
144 2
		}
145
146 2
		$this->assertNoMissingRevisions($revisions, $results);
147
148 1
		return $results;
149
	}
150
151
	/**
152
	 * Returns data, collected by plugin.
153
	 *
154
	 * @return array
155
	 */
156 1
	public function getCollectedData()
157
	{
158
		return array(
159 1
			'revision_summary' => $this->_revisionSummary,
160 1
			'author_revisions' => $this->_authorRevisions,
161 1
		);
162
	}
163
164
	/**
165
	 * Initializes plugin using previously collected data.
166
	 *
167
	 * @param array $collected_data Collected data.
168
	 *
169
	 * @return void
170
	 */
171 7
	public function setCollectedData(array $collected_data)
172
	{
173 7
		$this->_revisionSummary = $collected_data['revision_summary'];
174 7
		$this->_authorRevisions = $collected_data['author_revisions'];
175 7
	}
176
177
	/**
178
	 * Returns cache invalidator for this plugin data.
179
	 *
180
	 * @return string
181
	 */
182 2
	public function getCacheInvalidator()
183
	{
184 2
		return self::CACHE_FORMAT_VERSION;
185
	}
186
187
	/**
188
	 * Returns last known revision number.
189
	 *
190
	 * @return integer
191
	 */
192 3
	public function getLastRevision()
193
	{
194 3
		if ( !$this->_revisionSummary ) {
195 2
			return null;
196
		}
197
198 1
		end($this->_revisionSummary);
199
200 1
		return key($this->_revisionSummary);
201
	}
202
203
}
204