Failed Conditions
Push — master ( 7ce495...e5509d )
by Alexander
02:41
created

SummaryPlugin::find()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 41
ccs 24
cts 24
cp 1
rs 8.9457
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6
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
class SummaryPlugin extends AbstractRepositoryCollectorPlugin
15
{
16
17
	const STATISTIC_COMMIT_ADDED = 'commit_added';
18
19
	/**
20
	 * Returns plugin name.
21
	 *
22
	 * @return string
23
	 */
24 10
	public function getName()
25
	{
26 10
		return 'summary';
27
	}
28
29
	/**
30
	 * Defines parsing statistic types.
31
	 *
32
	 * @return array
33
	 */
34 16
	public function defineStatisticTypes()
35
	{
36 16
		return array(
37 16
			self::STATISTIC_COMMIT_ADDED,
38 16
		);
39
	}
40
41
	/**
42
	 * Does actual parsing.
43
	 *
44
	 * @param integer           $revision  Revision.
45
	 * @param \SimpleXMLElement $log_entry Log Entry.
46
	 *
47
	 * @return void
48
	 */
49 2
	protected function doParse($revision, \SimpleXMLElement $log_entry)
50
	{
51 2
		$this->repositoryFiller->addCommit(
52 2
			$revision,
53 2
			(string)$log_entry->author,
54 2
			strtotime($log_entry->date),
55 2
			(string)$log_entry->msg
56 2
		);
57
58 2
		$this->recordStatistic(self::STATISTIC_COMMIT_ADDED);
59
	}
60
61
	/**
62
	 * Find revisions by collected data.
63
	 *
64
	 * @param array       $criteria     Criteria.
65
	 * @param string|null $project_path Project path.
66
	 *
67
	 * @return array
68
	 * @throws \InvalidArgumentException When malformed criterion given (e.g. no field name).
69
	 */
70 6
	public function find(array $criteria, $project_path)
71
	{
72 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...
73 1
			return array();
74
		}
75
76 5
		$summary_revisions = array();
77 5
		$project_id = $this->getProject($project_path);
78
79 4
		foreach ( $criteria as $criterion ) {
80 4
			if ( strpos($criterion, ':') === false ) {
81 1
				$error_msg = 'Each criterion of "%s" plugin must be in "%s" format.';
82 1
				throw new \InvalidArgumentException(sprintf($error_msg, $this->getName(), 'field:value'));
83
			}
84
85 3
			list ($field, $value) = explode(':', $criterion, 2);
86
87 3
			if ( $field === 'author' ) {
88 2
				$sql = 'SELECT c.Revision
89
						FROM Commits c
90
						JOIN CommitProjects cp ON cp.Revision = c.Revision
91 2
						WHERE cp.ProjectId = :project_id AND c.Author = :author';
92 2
				$tmp_revisions = $this->database->fetchCol($sql, array(
93 2
					'project_id' => $project_id,
94 2
					'author' => $value,
95 2
				));
96
97 2
				foreach ( $tmp_revisions as $revision ) {
98 1
					$summary_revisions[$revision] = true;
99
				}
100
			}
101
			else {
102 1
				$error_msg = 'Searching by "%s" is not supported by "%s" plugin.';
103 1
				throw new \InvalidArgumentException(sprintf($error_msg, $field, $this->getName()));
104
			}
105
		}
106
107 2
		$summary_revisions = array_keys($summary_revisions);
108 2
		sort($summary_revisions, SORT_NUMERIC);
109
110 2
		return $summary_revisions;
111
	}
112
113
	/**
114
	 * Returns information about revisions.
115
	 *
116
	 * @param array $revisions Revisions.
117
	 *
118
	 * @return array
119
	 */
120 2
	public function getRevisionsData(array $revisions)
121
	{
122 2
		$sql = 'SELECT Revision, Author AS author, Date AS date, Message AS msg
123
				FROM Commits
124 2
				WHERE Revision IN (:revision_ids)';
125 2
		$results = $this->database->fetchAssoc($sql, array('revision_ids' => $revisions));
126
127 2
		foreach ( array_keys($results) as $revision ) {
128 1
			unset($results[$revision]['Revision']);
129
		}
130
131 2
		$this->assertNoMissingRevisions($revisions, $results);
132
133 1
		return $results;
134
	}
135
136
}
137