Completed
Push — master ( 59ac10...3a6c85 )
by Alexander
12s
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.0625

Importance

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