Passed
Push — master ( dfea48...2fc9d6 )
by Alexander
07:48 queued 05:22
created

DatabaseManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 26.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 1
b 0
f 0
dl 0
loc 84
ccs 5
cts 19
cp 0.2632
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A runMigrations() 0 3 1
A getDatabase() 0 23 3
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\DatabaseMigration\MigrationContext;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ConsoleHelpers\SVNBuddy\...ionLog\MigrationContext. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use ConsoleHelpers\DatabaseMigration\MigrationManager;
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 2
	public function __construct(
54
		$working_directory,
55
		MigrationManager $migration_manager,
56
		StatementProfiler $statement_profiler
57
	) {
58 2
		$this->_workingDirectory = $working_directory;
59 2
		$this->_migrationManager = $migration_manager;
60 2
		$this->_statementProfiler = $statement_profiler;
61 2
	}
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 \ConsoleHelpers\DatabaseMigration\MigrationContext $context Context.
100
	 *
101
	 * @return void
102
	 */
103
	public function runMigrations(MigrationContext $context)
104
	{
105
		$this->_migrationManager->run($context);
106
	}
107
108
}
109