DatabaseConfigFile   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 51
ccs 0
cts 32
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A newInstance() 0 3 1
A __construct() 0 4 1
A write() 0 12 2
A createDirIfNeeded() 0 9 3
A read() 0 9 2
1
<?php
2
3
namespace Queryr\Replicator;
4
5
use RuntimeException;
6
7
/**
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 */
11
class DatabaseConfigFile {
12
13
	private $configDir;
14
	private $configPath;
15
16
	public static function newInstance() {
17
		return new self();
18
	}
19
20
	private function __construct() {
21
		$this->configDir = __DIR__ . '/../config/';
22
		$this->configPath = $this->configDir . 'db.json';
23
	}
24
25
	public function write( array $config ) {
26
		$this->createDirIfNeeded();
27
28
		$writeResult = @file_put_contents(
29
			$this->configPath,
30
			json_encode( $config, JSON_PRETTY_PRINT )
31
		);
32
33
		if ( $writeResult === false ) {
34
			throw new RuntimeException( 'Could not write the config file' );
35
		}
36
	}
37
38
	private function createDirIfNeeded() {
39
		if ( !is_dir( $this->configDir ) ) {
40
			$success = @mkdir( $this->configDir );
41
42
			if ( !$success ) {
43
				throw new RuntimeException( 'Could create the config directory' );
44
			}
45
		}
46
	}
47
48
	/**
49
	 * @throws RuntimeException
50
	 */
51
	public function read(): array {
52
		$configJson = @file_get_contents( $this->configPath );
53
54
		if ( $configJson === false ) {
55
			throw new RuntimeException( 'Could not read the config file' );
56
		}
57
58
		return json_decode( $configJson, true );
59
	}
60
61
}
62
63