Ajax_Controller::invoke_callback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * The controller which is used for handling responses/ajax callbacks.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Ajax
23
 */
24
25
namespace PinkCrab\Ajax\Dispatcher;
26
27
use Closure;
28
use Exception;
29
use PinkCrab\Ajax\Ajax;
30
use PinkCrab\HTTP\HTTP;
31
use PinkCrab\Ajax\Ajax_Hooks;
32
use Psr\Http\Message\ResponseInterface;
33
use Psr\Http\Message\ServerRequestInterface;
34
35
class Ajax_Controller {
36
37
	protected ServerRequestInterface $server_request;
38
	protected Response_Factory $response_factory;
39
	protected HTTP $http_helper;
40
	protected Ajax_Request_Validator $request_validator;
41
42
	public function __construct(
43
		ServerRequestInterface $server_request,
44
		Response_Factory $response_factory,
45
		HTTP $http_helper,
46
		Ajax_Request_Validator $request_validator
47
	) {
48
		$this->response_factory  = $response_factory;
49
		$this->server_request    = $server_request;
50
		$this->http_helper       = $http_helper;
51
		$this->request_validator = $request_validator;
52
	}
53
54
	/**
55
	 * Validates an ajax call based on the server requests contents.
56
	 *
57
	 * @param \PinkCrab\Ajax\Ajax $ajax_class
58
	 * @return bool
59
	 */
60
	public function validate_request( Ajax $ajax_class ): bool {
61
		return $this->request_validator->validate( $ajax_class );
62
	}
63
64
	/**
65
	 * Used to invoke the callback supplied in an Ajax instance.
66
	 *
67
	 * @param \PinkCrab\Ajax\Ajax $ajax_class
68
	 * @return \Psr\Http\Message\ResponseInterface
69
	 * @filter Ajax_Hooks::CALLBACK_REQUEST_FILTER
70
	 */
71
	public function invoke_callback( Ajax $ajax_class ): ResponseInterface {
72
		return $ajax_class->callback(
73
			\apply_filters( Ajax_Hooks::CALLBACK_REQUEST_FILTER, $this->server_request, $ajax_class ),
74
			$this->response_factory
75
		);
76
	}
77
78
	/**
79
	 * Returns the Closure for a ajax request.
80
	 *
81
	 * @param \PinkCrab\Ajax\Ajax $ajax_class
82
	 * @return \Closure():noreturn
83
	 * @filter Ajax_Hooks::REQUEST_NONCE_VERIFICATION
84
	 * @action Ajax_Hooks::CALLBACK_EXECUTION_EXCEPTION
85
	 * @filter Ajax_Hooks::CALLBACK_RESPONSE_FILTER
86
	 */
87
	public function create_callback( Ajax $ajax_class ): Closure {
88
		/**
89
		 * @param \PinkCrab\Ajax\Ajax $ajax_class
90
		 * @return noreturn
91
		 */
92
		return function () use ( $ajax_class ): void {
93
94
			$valid_nonce = apply_filters(
95
				Ajax_Hooks::REQUEST_NONCE_VERIFICATION,
96
				$this->validate_request( $ajax_class ),
97
				$ajax_class,
98
				$this->server_request
99
			);
100
101
			try {
102
				$response = $valid_nonce
103
				? $this->invoke_callback( $ajax_class )
104
				: $this->response_factory->unauthorised();
105
			} catch ( Exception $th ) {
106
107
				do_action( Ajax_Hooks::CALLBACK_EXECUTION_EXCEPTION, $th, $ajax_class );
108
109
				$response = $this->response_factory->failure( array( 'error' => $th->getMessage() ) );
110
			}
111
			$this->http_helper->emit_psr7_response(
112
				\apply_filters( Ajax_Hooks::CALLBACK_RESPONSE_FILTER, $response, $ajax_class, $this->server_request )
113
			);
114
115
			\wp_die();
116
		};
117
	}
118
}
119