ConnectionTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B test() 0 34 5
B check_admin_page() 0 22 5
A check_airplane_mode() 0 14 3
A check_internet() 0 15 2
A check_external_url() 0 12 2
1
<?php
2
namespace DummyPress;
3
4
/**
5
 * ConnectionTest
6
 *
7
 * A more intelligent check to see if we can connect to Splashbase or not.
8
 *
9
 * This class checks whether or not we can connect to the Internet, and
10
 * if we can, whether we can connect to Splashbase itself. This is used by
11
 * our admin notice function to check whether or not we should display a notice
12
 * to users warning them of issues with Splashbase.
13
 *
14
 * The purpose of this is to avoid useless bug-hunting when images don't work.
15
 *
16
 * @package    WordPress
17
 * @subpackage Evans
18
 * @author     Mike Selander
19
 */
20
class ConnectionTest {
21
22
	/**
23
	 * Run all of our connection tests.
24
	 *
25
	 * @see check_admin_page, check_airplane_mode, check_internet, check_splashbase
26
	 *
27
	 * @return boolean Status of connection to Internet/Splashbase.
0 ignored issues
show
Documentation introduced by
Should the return type not be null|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
28
	 */
29
	public function test() {
30
31
		/*
32
		 * Make sure that we're looking at the correct admin page
33
		 */
34
		if ( ! $this->check_admin_page() ) {
35
			return;
36
		}
37
38
		/*
39
		 * Test #1 - Check for Airplane Mode plugin status
40
		 */
41
		if ( ! $this->check_airplane_mode() ) {
42
			return false;
43
		}
44
45
		/*
46
		 * Test #2 - Check Internet connection in general
47
		 */
48
		if ( ! $this->check_internet() ) {
49
			return false;
50
		}
51
52
		/*
53
		 * Test #3 - Check External URL itself (Splashbase here)
54
		 */
55
		if ( ! $this->check_external_url( 'http://www.splashbase.co/api/v1/images/' ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->check_exte...se.co/api/v1/images/');.
Loading history...
56
			return false;
57
		}
58
59
		// We've made it this far, looks like everything checks out OK!
60
		return true;
61
62
	}
63
64
65
	/**
66
	 * Check to make sure that we're only running this check in the correct place.
67
	 *
68
	 * We only want these (relatively)expensive checks to run on a single admin
69
	 * page, so we need to run some checks first and verify that we're on the
70
	 * right screen.
71
	 *
72
	 * @access private
73
	 *
74
	 * @global object $current_screen Current admin screen info.
75
	 *
76
	 * @return boolean Whether or not we're in the right place.
77
	 */
78
	private function check_admin_page() {
0 ignored issues
show
Coding Style introduced by
check_admin_page uses the super-global variable $_SERVER 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...
79
		global $current_screen;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
80
81
		// Only run if we're in the admin page
82
		if ( ! is_admin() ) {
83
			return false;
84
		}
85
86
		// Get the current admin screen & verify that we're on the right one
87
		// before continuing.
88
		if ( isset ( $current_screen ) && 'tools_page_create-test-data' != $current_screen->base ) {
89
			return false;
90
		}
91
92
		$last_uri_bit = explode( '=', $_SERVER['REQUEST_URI'] );
93
		if ( 'create-test-data' != end( $last_uri_bit ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !('create-test-da...!= end($last_uri_bit));.
Loading history...
94
			return false;
95
		}
96
97
		return true;
98
99
	}
100
101
102
	/**
103
	 * Check if we have Airplane Mode and if it's turned on or not.
104
	 *
105
	 * @access private
106
	 *
107
	 * @see get_site_option
108
	 *
109
	 * @return boolean Connected or not.
110
	 */
111
	private function check_airplane_mode() {
112
113
		if ( class_exists( 'Airplane_Mode_Core' ) ) {
114
			// Is airplane mode active?
115
			$airplane_mode = get_site_option( 'airplane-mode' );
116
117
			if ( $airplane_mode === 'on' ) {
118
				return false;
119
			}
120
		}
121
122
		return true;
123
124
	}
125
126
127
	/**
128
	 * Attempt to open a socket to a popular, compact to check overall connectivity.
129
	 *
130
	 * @access private
131
	 *
132
	 * @see fsockopen, fsockclose
133
	 *
134
	 * @return boolean Connected or not.
135
	 */
136
	private function check_internet() {
137
138
		// Attempt to open a socket connection to Google
139
		$connected = @fsockopen( "www.google.com", 80 );
140
141
		if ( ! $connected ) {
142
			return false;
143
		}
144
145
		// Close out our 1st test
146
		fclose( $connected );
147
148
		return true;
149
150
	}
151
152
153
	/**
154
	 * Check an external API to see if it's reachable or not.
155
	 *
156
	 * @access private
157
	 *
158
	 * @see wp_remote_get
159
	 *
160
	 * @param string $url External URL to attempt to reach.
161
	 * @return boolean Connected or not.
162
	 */
163
	private function check_external_url( $url ) {
164
165
		$test_url = esc_url( $url );
166
		$response = wp_remote_get( $test_url );
167
168
		if ( ! is_array( $response ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return is_array($response);.
Loading history...
169
			return false;
170
		}
171
172
		return true;
173
174
	}
175
176
}
177