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

SummaryRevisionLogPlugin::getRevisionData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 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
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 5
	public function getName()
38
	{
39 5
		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 revisions.
113
	 *
114
	 * @param array $revisions Revisions.
115
	 *
116
	 * @return array
117
	 */
118 2
	public function getRevisionsData(array $revisions)
119
	{
120 2
		$results = array();
121
122 2
		foreach ( $revisions as $revision ) {
123 2
			if ( isset($this->_revisionSummary[$revision]) ) {
124 1
				$results[$revision] = $this->_revisionSummary[$revision];
125 1
			}
126 2
		}
127
128 2
		$this->assertNoMissingRevisions($revisions, $results);
129
130 1
		return $results;
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_summary' => $this->_revisionSummary,
142 1
			'author_revisions' => $this->_authorRevisions,
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->_revisionSummary = $collected_data['revision_summary'];
156 6
		$this->_authorRevisions = $collected_data['author_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 3
	public function getLastRevision()
175
	{
176 3
		if ( !$this->_revisionSummary ) {
177 2
			return null;
178
		}
179
180 1
		end($this->_revisionSummary);
181
182 1
		return key($this->_revisionSummary);
183
	}
184
185
}
186