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

AdminNoticeLogger::write()   D

Complexity

Conditions 14
Paths 374

Size

Total Lines 61
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 44
nc 374
nop 1
dl 0
loc 61
rs 4.6017
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace lloc\Msls\ContentImport\LogWriters;
4
5
6
use lloc\Msls\ContentImport\ImportCoordinates;
7
use lloc\Msls\MslsRegistryInstance;
8
9
class AdminNoticeLogger extends MslsRegistryInstance implements LogWriter {
10
	protected $transient = 'msls_last_import_log';
11
12
	/**
13
	 * @var ImportCoordinates
14
	 */
15
	protected $import_coordinates;
16
17
	public function write( array $data ) {
18
		$message        = '<h3>' . esc_html__( 'Multisite Language Switcher last import report', 'multisite-language-switcher' ) . '</h3>';
19
		$message        .= '<b>' . sprintf(
20
				esc_html__( 'From post %d on site %d to post %d on site %d', 'multisite-language-switcher' ),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
21
				$this->import_coordinates->source_post_id,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
22
				$this->import_coordinates->source_blog_id,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
23
				$this->import_coordinates->dest_post_id,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
24
				$this->import_coordinates->dest_blog_id
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
25
			) . '</b>';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
26
		if ( ! empty( $data['info'] ) ) {
27
			$section_title = esc_html__( 'General information', 'multisite-language-switcher' );
28
			$entries       = $data['info'];
29
			$message       .= $this->get_section_html( $section_title, $entries );
30
		}
31
32
		if ( ! empty( $data['success'] ) ) {
33
			$section_title   = esc_html__( 'Details', 'multisite-language-switcher' );
34
			$success_data    = $data['success'];
35
			$success_entries = [];
36
37
			if ( isset( $success_data['post-field']['added'] ) ) {
38
				$success_entries[] = esc_html__( 'The following post fields have been set: ', 'multisite-language-switcher' ) .
39
				                     '<code>' . implode( '</code>, <code>', array_keys( $success_data['post-field']['added'] ) ) . '</code>.';
40
			}
41
			if ( isset( $success_data['meta']['added'] ) ) {
42
				$success_entries[] = esc_html__( 'The following post meta have been set: ', 'multisite-language-switcher' ) .
43
				                     '<code>' . implode( '</code>, <code>', array_keys( $success_data['meta']['added'] ) ) . '</code>.';
44
			}
45
			if ( isset( $success_data['term']['added'] ) ) {
46
				$success_entries[] = esc_html__( 'Terms have been assigned to the post for the following taxonomies: ', 'multisite-language-switcher' ) .
47
				                     '<code>' . implode( '</code>, <code>', array_keys( $success_data['term']['added'] ) ) . '</code>.';
48
			}
49
			if ( isset( $success_data['post-thumbnail']['set'] ) ) {
50
				$success_entries[] = esc_html__( 'The post thumbnail has been set.', 'multisite-language-switcher' );
51
			}
52
53
			$message .= $this->get_section_html( $section_title, $success_entries, false );
54
		}
55
56
		if ( ! empty( $data['error'] ) ) {
57
			$section_title = esc_html__( 'Errors:', 'multisite-language-switcher' );
58
			$error_data    = $data['error'];
59
			$error_entries = [];
60
			if ( isset( $error_data['term']['added'] ) || isset( $error_data['term']['created'] ) ) {
61
				$taxonomies      = isset( $error_data['term']['added'] ) ? array_keys( $error_data['term']['added'] ) : [];
62
				$taxonomies      = isset( $error_data['term']['created'] ) ? array_merge( $taxonomies, array_keys( $error_data['term']['created'] ) ) : $taxonomies;
63
				$error_entries[] = esc_html__( 'There were issues creating or assigning terms for the following taxonomies: ', 'multisite-language-switcher' ) .
64
				                   '<code>' . implode( '</code>, <code>', $taxonomies ) . '</code>.';
65
			}
66
			if ( isset( $error_data['post-thumbnail']['set'] ) || isset( $error_data['post-thumbnail']['created'] ) ) {
67
				$error_entries[] = esc_html__( 'The post thumbnail could not be created or set.', 'multisite-language-switcher' );
68
			}
69
			$message .= $this->get_section_html( $section_title, $error_entries, false );
70
		}
71
72
		$html = '<div class="notice notice-success is-dismissible"><p>' . $message . '</p></div>';
73
74
		switch_to_blog( $this->import_coordinates->dest_blog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
75
76
		set_transient( $this->transient, $html, HOUR_IN_SECONDS );
77
	}
78
79
	protected function get_section_html( $section_title, $entries, $escape_entries = true ) {
80
		$html = '<h3>' . $section_title . '</h3>';
81
		$html .= '<ul>';
82
		foreach ( $entries as $entry ) {
83
			if ( $escape_entries ) {
84
				$html .= '<li>' . esc_html( $entry ) . '</li>';
85
			} else {
86
				$html .= '<li>' . $entry . '</li>';
87
			}
88
		}
89
		$html .= '</ul>';
90
91
		return $html;
92
	}
93
94
	public function show_last_log($echo = true) {
95
		if ( ! ( $html = get_transient( $this->transient ) ) ) {
96
			return;
97
		}
98
99
		if ( $echo ) {
100
			echo $html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
101
		}
102
103
		// we've shown it, no reason to keep it
104
		delete_transient( $this->transient );
105
106
		return $html;
107
	}
108
109
	public function set_import_coordinates( $import_coordinates ) {
110
		$this->import_coordinates = $import_coordinates;
111
	}
112
113
	/**
114
	 * Returns the name of the transient where the logger will store the output HTML.
115
	 *
116
	 * @return string
117
	 */
118
	public function get_transient() {
119
		return $this->transient;
120
	}
121
}