Test Failed
Push — master ( 2def23...999d23 )
by Paul
04:14
created

CreateReview::getReferer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Handlers;
4
5
use GeminiLabs\SiteReviews\Commands\CreateReview as Command;
6
use GeminiLabs\SiteReviews\Database\ReviewManager;
7
use GeminiLabs\SiteReviews\Modules\Notification;
8
use GeminiLabs\SiteReviews\Modules\Session;
9
10
class CreateReview
11
{
12
	/**
13
	 * @return void|string
14
	 */
15
	public function handle( Command $command )
16
	{
17
		$review = glsr( ReviewManager::class )->create( $command );
18
		if( !$review ) {
19
			glsr( Session::class )->set( $command->form_id.'errors', [] );
20
			glsr( Session::class )->set( $command->form_id.'message', __( 'Your review could not be submitted and the error has been logged. Please notify the site admin.', 'site-reviews' ));
21
			return;
22
		}
23
		glsr( Session::class )->set( $command->form_id.'message', __( 'Your review has been submitted!', 'site-reviews' ));
24
		glsr( Notification::class )->send( $review );
25
		do_action( 'site-reviews/local/review/submitted', null, $command ); // @compat
26
		do_action( 'site-reviews/review/submitted', $review );
27
		if( $command->ajax_request )return;
28
		wp_safe_redirect( $this->getReferer( $command ));
29
		exit;
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
30
	}
31
32
	/**
33
	 * @return string
34
	 */
35
	protected function getReferer( Command $command )
36
	{
37
		$referer = trim( get_post_meta( $command->post_id, 'redirect_to', true ));
0 ignored issues
show
Bug introduced by
It seems like get_post_meta($command->...d, 'redirect_to', true) can also be of type false; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
		$referer = trim( /** @scrutinizer ignore-type */ get_post_meta( $command->post_id, 'redirect_to', true ));
Loading history...
38
		if( empty( $referer )) {
39
			$referer = $command->referer;
40
		}
41
		if( empty( $referer )) {
42
			glsr_log()->warning( 'The form referer ($_SERVER[REQUEST_URI]) was empty.' )->info( $command );
43
			$referer = home_url();
44
		}
45
		return $referer;
46
	}
47
}
48