Test Failed
Push — master ( 98f0ba...364259 )
by Paul
03:58
created

PublicController::routerCreateReview()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Commands\CreateReview;
7
use GeminiLabs\SiteReviews\Controllers\Controller;
8
use GeminiLabs\SiteReviews\Database\OptionManager;
9
use GeminiLabs\SiteReviews\Handlers\EnqueuePublicAssets;
10
use GeminiLabs\SiteReviews\Modules\Validator\ValidateReview;
11
12
class PublicController extends Controller
13
{
14
	/**
15
	 * @return void
16
	 * @action wp_enqueue_scripts
17
	 */
18
	public function enqueueAssets()
19
	{
20
		(new EnqueuePublicAssets)->handle();
21
	}
22
23
	/**
24
	 * @return string
25
	 * @filter script_loader_tag
26
	 */
27
	public function filterEnqueuedScripts( $tag, $handle )
28
	{
29
		return $handle == Application::ID.'/google-recaptcha'
30
			&& glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.integration' ) == 'custom'
31
			? str_replace( ' src=', ' async defer src=', $tag )
32
			: $tag;
33
	}
34
35
	/**
36
	 * Add a variable to query_vars for custom pagination
37
	 * @param array $vars
38
	 * @return array
39
	 * @filter query_vars
40
	 * @todo remove need for dirty hack
41
	 */
42
	public function filterQueryVars( $vars )
43
	{
44
		$vars[] = Application::PAGED_QUERY_VAR;
45
		// dirty hack to fix a form submission with a field that has "name" as name
46
		if( filter_input( INPUT_POST, 'action' ) == 'submit-review'
47
			&& !is_null( filter_input( INPUT_POST, 'gotcha' ))) {
48
			$index = array_search( 'name', $vars, true );
49
			if( false !== $index ) {
50
				unset( $vars[$index] );
51
			}
52
		}
53
		return $vars;
54
	}
55
56
	/**
57
	 * @return mixed
58
	 */
59
	public function routerCreateReview( array $request )
60
	{
61
		$validated = glsr( ValidateReview::class )->validate( $request );
62
		if( !empty( $validated->error )) {
63
			return $validated->request;
64
		}
65
		if( $validated->recaptchaIsUnset )return;
66
		return $this->execute( new CreateReview( $validated->request ));
67
	}
68
}
69