Failed Conditions
Push — master ( 6b0328...1f80f7 )
by Alexander
03:19 queued 50s
created

SummaryPlugin::getRevisionsData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10
cc 2
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\Plugin\RepositoryCollectorPlugin;
12
13
14
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\Plugin\IOverwriteAwarePlugin;
15
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\Plugin\TOverwriteAwarePlugin;
16
17
class SummaryPlugin extends AbstractRepositoryCollectorPlugin implements IOverwriteAwarePlugin
18
{
19
20
	use TOverwriteAwarePlugin;
21
22
	const STATISTIC_COMMIT_ADDED = 'commit_added';
23
24
	const STATISTIC_COMMIT_REMOVED = 'commit_removed';
25
26
	/**
27
	 * Returns plugin name.
28
	 *
29
	 * @return string
30
	 */
31 12
	public function getName()
32
	{
33 12
		return 'summary';
34
	}
35
36
	/**
37
	 * Defines parsing statistic types.
38
	 *
39
	 * @return array
40
	 */
41 19
	public function defineStatisticTypes()
42
	{
43
		return array(
44 19
			self::STATISTIC_COMMIT_ADDED, self::STATISTIC_COMMIT_REMOVED,
45
		);
46
	}
47
48
	/**
49
	 * Does actual parsing.
50
	 *
51
	 * @param integer           $revision  Revision.
52
	 * @param \SimpleXMLElement $log_entry Log Entry.
53
	 *
54
	 * @return void
55
	 */
56 4
	protected function doParse($revision, \SimpleXMLElement $log_entry)
57
	{
58 4
		$this->repositoryFiller->addCommit(
59 4
			$revision,
60 4
			(string)$log_entry->author,
61 4
			strtotime($log_entry->date),
62 4
			(string)$log_entry->msg
63
		);
64
65 4
		$this->recordStatistic(self::STATISTIC_COMMIT_ADDED);
66 4
	}
67
68
	/**
69
	 * @inheritDoc
70
	 */
71 2
	protected function remove($revision)
72
	{
73 2
		$this->repositoryFiller->removeCommit($revision);
74 2
		$this->recordStatistic(self::STATISTIC_COMMIT_REMOVED);
75 2
	}
76
77
	/**
78
	 * Find revisions by collected data.
79
	 *
80
	 * @param array       $criteria     Criteria.
81
	 * @param string|null $project_path Project path.
82
	 *
83
	 * @return array
84
	 * @throws \InvalidArgumentException When malformed criterion given (e.g. no field name).
85
	 */
86 6
	public function find(array $criteria, $project_path)
87
	{
88 6
		if ( !$criteria ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $criteria of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89 1
			return array();
90
		}
91
92 5
		$summary_revisions = array();
93 5
		$project_id = $this->getProject($project_path);
94
95 4
		foreach ( $criteria as $criterion ) {
96 4
			if ( strpos($criterion, ':') === false ) {
97 1
				$error_msg = 'Each criterion of "%s" plugin must be in "%s" format.';
98 1
				throw new \InvalidArgumentException(sprintf($error_msg, $this->getName(), 'field:value'));
99
			}
100
101 3
			list ($field, $value) = explode(':', $criterion, 2);
102
103 3
			if ( $field === 'author' ) {
104 2
				$sql = 'SELECT c.Revision
105
						FROM Commits c
106
						JOIN CommitProjects cp ON cp.Revision = c.Revision
107
						WHERE cp.ProjectId = :project_id AND c.Author = :author';
108 2
				$tmp_revisions = $this->database->fetchCol($sql, array(
109 2
					'project_id' => $project_id,
110 2
					'author' => $value,
111
				));
112
113 2
				foreach ( $tmp_revisions as $revision ) {
114 2
					$summary_revisions[$revision] = true;
115
				}
116
			}
117
			else {
118 1
				$error_msg = 'Searching by "%s" is not supported by "%s" plugin.';
119 3
				throw new \InvalidArgumentException(sprintf($error_msg, $field, $this->getName()));
120
			}
121
		}
122
123 2
		$summary_revisions = array_keys($summary_revisions);
124 2
		sort($summary_revisions, SORT_NUMERIC);
125
126 2
		return $summary_revisions;
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
		$sql = 'SELECT Revision, Author AS author, Date AS date, Message AS msg
139
				FROM Commits
140
				WHERE Revision IN (:revision_ids)';
141 2
		$results = $this->database->fetchAssoc($sql, array('revision_ids' => $revisions));
142
143 2
		foreach ( array_keys($results) as $revision ) {
144 1
			unset($results[$revision]['Revision']);
145
		}
146
147 2
		$this->assertNoMissingRevisions($revisions, $results);
148
149 1
		return $results;
150
	}
151
152
}
153