InstallerTrait::writeError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
}