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

AbstractPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 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\ExtendedPdoInterface;
15
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RepositoryFiller;
16
17
abstract class AbstractPlugin implements IPlugin
18
{
19
20
	/**
21
	 * Database.
22
	 *
23
	 * @var ExtendedPdoInterface
24
	 */
25
	protected $database;
26
27
	/**
28
	 * Repository filler.
29
	 *
30
	 * @var RepositoryFiller
31
	 */
32
	protected $repositoryFiller;
33
34
	/**
35
	 * Parsing statistics.
36
	 *
37
	 * @var array
38
	 */
39
	private $_statistics = array();
40
41
	/**
42
	 * Creates plugin instance.
43
	 *
44
	 * @param ExtendedPdoInterface $database          Database.
45
	 * @param RepositoryFiller     $repository_filler Repository filler.
46
	 */
47 105
	public function __construct(ExtendedPdoInterface $database, RepositoryFiller $repository_filler)
48
	{
49 105
		$this->database = $database;
50 105
		$this->repositoryFiller = $repository_filler;
51
52 105
		$this->_resetParsingStatistics();
53 105
	}
54
55
	/**
56
	 * Resets parsing statistics.
57
	 *
58
	 * @return void
59
	 */
60 105
	private function _resetParsingStatistics()
61
	{
62 105
		$this->_statistics = array();
63
64 105
		foreach ( $this->defineStatisticTypes() as $parsing_statistic_type ) {
65 94
			$this->_statistics[$parsing_statistic_type] = 0;
66 105
		}
67 105
	}
68
69
	/**
70
	 * Hook, that is called before "RevisionLog::refresh" method call.
71
	 *
72
	 * @return void
73
	 */
74 72
	public function whenDatabaseReady()
75
	{
76
77 72
	}
78
79
	/**
80
	 * Records parsing statistics.
81
	 *
82
	 * @param string  $type   Type.
83
	 * @param integer $to_add Number to add.
84
	 *
85
	 * @return void
86
	 */
87 29
	protected function recordStatistic($type, $to_add = 1)
88
	{
89 29
		$this->_statistics[$type] += $to_add;
90 29
	}
91
92
	/**
93
	 * Returns parsing statistics.
94
	 *
95
	 * @return array
96
	 */
97 20
	public function getStatistics()
98
	{
99 20
		return $this->_statistics;
100
	}
101
102
	/**
103
	 * Adds results for missing revisions.
104
	 *
105
	 * @param array $revisions Revisions.
106
	 * @param array $results   Results.
107
	 *
108
	 * @return array
109
	 */
110 4
	protected function addMissingResults(array $revisions, array $results)
111
	{
112 4
		foreach ( $this->_getMissingRevisions($revisions, $results) as $missing_revision ) {
113 4
			$results[$missing_revision] = array();
114 4
		}
115
116 4
		return $results;
117
	}
118
119
	/**
120
	 * Adds results for missing revisions.
121
	 *
122
	 * @param array $revisions Revisions.
123
	 * @param array $results   Results.
124
	 *
125
	 * @return void
126
	 * @throws \InvalidArgumentException When some revisions are missing in results.
127
	 */
128 4
	protected function assertNoMissingRevisions(array $revisions, array $results)
129
	{
130 4
		$missing_revisions = $this->_getMissingRevisions($revisions, $results);
131
132 4
		if ( !$missing_revisions ) {
133 2
			return;
134
		}
135
136 2
		throw new \InvalidArgumentException(sprintf(
137 2
			'Revision(-s) "%s" not found by "%s" plugin.',
138 2
			implode('", "', $missing_revisions),
139 2
			$this->getName()
140 2
		));
141
	}
142
143
	/**
144
	 * Returns revisions, that are missing in results.
145
	 *
146
	 * @param array $revisions Revisions.
147
	 * @param array $results   Results.
148
	 *
149
	 * @return array
150
	 */
151 8
	private function _getMissingRevisions(array $revisions, array $results)
152
	{
153 8
		return array_diff($revisions, array_keys($results));
154
	}
155
156
	/**
157
	 * Returns last revision processed by plugin.
158
	 *
159
	 * @return integer
160
	 */
161 48
	public function getLastRevision()
162
	{
163
		$sql = 'SELECT LastRevision
164
				FROM PluginData
165 48
				WHERE Name = :name';
166 48
		$last_revision = $this->database->fetchValue($sql, array('name' => $this->getName()));
167
168 48
		return $last_revision !== false ? $last_revision : 0;
169
	}
170
171
	/**
172
	 * Sets last revision processed by plugin.
173
	 *
174
	 * @param integer $last_revision Last revision.
175
	 *
176
	 * @return void
177
	 */
178 35
	protected function setLastRevision($last_revision)
179
	{
180
		$sql = 'REPLACE INTO PluginData (Name, LastRevision)
181 35
				VALUES (:name, :last_revision)';
182 35
		$this->database->perform($sql, array('name' => $this->getName(), 'last_revision' => $last_revision));
183 35
	}
184
185
	/**
186
	 * Finds project by path.
187
	 *
188
	 * @param string $path Path.
189
	 *
190
	 * @return integer
191
	 * @throws \InvalidArgumentException When project can't be found.
192
	 */
193 26
	protected function getProject($path)
194
	{
195
		$sql = 'SELECT Id
196
				FROM Projects
197 26
				WHERE Path = :path';
198 26
		$project_id = $this->database->fetchValue($sql, array('path' => $path));
199
200 26
		if ( $project_id === false ) {
201 5
			throw new \InvalidArgumentException('The project with "' . $path . '" path not found.');
202
		}
203
204 21
		return $project_id;
205
	}
206
207
	/**
208
	 * Automatically free memory, when >200MB is used.
209
	 *
210
	 * @return void
211
	 *
212
	 * @codeCoverageIgnore
213
	 */
214
	protected function freeMemoryAutomatically()
215
	{
216
		$memory_usage = memory_get_usage();
217
218
		if ( $memory_usage > 200 * 1024 * 1024 ) {
219
			$this->freeMemoryManually();
220
		}
221
	}
222
223
	/**
224
	 * Frees consumed memory manually.
225
	 *
226
	 * @return void
227
	 *
228
	 * @codeCoverageIgnore
229
	 */
230
	protected function freeMemoryManually()
231
	{
232
		$profiler = $this->database->getProfiler();
233
234
		if ( is_object($profiler) && $profiler->isActive() ) {
235
			$profiler->resetProfiles();
236
		}
237
	}
238
239
}
240