Completed
Pull Request — develop (#1394)
by Naveen
02:56
created

Config::get_params()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Configuration;
4
5
6
class Config {
7
	/**
8
	 * @var \Wordlift_Admin_Setup
9
	 */
10
	private $admin_setup;
11
	/**
12
	 * @var \Wordlift_Key_Validation_Service
13
	 */
14
	private $key_validation_service;
15
16
	/**
17
	 * Config constructor.
18
	 *
19
	 * @param $admin_setup \Wordlift_Admin_Setup
20
	 * @param $key_validation_service \Wordlift_Key_Validation_Service
21
	 */
22
	public function __construct( $admin_setup, $key_validation_service ) {
23
24
		$this->admin_setup            = $admin_setup;
25
		$this->key_validation_service = $key_validation_service;
26
		add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) );
27
28
	}
29
30
	/**
31
	 * Check if the key is valid and also not bound to any domain.
32
	 *
33
	 * @param $key string
34
	 *
35
	 * @return bool
36
	 */
37
	private function is_key_valid_and_not_bound_to_any_domain( $key ) {
38
		$account_info = $this->key_validation_service->get_account_info( $key );
39
40
		/**
41
		 * we need to check if the key is not associated with any account
42
		 * before setting it, we should check if the url is null.
43
		 */
44
		if ( is_wp_error( $account_info )
45
		     || wp_remote_retrieve_response_code( $account_info ) !== 200 ) {
46
			return false;
47
		}
48
49
		$account_info_json = $account_info['body'];
50
51
		$account_info_data = json_decode( $account_info_json, true );
52
53
		if ( ! $account_info_data ) {
54
			// Invalid json returned by api.
55
			return false;
56
		}
57
58
		if ( $account_info_data['url'] !== null ) {
59
			// key already associated with another account.
60
			return false;
61
		}
62
63
		return true;
64
	}
65
66
67
	public function config() {
68
69
		// Perform validation check for all the parameters.
70
		$required_fields = array(
71
			'diagnostic',
72
			'vocabulary',
73
			'language',
74
			'country',
75
			'publisherName',
76
			'publisher',
77
			'license'
78
		);
79
80
		// validate all the fields before processing
81
		foreach ( $required_fields as $field ) {
82
			if ( ! array_key_exists( $field, $_POST ) ) {
83
				wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ) );
84
85
				return;
86
			}
87
		}
88
89
		$key = (string) $_POST['license'];
90
91
		if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) {
92
			wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ) );
93
94
			// exit if not valid.
95
			return;
96
		}
97
98
		$this->admin_setup->save_configuration( $this->get_params() );
99
100
101
	}
102
103
	/**
104
	 *
105
	 * @return array
106
	 */
107
	private function get_params() {
108
109
		$attachment_id = $this->may_be_get_attachment_id();
110
111
		$params = array(
112
			'key'              => (string) $_POST['license'],
113
			'vocabulary'       => (string) $_POST['vocabulary'],
114
			'wl-site-language' => (string) $_POST['language'],
115
			'wl-country-code'  => (string) $_POST['country'],
116
			'name'             => (string) $_POST['publisherName'],
117
			'user_type'        => (string) $_POST['publisher'],
118
			'logo'             => $attachment_id
119
		);
120
121
		if ( (bool) $_POST['diagnostic'] ) {
122
			$params['share-diagnostic'] = 'yes';
123
		}
124
125
		return $params;
126
	}
127
128
	/**
129
	 * @return int | bool
130
	 */
131
	private function may_be_get_attachment_id() {
132
		// if image or image extension not posted then return false.
133
		if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) {
134
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Wordlift\Configuration\C...ay_be_get_attachment_id of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
135
		}
136
137
		$allowed_extensions = array( 'png', 'jpeg', 'jpg' );
138
		$image_string       = (string) $_POST['image'];
139
		$image_ext          = (string) $_POST['imageExtension'];
140
141
		if ( ! in_array( $image_ext, $allowed_extensions ) ) {
142
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Wordlift\Configuration\C...ay_be_get_attachment_id of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
143
		}
144
145
		$image_decoded_string = base64_decode( $image_string );
146
147
		$upload_dir = wp_upload_dir();
148
149
		$file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext;
150
151
		file_put_contents( $file_path, $image_decoded_string );
152
153
		return wp_insert_attachment( array(), $file_path );
154
	}
155
156
}