Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

Controller   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 40.63%

Importance

Changes 0
Metric Value
wmc 16
eloc 25
dl 0
loc 76
ccs 13
cts 32
cp 0.4063
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostId() 0 3 1
A isReviewAdminPage() 0 4 2
A execute() 0 4 2
A download() 0 8 2
A isReviewAdminScreen() 0 7 2
A hasQueryPermission() 0 7 4
A isReviewEditor() 0 6 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Contracts\CommandContract;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use WP_Query;
8
9
abstract class Controller
10
{
11
    /**
12
     * @return void
13
     */
14
    public function download($filename, $content)
15
    {
16
        if (glsr()->can('edit_others_posts')) {
17
            nocache_headers();
18
            header('Content-Type: text/plain');
19
            header('Content-Disposition: attachment; filename="'.$filename.'"');
20
            echo html_entity_decode($content);
21
            exit;
22
        }
23
    }
24
25
    /**
26
     * @return mixed
27
     */
28 15
    public function execute(CommandContract $command)
29
    {
30 15
        if (method_exists($command, 'handle')) {
31 15
            return $command->handle();
32
        }
33
    }
34
35
    /**
36
     * @return int
37
     */
38
    protected function getPostId()
39
    {
40
        return intval(filter_input(INPUT_GET, 'post'));
41
    }
42
43
    /**
44
     * @return bool
45
     */
46 17
    protected function hasQueryPermission(WP_Query $query)
47
    {
48 17
        global $pagenow;
49 17
        return glsr()->isAdmin()
50 17
            && $query->is_main_query()
51 17
            && glsr()->post_type === $query->get('post_type')
52 17
            && 'edit.php' === $pagenow;
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    protected function isReviewAdminPage()
59
    {
60
        return glsr()->isAdmin()
61
            && in_array(glsr()->post_type, [get_post_type(), filter_input(INPUT_GET, 'post_type')]);
62
    }
63
64
    /**
65
     * @return bool
66
     */
67 7
    protected function isReviewAdminScreen()
68
    {
69 7
        $screen = glsr_current_screen();
70
        $screenIds = [
71 7
            'dashboard',
72
        ];
73 7
        return Str::startsWith(glsr()->post_type, $screen->post_type) || in_array($screen->id, $screenIds);
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    protected function isReviewEditor()
80
    {
81
        $screen = glsr_current_screen();
82
        return ('post' == $screen->base)
83
            && glsr()->post_type == $screen->id
84
            && glsr()->post_type == $screen->post_type;
85
    }
86
}
87