Completed
Push — wpcom/json-endpoints-2017-01-1... ( 87c71c...1c58b3 )
by
unknown
12:29 queued 03:48
created

callback()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 12
nop 2
dl 0
loc 34
rs 4.8196
c 0
b 0
f 0

How to fix   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
/**
4
 * @autounit api logo
5
 */
6
class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint {
7
	function callback( $path = '', $site_id = 0 ) {
8
		// Switch to the given blog.
9
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
10
		if ( is_wp_error( $blog_id ) ) {
11
			return $blog_id;
12
		}
13
14
		if ( ! current_user_can( 'edit_theme_options' ) ) {
15
			return new WP_Error( 'unauthorized', 'User is not authorized to access logo settings', 403 );
16
		}
17
18
		if ( strpos( $path, '/delete' ) ) {
19
			delete_option( 'site_logo' );
20
			return array();
21
		}
22
23
		$args = $this->input();
24
		$logo_settings = $this->get_current_settings();
25
		if ( empty( $args ) || ! is_array( $args ) ) {
26
			return $logo_settings;
27
		}
28
29
		if ( isset( $args['id'] ) ) {
30
			$logo_settings['id'] = intval( $args['id'], 10 );
31
		}
32
		if ( isset( $args['url'] ) ) {
33
			$logo_settings['url'] = $args['url'];
34
		}
35
		if ( isset( $args['url'] ) || isset( $args['id'] ) ) {
36
			update_option( 'site_logo', $logo_settings );
37
		}
38
39
		return $this->get_current_settings();
40
	}
41
42
	function get_current_settings() {
43
		$logo_settings = get_option( 'site_logo' );
44
		if ( ! is_array( $logo_settings ) ) {
45
			$logo_settings = array();
46
		}
47
		return $logo_settings;
48
	}
49
}
50
51