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

DatabaseManager::getDatabase()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 8.9713
cc 3
eloc 13
nc 4
nop 2
crap 12
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;
12
13
14
use Aura\Sql\ExtendedPdo;
15
use Aura\Sql\ExtendedPdoInterface;
16
use ConsoleHelpers\ConsoleKit\ConsoleIO;
17
use ConsoleHelpers\SVNBuddy\Database\Migration\MigrationManager;
18
use ConsoleHelpers\SVNBuddy\Database\Migration\MigrationContext;
19
use ConsoleHelpers\SVNBuddy\Database\StatementProfiler;
20
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
21
22
class DatabaseManager
23
{
24
25
	/**
26
	 * Working directory.
27
	 *
28
	 * @var string
29
	 */
30
	private $_workingDirectory;
31
32
	/**
33
	 * Migration manager.
34
	 *
35
	 * @var MigrationManager
36
	 */
37
	private $_migrationManager;
38
39
	/**
40
	 * Statement profiler.
41
	 *
42
	 * @var StatementProfiler
43
	 */
44
	private $_statementProfiler;
45
46
	/**
47
	 * Database manager constructor.
48
	 *
49
	 * @param string            $working_directory  Working directory.
50
	 * @param MigrationManager  $migration_manager  Migration manager.
51
	 * @param StatementProfiler $statement_profiler Statement profiler.
52
	 */
53 1
	public function __construct(
54
		$working_directory,
55
		MigrationManager $migration_manager,
56
		StatementProfiler $statement_profiler
57
	) {
58 1
		$this->_workingDirectory = $working_directory;
59 1
		$this->_migrationManager = $migration_manager;
60 1
		$this->_statementProfiler = $statement_profiler;
61 1
	}
62
63
	/**
64
	 * Returns db for given repository.
65
	 *
66
	 * @param string    $repository_url Repository url.
67
	 * @param ConsoleIO $io             Console IO.
68
	 *
69
	 * @return ExtendedPdoInterface
70
	 */
71
	public function getDatabase($repository_url, ConsoleIO $io = null)
72
	{
73
		if ( preg_match(Connector::URL_REGEXP, $repository_url, $regs) ) {
74
			$sub_folder = $regs[2] . $regs[3] . $regs[4];
75
		}
76
		else {
77
			$sub_folder = 'misc';
78
		}
79
80
		$parent_path = $this->_workingDirectory . '/' . $sub_folder;
81
82
		if ( !file_exists($parent_path) ) {
83
			mkdir($parent_path, 0777, true);
84
		}
85
86
		$db = new ExtendedPdo('sqlite:' . $parent_path . '/log_' . crc32($repository_url) . '.sqlite');
87
88
		$profiler = clone $this->_statementProfiler;
89
		$profiler->setIO($io);
90
91
		$db->setProfiler($profiler);
92
93
		return $db;
94
	}
95
96
	/**
97
	 * Runs outstanding migrations on the database.
98
	 *
99
	 * @param MigrationContext $context Context.
100
	 *
101
	 * @return void
102
	 */
103
	public function runMigrations(MigrationContext $context)
104
	{
105
		$this->_migrationManager->run($context);
106
	}
107
108
}
109