Completed
Pull Request — master (#111)
by Luca
02:07
created

ImportLogger::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport;
4
5
6
use lloc\Msls\ContentImport\LogWriters\AdminNoticeLogger;
7
use lloc\Msls\ContentImport\LogWriters\LogWriter;
8
9
class ImportLogger {
10
11
	protected $levels_delimiter = '/';
12
13
	protected $data = [
14
		'info'    => [],
15
		'error'   => [],
16
		'success' => [],
17
	];
18
19
	/**
20
	 * @var ImportCoordinates
21
	 */
22
	protected $import_coordinates;
23
24
	public function __construct( ImportCoordinates $import_coordinates ) {
25
		$this->import_coordinates = $import_coordinates;
26
	}
27
28
	/**
29
	 * Merges the specified log data into this log.
30
	 *
31
	 * @param ImportLogger|null $logger
32
	 */
33
	public function merge( ImportLogger $logger = null ) {
34
		if ( null === $logger ) {
35
			return;
36
		}
37
38
		$this->data = array_merge_recursive( $this->data, $logger->get_data() );
39
	}
40
41
	/**
42
	 * @return array
43
	 */
44
	public function get_data() {
45
		return $this->data;
46
	}
47
48
	/**
49
	 * Saves the log or prints it some place.
50
	 */
51
	public function save() {
52
		$log_writer = $default_log_writer = AdminNoticeLogger::instance();
53
		$log_writer->set_import_coordinates( $this->import_coordinates );
54
55
		/**
56
		 * Filters the log class or object that should be used to write the log to the destination.
57
		 *
58
		 * @param LogWriter $log_writer
59
		 * @param ImportCoordinates $import_coordinates
60
		 */
61
		$log_writer = apply_filters( 'msls_content_import_log_writer', $log_writer, $this->import_coordinates );
62
63
		if ( empty( $log_writer ) ) {
64
			// we assume that was done on purpose to prevent logging
65
			return;
66
		}
67
68
		if ( is_string( $log_writer ) ) {
69
			$log_writer = new $log_writer;
70
		}
71
72
		if ( ! $log_writer instanceof LogWriter ) {
73
			// something is fishy, let's use the default one
74
			$log_writer = $default_log_writer;
75
		}
76
77
		$log_writer->write( $this->get_data() );
78
	}
79
80
	/**
81
	 * Logs an error.
82
	 *
83
	 * @param string $where A location string using `/` as level format.
84
	 * @param mixed  $what  What should be stored in the log.
85
	 */
86
	public function log_error( $where, $what ) {
87
		$this->log( $where, $what, 'error' );
88
	}
89
90
	/**
91
	 * Logs something.
92
	 *
93
	 * @param string $where A location string using `/` as level format.
94
	 * @param mixed  $what  What should be stored in the log.
95
	 * @param string $root  Where to log the information.
96
	 */
97
	protected function log( $where, $what, $root = 'info' ) {
98
		if ( ! isset( $this->data[ $root ] ) ) {
99
			$this->data[ $root ] = [];
100
		}
101
102
		$data = $this->build_nested_array( $this->build_path( $where ), $what );
103
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
104
105
		$this->data[ $root ] = array_merge_recursive( $this->data[ $root ], $data );
106
	}
107
108
	protected function build_nested_array( $path, $what = '' ) {
109
		$json = '{"'
110
		        . implode( '":{"', $path )
111
		        . '":' . json_encode( $what )
112
		        . implode(
113
			        '',
114
			        array_fill( 0, count( $path ),
115
				        '}' )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
116
		        );
117
		$data = json_decode( $json, true );
118
119
		return $data;
120
	}
121
122
	/**
123
	 * @param $where
124
	 *
125
	 * @return array
126
	 */
127
	protected function build_path( $where ) {
128
		$where_path = explode( $this->levels_delimiter, $where );
129
130
		return $where_path;
131
	}
132
133
	/**
134
	 * Returns the string that will be used to split paths into levels.
135
	 *
136
	 * @return string
137
	 */
138
	public function get_levels_delimiter() {
139
		return $this->levels_delimiter;
140
	}
141
142
	/**
143
	 * Sets the string that will be used to split paths into levels.
144
	 *
145
	 * @param string $levels_delimiter
146
	 */
147
	public function set_levels_delimiter( $levels_delimiter ) {
148
		$this->levels_delimiter = $levels_delimiter;
149
	}
150
151
	/**
152
	 * Logs a success.
153
	 *
154
	 * @param string $where A location string using `/` as level format.
155
	 * @param mixed  $what  What should be stored in the log.
156
	 */
157
	public function log_success( $where, $what ) {
158
		$this->log( $where, $what, 'success' );
159
	}
160
161
	/**
162
	 * Logs some generic information.
163
	 *
164
	 * @param string $message
165
	 */
166
	public function log_information( $key, $message ) {
167
		$this->data['info'][ $key ] = $message;
168
	}
169
170
	public function get_error( $where ) {
171
		return $this->get_nested_value( 'error' . $this->levels_delimiter . $where );
172
	}
173
174
	/**
175
	 * @param $where
176
	 *
177
	 * @return mixed
178
	 */
179
	protected function get_nested_value( $where ) {
180
		$path = $this->build_path( $where );
181
182
		$data = $this->data[ array_shift( $path ) ];
183
184
		foreach ( $path as $frag ) {
185
			$data = $data[ $frag ];
186
		}
187
188
		return $data;
189
	}
190
191
	public function get_success( $where ) {
192
		return $this->get_nested_value( 'success' . $this->levels_delimiter . $where );
193
	}
194
195
	public function get_information( $key ) {
196
		return isset( $this->data['info'][ $key ] ) ? $this->data['info'][ $key ] : '';
197
	}
198
}