Completed
Push — master ( 48986a...95c69f )
by David
06:07
created

Wordlift_Publisher_Ajax_Adapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B publisher() 0 22 4
1
<?php
2
/**
3
 * Ajax Adapters: Publisher Ajax Adapter.
4
 *
5
 * An Ajax adapter to the {@link Wordlift_Publisher_Service}.
6
 *
7
 * @since   3.11.0
8
 * @package Wordlift
9
 */
10
11
/**
12
 * Define the {@link Wordlift_Publisher_Ajax_Adapter} instance.
13
 *
14
 * @since   3.11.0
15
 * @package Wordlift
16
 */
17
class Wordlift_Publisher_Ajax_Adapter {
18
19
	/**
20
	 * The {@link Wordlift_Publisher_Service} instance.
21
	 *
22
	 * @since  3.11.0
23
	 * @access private
24
	 * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
25
	 */
26
	private $publisher_service;
27
28
	/**
29
	 * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance.
30
	 *
31
	 * @since 3.11.0
32
	 *
33
	 * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
34
	 */
35
	function __construct( $publisher_service ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
37
		$this->publisher_service = $publisher_service;
38
39
	}
40
41
	/**
42
	 * The publisher AJAX action. This function is hook to the `wl_publisher`
43
	 * action.
44
	 *
45
	 * @since 3.11.0
46
	 */
47
	public function publisher() {
0 ignored issues
show
Coding Style introduced by
publisher uses the super-global variable $_POST 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...
48
49
		// Ensure we don't have garbage before us.
50
		ob_clean();
51
52
		// Check if the current user can `manage_options`.
53
		if ( ! current_user_can( 'manage_options' ) ) {
54
			wp_send_json_error( 'Access denied.' );
55
		}
56
57
		// No actual search parameter was passed, bail out.
58
		if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) {
59
			wp_send_json_error( 'The q parameter is required.' );
60
		}
61
62
		// Get the response.
63
		$response = $this->publisher_service->query( $_POST['q'] );
64
65
		// Finally output the response.
66
		wp_send_json_success( $response );
67
68
	}
69
70
}
71