Failed Conditions
Push — master ( d32373...a3f91c )
by Alexander
01:43
created

ProjectsPlugin   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 194
ccs 63
cts 66
cp 0.9545
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A defineStatisticTypes() 0 7 1
B doProcess() 0 26 8
A createRepositoryWideProject() 0 24 4
A find() 0 30 3
A getRevisionsData() 0 23 3
A getAllProjects() 0 7 1
A getMeta() 0 6 1
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
		);
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();
63
		}
64
65 6
		foreach ( $projects as $project_data ) {
66 3
			$is_deleted = $project_data['IsDeleted'];
67
68 3
			if ( $is_deleted && !is_numeric($project_data['RevisionDeleted']) ) {
69 2
				$this->repositoryFiller->setProjectStatus($project_data['Id'], 0);
70 2
				$this->recordStatistic(self::STATISTIC_PROJECT_RESTORED);
71
			}
72 1
			elseif ( !$is_deleted && is_numeric($project_data['RevisionDeleted']) ) {
73 1
				$this->repositoryFiller->setProjectStatus($project_data['Id'], 1);
74 1
				$this->recordStatistic(self::STATISTIC_PROJECT_DELETED);
75
			}
76
		}
77
78 6
		$this->advanceProgressBar();
79 6
	}
80
81
	/**
82
	 * Creates one project per repository and moves all commits into it.
83
	 *
84
	 * @return void
85
	 */
86 2
	protected function createRepositoryWideProject()
87
	{
88 2
		$select_sql = 'SELECT Id FROM Paths WHERE ProjectPath = :project_path LIMIT 100';
89
90 2
		while ( true ) {
91 2
			$path_ids = $this->database->fetchCol($select_sql, array('project_path' => ''));
92
93 2
			if ( !$path_ids ) {
94 2
				break;
95
			}
96
97 1
			$this->repositoryFiller->movePathsIntoProject($path_ids, '/');
98
		}
99
100 2
		$project_id = $this->repositoryFiller->addProject('/');
101
102 2
		$sql = 'SELECT Revision
103
				FROM Commits';
104 2
		$all_commits = new ColIterator($this->database->perform($sql));
105
106 2
		foreach ( $all_commits as $revision ) {
107 1
			$this->repositoryFiller->addCommitToProject($revision, $project_id);
108
		}
109 2
	}
110
111
	/**
112
	 * Find revisions by collected data.
113
	 *
114
	 * @param array  $criteria     Criteria.
115
	 * @param string $project_path Project path.
116
	 *
117
	 * @return array
118
	 * @throws \InvalidArgumentException When some of given project paths are not found.
119
	 */
120 4
	public function find(array $criteria, $project_path)
121
	{
122 4
		if ( !$criteria ) {
123 1
			return array();
124
		}
125
126 3
		$sql = 'SELECT Path, Id
127
				FROM Projects
128
				WHERE Path IN (:paths)';
129 3
		$projects = $this->database->fetchPairs($sql, array('paths' => $criteria));
130
131 3
		$missing_projects = array_diff($criteria, array_keys($projects));
132
133 3
		if ( $missing_projects ) {
134 1
			throw new \InvalidArgumentException(sprintf(
135 1
				'The "%s" project(-s) not found by "%s" plugin.',
136 1
				implode('", "', $missing_projects),
137 1
				$this->getName()
138
			));
139
		}
140
141 2
		$sql = 'SELECT DISTINCT Revision
142
				FROM CommitProjects
143
				WHERE ProjectId IN (:project_ids)';
144 2
		$project_revisions = $this->database->fetchCol($sql, array('project_ids' => $projects));
145
146 2
		sort($project_revisions, SORT_NUMERIC);
147
148 2
		return $project_revisions;
149
	}
150
151
	/**
152
	 * Returns information about revisions.
153
	 *
154
	 * @param array $revisions Revisions.
155
	 *
156
	 * @return array
157
	 */
158 1
	public function getRevisionsData(array $revisions)
159
	{
160 1
		$results = array();
161 1
		$all_projects = $this->getAllProjects();
162
163 1
		$sql = 'SELECT Revision, ProjectId
164
				FROM CommitProjects
165
				WHERE Revision IN (:revisions)';
166 1
		$revisions_data = $this->database->fetchAll($sql, array('revisions' => $revisions));
167
168 1
		foreach ( $revisions_data as $revision_data ) {
169 1
			$revision = $revision_data['Revision'];
170 1
			$project_path = $all_projects[$revision_data['ProjectId']];
171
172 1
			if ( !isset($results[$revision]) ) {
173 1
				$results[$revision] = array();
174
			}
175
176 1
			$results[$revision][] = $project_path;
177
		}
178
179 1
		return $this->addMissingResults($revisions, $results);
180
	}
181
182
	/**
183
	 * Returns all projects.
184
	 *
185
	 * @return array
186
	 */
187 1
	protected function getAllProjects()
188
	{
189 1
		$sql = 'SELECT Id, Path
190
				FROM Projects';
191
192 1
		return $this->database->fetchPairs($sql);
193
	}
194
195
	/**
196
	 * Returns project meta information.
197
	 *
198
	 * @param string $project_path Project path.
199
	 *
200
	 * @return array
201
	 */
202
	public function getMeta($project_path)
203
	{
204
		$projects = $this->getProjects('Id = :id', array('id' => $this->getProject($project_path)));
205
206
		return reset($projects);
207
	}
208
209
}
210