Completed
Push — develop ( 195cb3...f65bd6 )
by David
02:59
created

Wordlift_Batch_Analysis_Adapter::submit()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
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
	 * @var Wordlift_Batch_Analysis_Service
21
	 */
22
	private $batch_analysis_service;
23
24
25
	/**
26
	 * Wordlift_Batch_Analysis_Adapter constructor.
27
	 *
28
	 * @since 3.14.2
29
	 *
30
	 * @param \Wordlift_Batch_Analysis_Service $batch_analysis_service
31
	 */
32
	public function __construct( $batch_analysis_service ) {
33
34
		$this->batch_analysis_service = $batch_analysis_service;
35
36
	}
37
38 View Code Duplication
	public function submit_auto_selected_posts() {
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
submit_auto_selected_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...
39
40
		if ( ! isset( $_REQUEST['link'] ) ) {
41
			wp_die( 'The `link` parameter is required.' );
42
		}
43
44
		$count = $this->batch_analysis_service->submit_auto_selected_posts( $_REQUEST['link'] );
45
46
		// Clear any buffer.
47
		ob_clean();
48
49
		// Send the response.
50
		wp_send_json_success( array( 'count' => $count ) );
51
52
	}
53
54
	public function submit() {
0 ignored issues
show
Coding Style introduced by
submit 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...
55
56
		if ( ! isset( $_REQUEST['link'] ) || ! isset( $_REQUEST['post'] ) ) {
57
			wp_die( 'The `link` and `post` parameters are required.' );
58
		}
59
60
		$count = $this->batch_analysis_service->submit( (array) $_REQUEST['post'], $_REQUEST['link'] );
61
62
		// Clear any buffer.
63
		ob_clean();
64
65
		// Send the response.
66
		wp_send_json_success( array( 'count' => $count ) );
67
68
	}
69
70 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...
71
72
		if ( ! isset( $_REQUEST['post'] ) ) {
73
			wp_die( 'The `post` parameter is required.' );
74
		}
75
76
		$count = $this->batch_analysis_service->cancel( (array) $_REQUEST['post'] );
77
78
		// Clear any buffer.
79
		ob_clean();
80
81
		// Send the response.
82
		wp_send_json_success( array( 'count' => $count ) );
83
84
	}
85
86 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...
87
88
		if ( ! isset( $_REQUEST['post'] ) ) {
89
			wp_die( 'The `post` parameter is required.' );
90
		}
91
92
		$this->batch_analysis_service->clear_warning( (array) $_REQUEST['post'] );
93
94
		// Clear any buffer.
95
		ob_clean();
96
97
		// Send the response.
98
		wp_send_json_success();
99
100
	}
101
102
}
103