Passed
Push — master ( ccb079...7906b4 )
by Paul
04:39
created

Controller::getPostId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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 (!glsr()->can('edit_others_posts')) {
19
            return;
20
        }
21
        nocache_headers();
22
        header('Content-Type: text/plain');
23
        header('Content-Disposition: attachment; filename="'.$filename.'"');
24
        echo html_entity_decode($content);
25
        exit;
26
    }
27
28
    /**
29
     * @param object $command
30
     * @return mixed
31
     * @throws InvalidArgumentException
32
     */
33 1
    public function execute($command)
34
    {
35 1
        $handlerClass = str_replace('Commands', 'Handlers', get_class($command));
36 1
        if (!class_exists($handlerClass)) {
37
            throw new InvalidArgumentException('Handler '.$handlerClass.' not found.');
38
        }
39
        try {
40 1
            return glsr($handlerClass)->handle($command);
41
        } catch (Exception $e) {
42
            status_header(400);
43
            glsr(Notice::class)->addError(new WP_Error('site_reviews_error', $e->getMessage()));
44
            glsr_log()->error($e->getMessage());
45
        }
46
    }
47
48
    /**
49
     * @return int
50
     */
51
    protected function getPostId()
52
    {
53
        return intval(filter_input(INPUT_GET, 'post'));
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    protected function isReviewAdminPage()
60
    {
61
        return is_admin() 
62
            && in_array(glsr()->post_type, [get_post_type(), filter_input(INPUT_GET, 'post_type')]);
63
    }
64
65
    /**
66
     * @param int $postId
67
     * @return bool
68
     */
69 1
    protected function isReviewPostId($postId)
70
    {
71 1
        return Application::POST_TYPE == get_post_field('post_type', $postId);
72
    }
73
}
74