Completed
Push — develop ( 5437c6...400f81 )
by David
06:08
created

Wordlift_Batch_Analysis_Adapter::submit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Adapters: Batch Analysis Adapter.
4
 *
5
 * @since      3.14.2
6
 * @package    Wordlift
7
 * @subpackage Wordlift/includes
8
 */
9
10
/**
11
 * Define the {@link Wordlift_Batch_Analysis_Adapter} class.
12
 *
13
 * @since      3.14.2
14
 * @package    Wordlift
15
 * @subpackage Wordlift/includes
16
 */
17
class Wordlift_Batch_Analysis_Adapter {
18
19
	/**
20
	 * A {@link Wordlift_Log_Service} instance.
21
	 *
22
	 * @since  3.17.0
23
	 * @access private
24
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
25
	 */
26
	private $log;
27
28
	/**
29
	 * The {@link Wordlift_Batch_Analysis_Service} instance.
30
	 *
31
	 * @since  3.17.0
32
	 * @access private
33
	 * @var \Wordlift_Batch_Analysis_Service $batch_analysis_service The {@link Wordlift_Batch_Analysis_Service} instance.
34
	 */
35
	private $batch_analysis_service;
36
37
	/**
38
	 * Wordlift_Batch_Analysis_Adapter constructor.
39
	 *
40
	 * @since 3.14.2
41
	 *
42
	 * @param \Wordlift_Batch_Analysis_Service $batch_analysis_service
43
	 */
44
	public function __construct( $batch_analysis_service ) {
45
46
		$this->log = Wordlift_Log_Service::get_logger( get_class() );
47
48
		$this->batch_analysis_service = $batch_analysis_service;
49
50
		add_action( 'wp_ajax_wl_batch_analysis_complete', array(
51
			$this,
52
			'complete',
53
		) );
54
55
	}
56
57
	/**
58
	 * Submit the posts for batch analysis.
59
	 *
60
	 * @since 3.14.2
61
	 */
62
	public function submit() {
63
64
		// Get the parameters from the $_REQUEST.
65
		$params = self::create_params_from_request();
66
67
		// Submit the request.
68
		$count = $this->batch_analysis_service->submit( $params );
69
70
		// Clear any buffer.
71
		ob_clean();
72
73
		// Send the response.
74
		wp_send_json_success( array( 'count' => $count ) );
75
76
	}
77
78
	/**
79
	 * Submit the posts for batch analysis.
80
	 *
81
	 * @since 3.14.2
82
	 */
83
	public function submit_posts() {
0 ignored issues
show
Coding Style introduced by
submit_posts uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
85
		$this->log->trace( 'Received Batch Analysis request for posts...' );
86
87
		if ( empty( $_REQUEST['post'] ) ) {
88
			$this->log->error( 'Batch Analysis request for posts missing the post(s) id.' );
89
90
			wp_send_json_error( 'The `post` parameter is required.' );
91
		}
92
93
		// Get the parameters from the $_REQUEST.
94
		$params = self::create_params_from_request();
95
96
		// Submit the request.
97
		$count = $this->batch_analysis_service->submit_posts( $params );
98
99
		// Clear any buffer.
100
		ob_clean();
101
102
		// Send the response.
103
		wp_send_json_success( array( 'count' => $count ) );
104
105
	}
106
107
	public function complete() {
108
109
		$this->batch_analysis_service->complete();
110
111
		wp_send_json_success();
112
113
	}
114
115
116
	/**
117
	 * A helper function to create the parameters from the $_REQUEST.
118
	 *
119
	 * @since 3.17.0
120
	 *
121
	 * @return array An array or parameters.
122
	 */
123
	private static function create_params_from_request() {
0 ignored issues
show
Coding Style introduced by
create_params_from_request uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
124
125
		// Build params array and check if param exists.
126
		// @codingStandardsIgnoreStart, Ignore phpcs indentation errors.
127
		$params = array(
128
			// Get the `links` parameter, or use `default` if not provided.
129
			'links' => isset( $_REQUEST['links'] ) ? $_REQUEST['links'] : 'default',
130
			// If `include_annotated` is set to `yes`, the set the parameter to true.
131
			'include_annotated' => isset( $_REQUEST['include_annotated'] ) && 'yes' === $_REQUEST['include_annotated'],
132
			// Set the minimum amount of occurrences, use `1` by default.
133
			'min_occurrences' => isset( $_REQUEST['min_occurrences'] ) && is_numeric( $_REQUEST['min_occurrences'] ) ? intval( $_REQUEST['min_occurrences'] ) : 1,
134
			// Set the `post_type` to `post` if none provided.
135
			'post_type' => isset( $_REQUEST['post_type'] ) ? (array) $_REQUEST['post_type'] : 'post',
136
			// Set the exclude array.
137
			'exclude' => isset( $_REQUEST['exclude'] ) ? (array) $_REQUEST['exclude'] : array(),
138
			// Set the `from` date, or null if not provided.
139
			'from' => isset( $_REQUEST['from'] ) ? $_REQUEST['from'] : null,
140
			// Set the `to` date, or null if not provided.
141
			'to' => isset( $_REQUEST['to'] ) ? $_REQUEST['to'] : null,
142
			//
143
			'ids' => isset( $_REQUEST['post'] ) ? wp_parse_id_list( (array) $_REQUEST['post'] ) : array(),
144
		);
145
146
		// @codingStandardsIgnoreEnd
147
148
		return $params;
149
	}
150
151
	/**
152
	 * Cancel the batch analysis for the specified post.
153
	 *
154
	 * @since 3.14.0
155
	 */
156 View Code Duplication
	public function cancel() {
0 ignored issues
show
Duplication introduced by
This method 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...
Coding Style introduced by
cancel uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
157
158
		if ( ! isset( $_REQUEST['post'] ) ) {
159
			wp_die( 'The `post` parameter is required.' );
160
		}
161
162
		$count = $this->batch_analysis_service->cancel( (array) $_REQUEST['post'] );
163
164
		// Clear any buffer.
165
		ob_clean();
166
167
		// Send the response.
168
		wp_send_json_success( array( 'count' => $count ) );
169
170
	}
171
172
	/**
173
	 * Clear warnings for the specified post.
174
	 *
175
	 * @since 3.14.0
176
	 */
177 View Code Duplication
	public function clear_warning() {
0 ignored issues
show
Duplication introduced by
This method 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...
Coding Style introduced by
clear_warning uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
178
179
		if ( ! isset( $_REQUEST['post'] ) ) {
180
			wp_die( 'The `post` parameter is required.' );
181
		}
182
183
		$this->batch_analysis_service->clear_warning( (array) $_REQUEST['post'] );
184
185
		// Clear any buffer.
186
		ob_clean();
187
188
		// Send the response.
189
		wp_send_json_success();
190
191
	}
192
193
}
194