Failed Conditions
Push — master ( 22b9e7...2474db )
by Alexander
03:11
created

ProjectsPlugin::doProcess()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 5.3846
cc 8
eloc 14
nc 8
nop 2
crap 8
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
use Aura\Sql\Iterator\ColIterator;
15
16
class ProjectsPlugin extends AbstractDatabaseCollectorPlugin
17
{
18
19
	const STATISTIC_PROJECT_DELETED = 'project_deleted';
20
21
	const STATISTIC_PROJECT_RESTORED = 'project_restored';
22
23
	/**
24
	 * Returns plugin name.
25
	 *
26
	 * @return string
27
	 */
28 11
	public function getName()
29
	{
30 11
		return 'projects';
31
	}
32
33
	/**
34
	 * Defines parsing statistic types.
35
	 *
36
	 * @return array
37
	 */
38 15
	public function defineStatisticTypes()
39
	{
40
		return array(
41 15
			self::STATISTIC_PROJECT_DELETED,
42 15
			self::STATISTIC_PROJECT_RESTORED,
43 15
		);
44
	}
45
46
	/**
47
	 * Processes data.
48
	 *
49
	 * @param integer $from_revision From revision.
50
	 * @param integer $to_revision   To revision.
51
	 *
52
	 * @return void
53
	 */
54 6
	public function doProcess($from_revision, $to_revision)
55
	{
56 6
		$projects = $this->getProjects();
57 6
		$this->setLastRevision($to_revision);
58
59
		// When no projects exists and there 20+ commits, then consider repository
60
		// having single project without known structure (trunk/branches/tags) only.
61 6
		if ( !$projects && $to_revision >= 20 ) {
62 2
			$this->createRepositoryWideProject($to_revision);
63 2
		}
64
65 6
		foreach ( $projects as $project_data ) {
66 3
			$is_deleted = $project_data['IsDeleted'];
67
68 3
			if ( $is_deleted && !$project_data['RevisionDeleted'] ) {
69 2
				$this->repositoryFiller->setProjectStatus($project_data['Id'], 0);
70 2
				$this->recordStatistic(self::STATISTIC_PROJECT_RESTORED);
71 2
			}
72 1
			elseif ( !$is_deleted && $project_data['RevisionDeleted'] ) {
73 1
				$this->repositoryFiller->setProjectStatus($project_data['Id'], 1);
74 1
				$this->recordStatistic(self::STATISTIC_PROJECT_DELETED);
75 1
			}
76 6
		}
77
78 6
		$this->advanceProgressBar();
79 6
	}
80
81
	/**
82
	 * Creates one project per repository and moves all commits into it.
83
	 *
84
	 * @param integer $revision Revision.
85
	 *
86
	 * @return void
87
	 */
88 2
	protected function createRepositoryWideProject($revision)
0 ignored issues
show
Unused Code introduced by
The parameter $revision is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
	{
90 2
		$select_sql = 'SELECT Id FROM Paths WHERE ProjectPath = :project_path LIMIT 100';
91
92 2
		while ( true ) {
93 2
			$path_ids = $this->database->fetchCol($select_sql, array('project_path' => ''));
94
95 2
			if ( !$path_ids ) {
96 2
				break;
97
			}
98
99 1
			$this->repositoryFiller->movePathsIntoProject($path_ids, '/');
100 1
		}
101
102 2
		$project_id = $this->repositoryFiller->addProject('/');
103
104
		$sql = 'SELECT Revision
105 2
				FROM Commits';
106 2
		$all_commits = new ColIterator($this->database->perform($sql));
107
108 2
		foreach ( $all_commits as $revision ) {
109 1
			$this->repositoryFiller->addCommitToProject($revision, $project_id);
110 2
		}
111 2
	}
112
113
	/**
114
	 * Find revisions by collected data.
115
	 *
116
	 * @param array  $criteria     Criteria.
117
	 * @param string $project_path Project path.
118
	 *
119
	 * @return array
120
	 * @throws \InvalidArgumentException When some of given project paths are not found.
121
	 */
122 4
	public function find(array $criteria, $project_path)
123
	{
124 4
		if ( !$criteria ) {
125 1
			return array();
126
		}
127
128
		$sql = 'SELECT Path, Id
129
				FROM Projects
130 3
				WHERE Path IN (:paths)';
131 3
		$projects = $this->database->fetchPairs($sql, array('paths' => $criteria));
132
133 3
		$missing_projects = array_diff($criteria, array_keys($projects));
134
135 3
		if ( $missing_projects ) {
136 1
			throw new \InvalidArgumentException(sprintf(
137 1
				'The "%s" project(-s) not found by "%s" plugin.',
138 1
				implode('", "', $missing_projects),
139 1
				$this->getName()
140 1
			));
141
		}
142
143
		$sql = 'SELECT DISTINCT Revision
144
				FROM CommitProjects
145 2
				WHERE ProjectId IN (:project_ids)';
146 2
		$project_revisions = $this->database->fetchCol($sql, array('project_ids' => $projects));
147
148 2
		sort($project_revisions, SORT_NUMERIC);
149
150 2
		return $project_revisions;
151
	}
152
153
	/**
154
	 * Returns information about revisions.
155
	 *
156
	 * @param array $revisions Revisions.
157
	 *
158
	 * @return array
159
	 */
160 1
	public function getRevisionsData(array $revisions)
161
	{
162 1
		$results = array();
163 1
		$all_projects = $this->getAllProjects();
164
165
		$sql = 'SELECT Revision, ProjectId
166
				FROM CommitProjects
167 1
				WHERE Revision IN (:revisions)';
168 1
		$revisions_data = $this->database->fetchAll($sql, array('revisions' => $revisions));
169
170 1
		foreach ( $revisions_data as $revision_data ) {
171 1
			$revision = $revision_data['Revision'];
172 1
			$project_path = $all_projects[$revision_data['ProjectId']];
173
174 1
			if ( !isset($results[$revision]) ) {
175 1
				$results[$revision] = array();
176 1
			}
177
178 1
			$results[$revision][] = $project_path;
179 1
		}
180
181 1
		return $this->addMissingResults($revisions, $results);
182
	}
183
184
	/**
185
	 * Returns all projects.
186
	 *
187
	 * @return array
188
	 */
189 1
	protected function getAllProjects()
190
	{
191
		$sql = 'SELECT Id, Path
192 1
				FROM Projects';
193
194 1
		return $this->database->fetchPairs($sql);
195
	}
196
197
}
198