Completed
Push — master ( bf54d1...c54225 )
by Gregory
01:52
created

Config   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 98
ccs 0
cts 37
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 2
A __construct() 0 9 1
A load_config() 0 14 4
B load_wp_config() 0 18 5
1
<?php
2
namespace Tivnet\WPDB;
3
4
/**
5
 * Class Config
6
 * @package Tivnet\WPDB
7
 */
8
class Config {
9
10
	const APPLICATION_TITLE = 'WPDB: WordPress DataBase Manager';
11
12
	/**
13
	 * @var string
14
	 */
15
	const CONFIG_FILE_NAME = '.wpdb.json';
16
17
	/**
18
	 * @var string
19
	 */
20
	const WP_CONFIG_FILE_NAME = 'wp-config.php';
21
22
	/**
23
	 * @var string
24
	 */
25
	protected $path_project_root = '.';
26
27
	/**
28
	 * @var string[]
29
	 */
30
	protected $config = array();
31
32
	/**
33
	 * @var string[]
34
	 */
35
	protected static $config_defaults = array(
36
		'dir_dump'     => 'dbdump-data',
37
		'dir_web_root' => 'public',
38
		'dump_ext'     => 'sql',
39
		'cmd_ls'       => 'ls -o --time-style long-iso',
40
	);
41
42
	/**
43
	 * @param $key
44
	 *
45
	 * @return string
46
	 */
47
	public function get( $key ) {
48
		return isset( $this->config[ $key ] ) ? $this->config[ $key ] : '';
49
	}
50
51
52
	/**
53
	 * Config constructor.
54
	 */
55
	public function __construct() {
56
		$this->path_project_root = getcwd();
57
58
		$this->config = self::$config_defaults;
59
		$this->load_config();
60
		$this->load_wp_config();
61
62
//		print_r( $this->config );
63
	}
64
65
	/**
66
	 * Load configuration parameters from JSON file, if exists.
67
	 */
68
	protected function load_config() {
0 ignored issues
show
Coding Style introduced by
Method name "Config::load_config" is not in camel caps format
Loading history...
69
70
		$file_config = implode( '/', array(
71
			$this->path_project_root,
72
			self::CONFIG_FILE_NAME,
73
		) );
74
75
		if ( is_file( $file_config ) && is_readable( $file_config ) ) {
76
			$config_overwrite = json_decode( file_get_contents( $file_config ), JSON_OBJECT_AS_ARRAY );
77
			if ( is_array( $config_overwrite ) ) {
78
				$this->config = array_merge( $this->config, $config_overwrite );
1 ignored issue
show
Documentation Bug introduced by
It seems like array_merge($this->config, $config_overwrite) of type array is incompatible with the declared type array<integer,string> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
			}
80
		}
81
	}
82
83
	/**
84
	 * Read defines from WP config.
85
	 */
86
	protected function load_wp_config() {
0 ignored issues
show
Coding Style introduced by
Method name "Config::load_wp_config" is not in camel caps format
Loading history...
87
88
		$file_wp_config = implode( '/', array(
89
			$this->path_project_root,
90
			$this->get( 'dir_web_root' ),
91
			self::WP_CONFIG_FILE_NAME,
92
		) );
93
94
		if ( is_file( $file_wp_config ) && is_readable( $file_wp_config ) ) {
95
			$lines          = file( $file_wp_config );
96
			$regex_template = '/define\s*\(\s*["\'](DB_NAME|DB_USER|DB_HOST)["\']\s*,\s*["\'](.+)["\']\s*\)\s*;/';
97
			foreach ( $lines as $line ) {
98
				if ( preg_match( $regex_template, $line, $matches ) ) {
99
					$this->config[ $matches[1] ] = $matches[2];
100
				}
101
			}
102
		}
103
	}
104
105
}
106