Completed
Push — add/handling-connection-errors ( 287a33 )
by
unknown
32:57 queued 25:30
created

Error_Handler::delete_stored_errors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Jetpack Connection error class file.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
/**
11
 * The Jetpack Connection Errors that handles errors
12
 */
13
class Error_Handler {
14
15
	/**
16
	 * The name of the option that stores the errors
17
	 *
18
	 * @var string
19
	 */
20
	const STORED_ERRORS_OPTION = 'jetpack_xmlrpc_errors';
21
22
	/**
23
	 * The prefix of the transient that controls the gate for each error code
24
	 *
25
	 * @var string
26
	 */
27
	const ERROR_REPORTING_GATE = 'jetpack_connection_error_reporting_gate_';
28
29
	/**
30
	 * Keep track of a connection error that was encoutered
31
	 *
32
	 * @param \WP_Error $error the error object.
33
	 * @param boolean   $force Force the report, even if should_report_error is false.
34
	 * @return void
35
	 */
36
	public function report_error( \WP_Error $error, $force = false ) {
37
		if ( $this->should_report_error( $error ) || $force ) {
38
			$this->store_error( $error );
39
		}
40
	}
41
42
	/**
43
	 * Checks the status of the gate
44
	 *
45
	 * This protects the site (and WPCOM) against over loads.
46
	 *
47
	 * @param \WP_Error $error the error object.
48
	 * @return boolean $should_report True if gate is open and the error should be reported.
49
	 */
50
	public function should_report_error( \WP_Error $error ) {
51
52
		if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) {
53
			return true;
54
		}
55
56
		$transient = self::ERROR_REPORTING_GATE . $error->get_error_code();
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
		if ( get_transient( $transient ) ) {
59
			return false;
60
		}
61
62
		set_transient( $transient, true, HOUR_IN_SECONDS );
63
		return true;
64
	}
65
66
	/**
67
	 * Stores the error in the database so we know there is an issue and can inform the user
68
	 *
69
	 * @param \WP_Error $error the error object.
70
	 * @return boolean False if stored errors were not updated and true if stored errors were updated.
71
	 */
72
	public function store_error( \WP_Error $error ) {
73
		$stored_errors                             = $this->get_stored_errors();
74
		$stored_errors[ $error->get_error_code() ] = array(
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
			'error_code'    => $error->get_error_code(),
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
			'error_message' => $error->get_error_message(),
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
			'error_data'    => $error->get_error_data(),
0 ignored issues
show
Bug introduced by
The method get_error_data() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
			'timestamp'     => time(),
79
			'nonce'         => wp_generate_password( 10, false ),
80
		);
81
		return update_option( self::STORED_ERRORS_OPTION, $stored_errors );
82
	}
83
84
	/**
85
	 * Gets the reported errors stored in the database
86
	 *
87
	 * @return \WP_Error[] $errors
88
	 */
89
	public function get_stored_errors() {
90
		$stored_errors = get_option( self::STORED_ERRORS_OPTION );
91
		if ( ! is_array( $stored_errors ) ) {
92
			$stored_errors = array();
93
		}
94
		return array_map(
95
			function( $error ) {
96
				return new \WP_Error( $error['error_code'], $error['error_message'], $error['error_data'] );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $error['error_code'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97
			},
98
			$stored_errors
99
		);
100
	}
101
102
	/**
103
	 * Delete the reported errors stored in the database
104
	 *
105
	 * @return boolean True, if option is successfully deleted. False on failure.
106
	 */
107
	public function delete_stored_errors() {
108
		return delete_option( self::STORED_ERRORS_OPTION );
109
	}
110
111
}
112