Completed
Push — feature/replace-yarn-1-with-pn... ( a7a72b...b69108 )
by
unknown
649:09 queued 639:32
created

Initial_State   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 56
loc 56
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_data() 12 12 1
A can_use_connection_iframe() 19 19 4
A render() 5 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * The React initial state.
4
 *
5
 * @package automattic/jetpack-backup
6
 */
7
8
use Automattic\Jetpack\Constants;
9
use Automattic\Jetpack\Device_Detection\User_Agent_Info;
10
11
/**
12
 * The React initial state.
13
 */
14 View Code Duplication
class Initial_State {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
	/**
16
	 * Get the initial state data.
17
	 *
18
	 * @return array
19
	 */
20
	private function get_data() {
21
		return array(
22
			'API'            => array(
23
				'WP_API_root'       => esc_url_raw( rest_url() ),
24
				'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ),
25
				'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
26
			),
27
			'connectionData' => array(
28
				'doNotUseConnectionIframe' => ! $this->can_use_connection_iframe(),
29
			),
30
		);
31
	}
32
33
	/**
34
	 * Whether we can the connection iframe.
35
	 *
36
	 * @return bool
37
	 */
38
	private function can_use_connection_iframe() {
39
		global $is_safari;
40
41
		/**
42
		 * Filters whether the connection manager should use the iframe authorization
43
		 * flow instead of the regular redirect-based flow.
44
		 *
45
		 * @since 8.3.0
46
		 *
47
		 * @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false.
48
		 */
49
		$iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false );
50
51
		if ( ! $iframe_flow ) {
52
			return false;
53
		}
54
55
		return ! $is_safari && ! User_Agent_Info::is_opera_desktop() && ! Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' );
56
	}
57
58
	/**
59
	 * Render the initial state into a JavaScript variable.
60
	 *
61
	 * @return string
62
	 */
63
	public function render() {
64
		add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' );
65
66
		return 'var JPBACKUP_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));';
67
	}
68
69
}
70