Completed
Pull Request — develop (#1394)
by Naveen
03:05
created

Config::config()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 0
dl 0
loc 59
rs 8.2723
c 0
b 0
f 0

How to fix   Long Method   

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 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
	 * Raw byte array from image
32
	 *
33
	 * @param $image_string
34
	 *
35
	 * Returns png or jpeg based on header, or else returns false.
36
	 *
37
	 * @return bool|int|string
38
	 */
39
	public function get_mime_type_from_string( $image_string ) {
40
		$mime_types = array(
41
			'jpeg' => "\xFF\xD8\xFF",
42
			'png'  => "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
43
		);
44
45
		foreach ( $mime_types as $mime_type => $byte_value ) {
46
			if ( ! $byte_value === substr( $image_string, 0, strlen( $byte_value ) ) ) {
47
				continue;
48
			}
49
50
			return $mime_type;
51
		}
52
53
		return false;
54
	}
55
56
	public function config() {
57
58
		$account_info = $this->key_validation_service->get_account_info( (string) $_POST['license'] );
59
60
		/**
61
		 * we need to check if the key is not associated with any account
62
		 * before setting it, we should check if the url is null.
63
		 */
64
		if ( is_wp_error( $account_info )
65
		     || wp_remote_retrieve_response_code( $account_info ) !== 200 ) {
66
			return;
67
		}
68
69
		$account_info_json = $account_info['body'];
70
71
		$account_info_data = json_decode( $account_info_json, true );
72
73
		if ( ! $account_info_data ) {
74
			// Invalid json returned by api.
75
			return;
76
		}
77
78
		if ( $account_info_data['url'] !== null ) {
79
			// key already associated with another account.
80
			return;
81
		}
82
83
		$image_string = (string) $_POST['image'];
84
85
		$image_decoded_string = base64_decode( $image_string );
86
87
		$upload_dir = wp_upload_dir();
88
89
		$mime_type = $this->get_mime_type_from_string( $image_decoded_string );
90
91
		if ( ! $mime_type ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mime_type of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92
			wp_send_json_error( "Image type not valid" );
93
		}
94
95
		$file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $mime_type;
96
97
		file_put_contents( $file_path, $image_decoded_string );
98
99
		$attachment_id = wp_insert_attachment( array(), $file_path );
100
101
		$params = array(
102
			'send_diagnostic' => $_POST['diagnostic'],
103
			'key'             => $_POST['license'],
104
			'vocabulary'      => $_POST['vocabulary'],
105
			'language'        => $_POST['language'],
106
			'name'            => $_POST['publisherName'],
107
			'user_type'       => $_POST['publisher'],
108
			'logo'            => $attachment_id
109
		);
110
111
		$this->admin_setup->save_configuration( $params );
112
113
114
	}
115
116
}