Test Failed
Push — hotfix/fix-counts ( 1fe4ce...872cd6 )
by Paul
03:14
created

Controller   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 51
ccs 6
cts 21
cp 0.2857
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostId() 0 3 1
A execute() 0 13 3
A download() 0 8 2
A isReviewPostId() 0 3 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use Exception;
6
use GeminiLabs\SiteReviews\Application;
7
use GeminiLabs\SiteReviews\Modules\Notice;
8
use InvalidArgumentException;
9
use WP_Error;
10
11
abstract class Controller
12
{
13
	/**
14
	 * @return void
15
	 */
16
	public function download( $filename, $content )
17
	{
18
		if( !current_user_can( glsr()->constant( 'CAPABILITY' )))return;
19
		nocache_headers();
20
		header( 'Content-Type: text/plain' );
21
		header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
22
		echo html_entity_decode( $content );
23
		exit;
24
	}
25
26
	/**
27
	 * @param object $command
28
	 * @return mixed
29
	 * @throws InvalidArgumentException
30
	 */
31 1
	public function execute( $command )
32
	{
33 1
		$handlerClass = str_replace( 'Commands', 'Handlers', get_class( $command ));
34 1
		if( !class_exists( $handlerClass )) {
35
			throw new InvalidArgumentException( 'Handler '.$handlerClass.' not found.' );
36
		}
37
		try {
38 1
			return glsr( $handlerClass )->handle( $command );
39
		}
40
		catch( Exception $e ) {
41
			status_header( 400 );
42
			glsr( Notice::class )->addError( new WP_Error( 'site_reviews_error', $e->getMessage() ));
43
			glsr_log()->error( $e->getMessage() );
44
		}
45
	}
46
47
	/**
48
	 * @return int
49
	 */
50
	protected function getPostId()
51
	{
52
		return intval( filter_input( INPUT_GET, 'post' ));
53
	}
54
55
	/**
56
	 * @param int $postId
57
	 * @return bool
58
	 */
59 1
	protected function isReviewPostId( $postId )
60
	{
61 1
		return get_post_field( 'post_type', $postId ) == Application::POST_TYPE;
62
	}
63
}
64