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

MergesPlugin   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
eloc 47
dl 0
loc 151
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A defineStatisticTypes() 0 4 1
A getRevisionsData() 0 21 3
A getName() 0 3 1
A getRevisionQueryFlags() 0 3 1
B find() 0 55 6
A doParse() 0 10 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\RevisionLog;
15
16
class MergesPlugin extends AbstractRepositoryCollectorPlugin
17
{
18
19
	const STATISTIC_MERGE_ADDED = 'merge_added';
20
21
	/**
22
	 * Returns plugin name.
23
	 *
24
	 * @return string
25
	 */
26 7
	public function getName()
27
	{
28 7
		return 'merges';
29
	}
30
31
	/**
32
	 * Returns revision query flags.
33
	 *
34
	 * @return array
35
	 */
36 1
	public function getRevisionQueryFlags()
37
	{
38 1
		return array(RevisionLog::FLAG_MERGE_HISTORY);
39
	}
40
41
	/**
42
	 * Defines parsing statistic types.
43
	 *
44
	 * @return array
45
	 */
46 16
	public function defineStatisticTypes()
47
	{
48 16
		return array(
49 16
			self::STATISTIC_MERGE_ADDED,
50 16
		);
51
	}
52
53
	/**
54
	 * Does actual parsing.
55
	 *
56
	 * @param integer           $revision  Revision.
57
	 * @param \SimpleXMLElement $log_entry Log Entry.
58
	 *
59
	 * @return void
60
	 */
61 2
	protected function doParse($revision, \SimpleXMLElement $log_entry)
62
	{
63 2
		$merged_revisions = array();
64
65 2
		foreach ( $log_entry->logentry as $merged_log_entry ) {
66 2
			$merged_revisions[] = (int)$merged_log_entry['revision'];
67
		}
68
69 2
		$this->repositoryFiller->addMergeCommit($revision, $merged_revisions);
70 2
		$this->recordStatistic(self::STATISTIC_MERGE_ADDED, count($merged_revisions));
71
	}
72
73
	/**
74
	 * Find revisions by collected data.
75
	 *
76
	 * @param array       $criteria     Criteria.
77
	 * @param string|null $project_path Project path.
78
	 *
79
	 * @return array
80
	 * @throws \InvalidArgumentException When one of given merge revision wasn't found.
81
	 */
82 7
	public function find(array $criteria, $project_path)
83
	{
84 7
		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...
85 1
			return array();
86
		}
87
88 6
		$first_criteria = reset($criteria);
89 6
		$project_id = $this->getProject($project_path);
90
91 5
		if ( $first_criteria === 'all_merges' ) {
92 1
			$sql = 'SELECT DISTINCT m.MergeRevision
93
					FROM Merges m
94
					JOIN CommitProjects cp ON cp.Revision = m.MergeRevision
95 1
					WHERE cp.ProjectId = :project_id';
96
97 1
			return $this->database->fetchCol($sql, array('project_id' => $project_id));
98
		}
99 4
		elseif ( $first_criteria === 'all_merged' ) {
100 1
			$sql = 'SELECT DISTINCT m.MergedRevision
101
					FROM Merges m
102
					JOIN CommitProjects cp ON cp.Revision = m.MergedRevision
103 1
					WHERE cp.ProjectId = :project_id';
104
105 1
			return $this->database->fetchCol($sql, array('project_id' => $project_id));
106
		}
107
108 3
		$merge_revisions = array();
109 3
		$merged_revisions = array();
110
111 3
		$sql = 'SELECT m.MergeRevision, m.MergedRevision
112
				FROM Merges m
113
				JOIN CommitProjects cp ON cp.Revision = m.MergeRevision
114 3
				WHERE cp.ProjectId = :project_id AND m.MergeRevision IN (:merge_revisions)';
115 3
		$tmp_revisions = $this->database->fetchAll($sql, array(
116 3
			'project_id' => $project_id,
117 3
			'merge_revisions' => $criteria,
118 3
		));
119
120 3
		foreach ( $tmp_revisions as $revision_data ) {
121 2
			$merge_revisions[$revision_data['MergeRevision']] = true;
122 2
			$merged_revisions[$revision_data['MergedRevision']] = true;
123
		}
124
125 3
		$unknown_merge_revisions = array_diff($criteria, array_keys($merge_revisions));
126
127 3
		if ( $unknown_merge_revisions ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $unknown_merge_revisions 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...
128 3
			throw new \InvalidArgumentException(
129 3
				'The merge revision(-s) "' . implode('", "', $unknown_merge_revisions) . '" not found.'
130 3
			);
131
		}
132
133 2
		$merged_revisions = array_keys($merged_revisions);
134 2
		sort($merged_revisions, SORT_NUMERIC);
135
136 2
		return $merged_revisions;
137
	}
138
139
	/**
140
	 * Returns information about revisions.
141
	 *
142
	 * @param array $revisions Revisions.
143
	 *
144
	 * @return array
145
	 */
146 1
	public function getRevisionsData(array $revisions)
147
	{
148 1
		$results = array();
149
150 1
		$sql = 'SELECT MergeRevision, MergedRevision
151
				FROM Merges
152 1
				WHERE MergedRevision IN (:merged_revisions)';
153 1
		$revisions_data = $this->database->fetchAll($sql, array('merged_revisions' => $revisions));
154
155 1
		foreach ( $revisions_data as $revision_data ) {
156 1
			$merge_revision = $revision_data['MergeRevision'];
157 1
			$merged_revision = $revision_data['MergedRevision'];
158
159 1
			if ( !isset($results[$merged_revision]) ) {
160 1
				$results[$merged_revision] = array();
161
			}
162
163 1
			$results[$merged_revision][] = $merge_revision;
164
		}
165
166 1
		return $this->addMissingResults($revisions, $results);
167
	}
168
169
}
170