Passed
Push — master ( 81c350...2e0505 )
by Paul
04:39
created

Router::normalizeRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\AdminController;
7
use GeminiLabs\SiteReviews\Controllers\AjaxController;
8
use GeminiLabs\SiteReviews\Controllers\PublicController;
9
use GeminiLabs\SiteReviews\Helper;
10
11
class Router
12
{
13
	/**
14
	 * @return void
15
	 */
16 1
	public function routeAdminPostRequest()
17
	{
18 1
		$request = $this->getRequest();
19 1
		if( !$this->isValidPostRequest( $request ))return;
20
		$this->checkAdminNonce( $request['action'] );
21
		$this->routeRequest( 'admin', $request['action'], $request );
22
	}
23
24
	/**
25
	 * All ajax requests in the plugin are triggered by a single action hook (i.e. "glsr_action")
26
	 * Each route is determined by the request["action"]
27
	 * @return void
28
	 */
29 1
	public function routeAjaxRequest()
30
	{
31 1
		$request = $this->getRequest();
32 1
		if( !isset( $request['action'] )) {
33
			glsr_log()->error( 'The AJAX request must include an action' )->info( $request );
34
			wp_die();
35
		}
36 1
		if( !isset( $request['nonce'] )) {
37
			glsr_log()->error( 'The AJAX request must include a nonce' )->info( $request );
38
			wp_die();
39
		}
40 1
		if( !wp_verify_nonce( $request['nonce'], $request['action'] )) {
41
			glsr_log()->error( 'Nonce check failed for ajax request' )->info( $request );
42
			wp_die( -1, 403 );
43
		}
44 1
		$request['ajax_request'] = true;
45 1
		$this->routeRequest( 'ajax', $request['action'], $request );
46
		wp_die();
47
	}
48
49
	/**
50
	 * @return void
51
	 */
52
	public function routePublicPostRequest()
53
	{
54
		if( is_admin() )return;
55
		$request = $this->getRequest();
56
		if( !$this->isValidPostRequest( $request ))return;
57
		if( !wp_verify_nonce( $request['_wpnonce'], $request['action'] )) {
58
			glsr_log()->error( 'Nonce check failed for public request' )->info( $request );
59
			return;
60
		}
61
		$this->routeRequest( 'public', $request['action'], $request );
62
	}
63
64
	/**
65
	 * @return void
66
	 */
67 1
	public function routeWebhookRequest()
68
	{
69 1
		$request = filter_input( INPUT_GET, Application::PREFIX.'hook' );
70 1
		if( !$request )return;
71
		// @todo manage webhook here
72
	}
73
74
	/**
75
	 * @param string $action
76
	 * @return void
77
	 * @todo verify the $action-options
78
	 */
79
	protected function checkAdminNonce( $action )
80
	{
81
		$nonce = glsr( Helper::class )->filterInput( 'option_page' ) == $action
82
			&& glsr( Helper::class )->filterInput( 'action' ) == 'update'
83
			? $action.'-options'
84
			: $action;
85
		check_admin_referer( $nonce );
86
	}
87
88
	/**
89
	 * @return array
90
	 */
91 1
	protected function getRequest()
92
	{
93 1
		foreach( ['request', Application::ID] as $key ) {
94 1
			$request = glsr( Helper::class )->filterInputArray( $key );
95 1
			if( !empty( $request ))break;
96
		}
97 1
		return $this->normalizeRequest( $request );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $request seems to be defined by a foreach iteration on line 93. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
98
	}
99
100
	/**
101
	 * @return bool
102
	 */
103 1
	protected function isValidPostRequest( array $request = [] )
104
	{
105 1
		return !empty( $request['action'] ) && empty( glsr( Helper::class )->filterInput( 'ajax_request' ));
106
	}
107
108
	/**
109
	 * @return array
110
	 */
111 1
	protected function normalizeRequest( array $request )
112
	{
113 1
		if( isset( $request[Application::ID]['action'] )) {
114
			$request = $request[Application::ID];
115
		}
116 1
		if( glsr( Helper::class )->filterInput( 'action', $request ) == 'submit-review' ) {
117 1
			$request['recaptcha-token'] = glsr( Helper::class )->filterInput( 'g-recaptcha-response' );
118
		}
119 1
		return $request;
120
	}
121
122
	/**
123
	 * @param string $type
124
	 * @param string $action
125
	 * @return void
126
	 */
127 1
	protected function routeRequest( $type, $action, array $request = [] )
128
	{
129 1
		$actionHook = 'site-reviews/route/'.$type.'/request';
130 1
		$controller = glsr( glsr( Helper::class )->buildClassName( $type.'-controller', 'Controllers' ));
131 1
		$method = glsr( Helper::class )->buildMethodName( $action, 'router' );
132 1
		$request = apply_filters( 'site-reviews/route/request', $request, $action, $type );
133 1
		do_action( $actionHook, $action, $request );
134 1
		if( is_callable( [$controller, $method] )) {
135 1
			call_user_func( [$controller, $method], $request );
136
			return;
137
		}
138
		if( did_action( $actionHook ) === 0 ) {
139
			glsr_log( 'Unknown '.$type.' router request: '.$action );
140
		}
141
	}
142
}
143