Completed
Push — master ( 24b021...4f2edc )
by Alexander
19s queued 16s
created

AbstractDatabaseCollectorPlugin::getProjects()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 31
ccs 19
cts 19
cp 1
rs 9.7666
c 1
b 0
f 0
cc 4
nc 6
nop 2
crap 4
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 Symfony\Component\Console\Helper\ProgressBar;
15
16
abstract class AbstractDatabaseCollectorPlugin extends AbstractPlugin implements IDatabaseCollectorPlugin
17
{
18
19
	/**
20
	 * Progress bar.
21
	 *
22
	 * @var ProgressBar
23
	 */
24
	private $_progressBar;
25
26
	/**
27
	 * Processes data.
28
	 *
29
	 * @param integer     $from_revision From revision.
30
	 * @param integer     $to_revision   To revision.
31
	 * @param ProgressBar $progress_bar  Progress bar.
32
	 *
33
	 * @return void
34
	 */
35 18
	public function process($from_revision, $to_revision, ProgressBar $progress_bar = null)
36
	{
37 18
		$this->_progressBar = $progress_bar;
38
39 18
		$this->database->beginTransaction();
40 18
		$this->doProcess($from_revision, $to_revision);
41 18
		$this->database->commit();
42
43 18
		$this->freeMemoryAutomatically();
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
	abstract public function doProcess($from_revision, $to_revision);
55
56
	/**
57
	 * Advanced progress bar.
58
	 *
59
	 * @return void
60
	 */
61 18
	protected function advanceProgressBar()
62
	{
63 18
		if ( isset($this->_progressBar) ) {
64
			$this->_progressBar->advance();
65
		}
66
	}
67
68
	/**
69
	 * Returns projects.
70
	 *
71
	 * @param string $where_clause Where clause.
72
	 * @param array  $values       Values.
73
	 *
74
	 * @return array
75
	 */
76 17
	protected function getProjects($where_clause = '', array $values = array())
77
	{
78 17
		$sql = 'SELECT *
79 17
				FROM Projects';
80
81 17
		if ( $where_clause ) {
82 11
			$sql .= ' WHERE ' . $where_clause;
83
		}
84
85 17
		$projects = $this->database->fetchAll($sql, $values);
86
87 17
		if ( !$projects ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $projects 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...
88 10
			return array();
89
		}
90
91 13
		$path_hashes = array_map(
92 13
			array($this->repositoryFiller, 'getPathChecksum'),
93 13
			$this->getField('Path', $projects)
94 13
		);
95
96 13
		$sql = 'SELECT Path, Id AS PathId, RevisionAdded, RevisionDeleted, RevisionLastSeen
97
				FROM Paths
98 13
				WHERE PathHash IN (:path_hashes)';
99 13
		$paths = $this->database->fetchAssoc($sql, array('path_hashes' => $path_hashes));
100
101 13
		foreach ( $projects as $index => $project_data ) {
102 13
			$project_path = $project_data['Path'];
103 13
			$projects[$index] = array_merge($projects[$index], $paths[$project_path]);
104
		}
105
106 13
		return $projects;
107
	}
108
109
	/**
110
	 * Returns given column value from each array entry.
111
	 *
112
	 * @param string $field   Field.
113
	 * @param array  $records Records.
114
	 *
115
	 * @return array
116
	 */
117 13
	protected function getField($field, array $records)
118
	{
119 13
		$ret = array();
120
121 13
		foreach ( $records as $row ) {
122 13
			$ret[] = $row[$field];
123
		}
124
125 13
		return $ret;
126
	}
127
128
}
129