Completed
Push — master ( 308042...e44400 )
by Alexander
03:30
created

RefsPlugin::findRevisionsByRef()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
cc 3
eloc 10
nc 3
nop 2
crap 3
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;
12
13
14
class RefsPlugin extends AbstractDatabaseCollectorPlugin
15
{
16
17
	/**
18
	 * Returns plugin name.
19
	 *
20
	 * @return string
21
	 */
22 5
	public function getName()
23
	{
24 5
		return 'refs';
25
	}
26
27
	/**
28
	 * Defines parsing statistic types.
29
	 *
30
	 * @return array
31
	 */
32 10
	public function defineStatisticTypes()
33
	{
34 10
		return array();
35
	}
36
37
	/**
38
	 * Processes data.
39
	 *
40
	 * @param integer $from_revision From revision.
41
	 * @param integer $to_revision   To revision.
42
	 *
43
	 * @return void
44
	 */
45 1
	public function doProcess($from_revision, $to_revision)
46
	{
47
		// Do nothing, because "paths" plugin determines refs as well.
48 1
		$this->setLastRevision($to_revision);
49 1
		$this->advanceProgressBar();
50 1
	}
51
52
	/**
53
	 * Find revisions by collected data.
54
	 *
55
	 * @param array       $criteria     Criteria.
56
	 * @param string|null $project_path Project path.
57
	 *
58
	 * @return array
59
	 */
60 4
	public function find(array $criteria, $project_path)
61
	{
62 4
		if ( !$criteria ) {
63 1
			return array();
64
		}
65
66 3
		$project_id = $this->getProject($project_path);
67
68 2
		if ( reset($criteria) === 'all_refs' ) {
69 1
			return $this->getProjectRefs($project_id);
70
		}
71
72 1
		$ref_revisions = $this->findRevisionsByRef($project_id, $criteria);
73
74 1
		sort($ref_revisions, SORT_NUMERIC);
75
76 1
		return $ref_revisions;
77
	}
78
79
	/**
80
	 * Returns names of all refs in a project.
81
	 *
82
	 * @param integer $project_id Project ID.
83
	 *
84
	 * @return array
85
	 */
86 1
	protected function getProjectRefs($project_id)
87
	{
88
		$sql = 'SELECT DISTINCT Name
89
				FROM ProjectRefs
90 1
				WHERE ProjectId = :project_id';
91
92 1
		return $this->database->fetchCol($sql, array('project_id' => $project_id));
93
	}
94
95
	/**
96
	 * Finds revisions by ref.
97
	 *
98
	 * @param integer $project_id Project ID.
99
	 * @param array   $refs       Refs.
100
	 *
101
	 * @return array
102
	 */
103 1
	protected function findRevisionsByRef($project_id, array $refs)
104
	{
105
		$sql = 'SELECT Path
106
				FROM Projects
107 1
				WHERE Id = :project_id';
108 1
		$project_path = $this->database->fetchValue($sql, array(
109 1
			'project_id' => $project_id,
110 1
		));
111
112 1
		$ref_revisions = array();
113
114 1
		foreach ( $refs as $ref ) {
115 1
			$tmp_revisions = $this->revisionLog->find('paths', $project_path . $ref . '/');
116
117
			// Add revisions from refs.
118 1
			foreach ( $tmp_revisions as $revision ) {
119 1
				$ref_revisions[$revision] = true;
120 1
			}
121 1
		}
122
123 1
		return array_keys($ref_revisions);
124
	}
125
126
	/**
127
	 * Returns information about revisions.
128
	 *
129
	 * @param array $revisions Revisions.
130
	 *
131
	 * @return array
132
	 */
133 1
	public function getRevisionsData(array $revisions)
134
	{
135 1
		$results = array();
136
137
		$sql = 'SELECT cr.Revision, pr.Name
138
				FROM CommitRefs cr
139
				JOIN ProjectRefs pr ON pr.Id = cr.RefId
140 1
				WHERE cr.Revision IN (:revisions)';
141 1
		$revisions_data = $this->database->fetchAll($sql, array('revisions' => $revisions));
142
143 1
		foreach ( $revisions_data as $revision_data ) {
144 1
			$revision = $revision_data['Revision'];
145 1
			$ref = $revision_data['Name'];
146
147 1
			if ( !isset($results[$revision]) ) {
148 1
				$results[$revision] = array();
149 1
			}
150
151 1
			$results[$revision][] = $ref;
152 1
		}
153
154 1
		return $this->addMissingResults($revisions, $results);
155
	}
156
157
}
158