InstallerTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 62
ccs 0
cts 35
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A writeProgress() 0 3 1
A writeProgressEnd() 0 3 1
A writeError() 0 3 1
A tryTask() 0 13 2
A establishDatabaseConnection() 0 17 1
1
<?php
2
3
namespace Queryr\Replicator\Cli\Install;
4
5
use Doctrine\DBAL\DriverManager;
6
use Queryr\Replicator\DatabaseConfigFile;
7
use Queryr\Replicator\ServiceFactory;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
trait InstallerTrait {
16
17
	/**
18
	 * @var InputInterface
19
	 */
20
	private $input;
21
22
	/**
23
	 * @var OutputInterface
24
	 */
25
	private $output;
26
27
	/**
28
	 * @var ServiceFactory
29
	 */
30
	private $factory;
31
32
	private function writeProgress( $message ) {
33
		$this->output->write( "<comment>$message... </comment>" );
34
	}
35
36
	private function writeProgressEnd( $message = 'done.' ) {
37
		$this->output->writeln( "<comment>$message</comment>" );
38
	}
39
40
	private function writeError( $message ) {
41
		$this->output->writeln( "<error>$message</error>" );
42
	}
43
44
	private function tryTask( $message, $task ) {
45
		$this->writeProgress( $message );
46
47
		try {
48
			$returnValue = call_user_func( $task );
49
		}
50
		catch ( \Exception $ex ) {
51
			throw new InstallationException( $ex->getMessage(), 0, $ex );
52
		}
53
54
		$this->writeProgressEnd();
55
		return $returnValue;
56
	}
57
58
	private function establishDatabaseConnection() {
59
		$config = $this->tryTask(
60
			'Reading database configuration file (config/db.json)',
61
			function() {
62
				return DatabaseConfigFile::newInstance()->read();
63
			}
64
		);
65
66
		$connection = $this->tryTask(
67
			'Establishing database connection',
68
			function() use ( $config ) {
69
				return DriverManager::getConnection( $config );
70
			}
71
		);
72
73
		$this->factory = ServiceFactory::newFromConnection( $connection );
74
	}
75
76
}