Failed Conditions
Push — master ( 598f4b...15edf4 )
by Alexander
04:31
created

ProjectsPlugin::doProcess()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 6.7272
cc 7
eloc 15
nc 5
nop 2
crap 7
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 ProjectsPlugin extends AbstractDatabaseCollectorPlugin
15
{
16
17
	const STATISTIC_PROJECT_DELETED = 'project_deleted';
18
19
	const STATISTIC_PROJECT_RESTORED = 'project_restored';
20
21
	/**
22
	 * Returns plugin name.
23
	 *
24
	 * @return string
25
	 */
26 9
	public function getName()
27
	{
28 9
		return 'projects';
29
	}
30
31
	/**
32
	 * Defines parsing statistic types.
33
	 *
34
	 * @return array
35
	 */
36 13
	public function defineStatisticTypes()
37
	{
38
		return array(
39 13
			self::STATISTIC_PROJECT_DELETED,
40 13
			self::STATISTIC_PROJECT_RESTORED,
41 13
		);
42
	}
43
44
	/**
45
	 * Processes data.
46
	 *
47
	 * @param integer $from_revision From revision.
48
	 * @param integer $to_revision   To revision.
49
	 *
50
	 * @return void
51
	 */
52 4
	public function doProcess($from_revision, $to_revision)
53
	{
54 4
		$projects = $this->getProjects();
55 4
		$this->setLastRevision($to_revision);
56
57 4
		if ( !$projects ) {
58 1
			$this->advanceProgressBar();
59
60 1
			return;
61
		}
62
63 3
		foreach ( $projects as $project_data ) {
64 3
			$is_deleted = $project_data['IsDeleted'];
65
66 3
			if ( $is_deleted && !$project_data['RevisionDeleted'] ) {
67 2
				$this->repositoryFiller->setProjectStatus($project_data['Id'], 0);
68 2
				$this->recordStatistic(self::STATISTIC_PROJECT_RESTORED);
69 2
			}
70 1
			elseif ( !$is_deleted && $project_data['RevisionDeleted'] ) {
71 1
				$this->repositoryFiller->setProjectStatus($project_data['Id'], 1);
72 1
				$this->recordStatistic(self::STATISTIC_PROJECT_DELETED);
73 1
			}
74 3
		}
75
76 3
		$this->advanceProgressBar();
77 3
	}
78
79
	/**
80
	 * Find revisions by collected data.
81
	 *
82
	 * @param array  $criteria     Criteria.
83
	 * @param string $project_path Project path.
84
	 *
85
	 * @return array
86
	 * @throws \InvalidArgumentException When some of given project paths are not found.
87
	 */
88 4
	public function find(array $criteria, $project_path)
89
	{
90 4
		if ( !$criteria ) {
91 1
			return array();
92
		}
93
94
		$sql = 'SELECT Path, Id
95
				FROM Projects
96 3
				WHERE Path IN (:paths)';
97 3
		$projects = $this->database->fetchPairs($sql, array('paths' => $criteria));
98
99 3
		$missing_projects = array_diff($criteria, array_keys($projects));
100
101 3
		if ( $missing_projects ) {
102 1
			throw new \InvalidArgumentException(sprintf(
103 1
				'The "%s" project(-s) not found by "%s" plugin.',
104 1
				implode('", "', $missing_projects),
105 1
				$this->getName()
106 1
			));
107
		}
108
109
		$sql = 'SELECT DISTINCT Revision
110
				FROM CommitProjects
111 2
				WHERE ProjectId IN (:project_ids)';
112 2
		$project_revisions = $this->database->fetchCol($sql, array('project_ids' => $projects));
113
114 2
		sort($project_revisions, SORT_NUMERIC);
115
116 2
		return $project_revisions;
117
	}
118
119
	/**
120
	 * Returns information about revisions.
121
	 *
122
	 * @param array $revisions Revisions.
123
	 *
124
	 * @return array
125
	 */
126 1
	public function getRevisionsData(array $revisions)
127
	{
128 1
		$results = array();
129 1
		$all_projects = $this->getAllProjects();
130
131
		$sql = 'SELECT Revision, ProjectId
132
				FROM CommitProjects
133 1
				WHERE Revision IN (:revisions)';
134 1
		$revisions_data = $this->database->fetchAll($sql, array('revisions' => $revisions));
135
136 1
		foreach ( $revisions_data as $revision_data ) {
137 1
			$revision = $revision_data['Revision'];
138 1
			$project_path = $all_projects[$revision_data['ProjectId']];
139
140 1
			if ( !isset($results[$revision]) ) {
141 1
				$results[$revision] = array();
142 1
			}
143
144 1
			$results[$revision][] = $project_path;
145 1
		}
146
147 1
		return $this->addMissingResults($revisions, $results);
148
	}
149
150
	/**
151
	 * Returns all projects.
152
	 *
153
	 * @return array
154
	 */
155 1
	protected function getAllProjects()
156
	{
157
		$sql = 'SELECT Id, Path
158 1
				FROM Projects';
159
160 1
		return $this->database->fetchPairs($sql);
161
	}
162
163
}
164