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

advanceProgressBar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2.2559
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 14
	public function process($from_revision, $to_revision, ProgressBar $progress_bar = null)
36
	{
37 14
		$this->_progressBar = $progress_bar;
38
39 14
		$this->database->beginTransaction();
40 14
		$this->doProcess($from_revision, $to_revision);
41 14
		$this->database->commit();
42
43 14
		$this->freeMemoryAutomatically();
44 14
	}
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 14
	protected function advanceProgressBar()
62
	{
63 14
		if ( isset($this->_progressBar) ) {
64
			$this->_progressBar->advance();
65
		}
66 14
	}
67
68
	/**
69
	 * Returns projects.
70
	 *
71
	 * @param string $where_clause Where clause.
72
	 *
73
	 * @return array
74
	 */
75 13
	protected function getProjects($where_clause = '')
76
	{
77
		$sql = 'SELECT *
78 13
				FROM Projects';
79
80 13
		if ( $where_clause ) {
81 9
			$sql .= ' WHERE ' . $where_clause;
82 9
		}
83
84 13
		$projects = $this->database->fetchAll($sql);
85
86 13
		if ( !$projects ) {
87 8
			return array();
88
		}
89
90 11
		$path_hashes = array_map('crc32', $this->getField('Path', $projects));
91
92
		$sql = 'SELECT Path, Id AS PathId, RevisionAdded, RevisionDeleted, RevisionLastSeen
93
				FROM Paths
94 11
				WHERE PathHash IN (:path_hashes)';
95 11
		$paths = $this->database->fetchAssoc($sql, array('path_hashes' => $path_hashes));
96
97 11
		foreach ( $projects as $index => $project_data ) {
98 11
			$project_path = $project_data['Path'];
99 11
			$projects[$index] = array_merge($projects[$index], $paths[$project_path]);
100 11
		}
101
102 11
		return $projects;
103
	}
104
105
	/**
106
	 * Returns given column value from each array entry.
107
	 *
108
	 * @param string $field   Field.
109
	 * @param array  $records Records.
110
	 *
111
	 * @return array
112
	 */
113 11
	protected function getField($field, array $records)
114
	{
115 11
		$ret = array();
116
117 11
		foreach ( $records as $row ) {
118 11
			$ret[] = $row[$field];
119 11
		}
120
121 11
		return $ret;
122
	}
123
124
}
125