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

Controller   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 24%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
dl 0
loc 61
ccs 6
cts 25
cp 0.24
rs 10
c 1
b 0
f 1
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostId() 0 3 1
A execute() 0 12 3
A download() 0 10 2
A isReviewPostId() 0 3 1
A isReviewAdminPage() 0 4 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