Failed Conditions
Push — master ( 5d2ea6...f862f5 )
by Alexander
12:15
created

DatabaseManager::getForks()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 24
ccs 2
cts 2
cp 1
crap 4
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Code-Insight 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/code-insight
9
 */
10
11
namespace ConsoleHelpers\CodeInsight\KnowledgeBase;
12
13
14
use Aura\Sql\ExtendedPdo;
15
use Aura\Sql\ExtendedPdoInterface;
16
use ConsoleHelpers\DatabaseMigration\MigrationManager;
17
use ConsoleHelpers\DatabaseMigration\MigrationContext;
18
19
class DatabaseManager
20
{
21
22
	/**
23
	 * Migration manager.
24
	 *
25
	 * @var MigrationManager
26
	 */
27
	private $_migrationManager;
28
29
	/**
30
	 * Database directory.
31
	 *
32
	 * @var string
33
	 */
34
	private $_databaseDirectory;
35
36
	/**
37
	 * Database manager constructor.
38
	 *
39
	 * @param MigrationManager $migration_manager Migration manager.
40
	 * @param string           $working_directory Working directory.
41
	 */
42 6
	public function __construct(MigrationManager $migration_manager, $working_directory)
43
	{
44 6
		$this->_migrationManager = $migration_manager;
45 6
		$this->_databaseDirectory = $working_directory . '/databases';
46
47 6
		if ( !file_exists($this->_databaseDirectory) ) {
48 6
			mkdir($this->_databaseDirectory);
49
		}
50 6
	}
51
52
	/**
53
	 * Returns db for given project.
54
	 *
55
	 * @param string      $project_path Project path.
56
	 * @param string|null $fork         Fork name.
57
	 *
58
	 * @return ExtendedPdoInterface
59
	 * @throws \InvalidArgumentException When relative project path is given.
60
	 */
61 4
	public function getDatabase($project_path, $fork = null)
62
	{
63 4
		if ( strpos($project_path, '/') !== 0 ) {
64 1
			throw new \InvalidArgumentException('The "$project_path" argument must contain absolute path.');
65
		}
66
67 3
		$project_path = $this->_databaseDirectory . $project_path;
68
69 3
		if ( !file_exists($project_path) ) {
70 3
			mkdir($project_path, 0777, true);
71
		}
72
73 3
		$db_file = $project_path . '/code_insight.sqlite';
74
75 3
		if ( empty($fork) ) {
76 2
			return new ExtendedPdo('sqlite:' . $db_file);
77
		}
78
79 2
		$fork_db_file = $project_path . '/code_insight-' . $fork . '.sqlite';
80
81 2
		if ( !file_exists($fork_db_file) && file_exists($db_file) ) {
82 1
			copy($db_file, $fork_db_file);
83
		}
84
85 2
		return new ExtendedPdo('sqlite:' . $fork_db_file);
86
	}
87
88
	/**
89
	 * Returns forks for given project.
90
	 *
91
	 * @param string $project_path Project path.
92
	 *
93
	 * @return string[]
94
	 * @throws \InvalidArgumentException When relative project path is given.
95 1
	 */
96
	public function getForks($project_path)
97 1
	{
98 1
		if ( strpos($project_path, '/') !== 0 ) {
99
			throw new \InvalidArgumentException('The "$project_path" argument must contain absolute path.');
100
		}
101
102
		$project_path = $this->_databaseDirectory . $project_path;
103
104
		if ( !file_exists($project_path) ) {
105
			return array();
106
		}
107
108
		$ret = array();
109
		$absolute_forks = glob($project_path . '/code_insight-*.sqlite');
110
111
		foreach ( $absolute_forks as $absolute_fork ) {
112
			$ret[] = preg_replace(
113
				'/^' . preg_quote($project_path . '/code_insight-', '/') . '(.+).sqlite/',
114
				'$1',
115
				$absolute_fork
116
			);
117
		}
118
119
		return $ret;
120
	}
121
122
	/**
123
	 * Runs outstanding migrations on the database.
124
	 *
125
	 * @param MigrationContext $context Context.
126
	 *
127
	 * @return void
128
	 */
129
	public function runMigrations(MigrationContext $context)
130
	{
131
		$this->_migrationManager->run($context);
132
	}
133
134
}
135