Test Failed
Push — master ( 8a9607...6de795 )
by Paul
05:45
created

Application   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 44
c 2
b 0
f 0
dl 0
loc 129
ccs 2
cts 40
cp 0.05
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPermission() 0 11 4
A deactivate() 0 3 1
A storeDefaults() 0 8 3
A hasPermission() 0 4 2
A can() 0 3 1
A init() 0 8 2
A register() 0 13 3
A isAdmin() 0 3 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Defaults\PermissionDefaults;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Install;
11
12
/**
13
 * @property array $addons
14
 * @property string $capability
15
 * @property string $cron_event
16
 * @property array $defaults
17
 * @property string $export_key
18
 * @property string $file
19
 * @property string $id
20
 * @property string $languages
21
 * @property string $name
22
 * @property string $paged_handle
23
 * @property string $paged_query_var
24
 * @property string $post_type
25
 * @property string $prefix
26
 * @property array $session
27
 * @property \GeminiLabs\SiteReviews\Arguments $storage
28
 * @property string $taxonomy
29
 * @property string $version
30
 * @property string $testedTo;
31
 */
32
final class Application extends Container
33
{
34
    use Plugin;
35
    use Session;
36
    use Storage;
37
38
    const EXPORT_KEY = '_glsr_export';
39
    const ID = 'site-reviews';
40
    const PAGED_HANDLE = 'pagination_request';
41
    const PAGED_QUERY_VAR = 'reviews-page'; // filtered
42
    const POST_TYPE = 'site-review';
43
    const PREFIX = 'glsr_';
44
    const TAXONOMY = 'site-review-category';
45
46
    /**
47
     * @var array
48
     */
49
    protected $addons = [];
50
51
    /**
52
     * @var array
53
     */
54
    protected $defaults;
55
56
    /**
57
     * @var string
58
     */
59
    protected $name;
60
61
    /**
62
     * @param string $capability
63
     * @return bool
64
     */
65
    public function can($capability)
66
    {
67
        return $this->make(Role::class)->can($capability);
68
    }
69
70
    /**
71
     * @param bool $networkDeactivating
72
     * @return void
73
     * @callback register_deactivation_hook
74
     */
75
    public function deactivate($networkDeactivating)
76
    {
77
        $this->make(Install::class)->deactivate($networkDeactivating);
78
    }
79
80
    /**
81
     * @param string $page
82
     * @param string $tab
83
     * @return string
84
     */
85
    public function getPermission($page = '', $tab = 'index')
86
    {
87
        $fallback = 'edit_posts';
88
        $permissions = $this->make(PermissionDefaults::class)->defaults();
89
        $permission = Arr::get($permissions, $page, $fallback);
90
        if (is_array($permission)) {
91
            $permission = Arr::get($permission, $tab, $fallback);
92
        }
93
        return empty($permission) || !is_string($permission)
94
            ? $fallback
95
            : $permission;
96
    }
97
98
    /**
99
     * @param string $page
100
     * @param string $tab
101
     * @return bool
102
     */
103
    public function hasPermission($page = '', $tab = 'index')
104
    {
105
        $isAdmin = $this->isAdmin();
106
        return !$isAdmin || $this->can($this->getPermission($page, $tab));
107
    }
108
109
    /**
110
     * @return void
111
     */
112
    public function init()
113
    {
114
        // Ensure the custom database tables exist, this is needed in cases
115
        // where the plugin has been updated instead of activated.
116
        if (empty(get_option(static::PREFIX.'db_version'))) {
117
            $this->make(Install::class)->run();
118
        }
119
        $this->make(Hooks::class)->run();
120
    }
121
122
    /**
123
     * @return bool
124
     */
125 15
    public function isAdmin()
126
    {
127 15
        return (is_admin() || is_network_admin()) && !wp_doing_ajax();
128
    }
129
130
    /**
131
     * @param object $addon
132
     * @return void
133
     */
134
    public function register($addon)
135
    {
136
        try {
137
            $reflection = new \ReflectionClass($addon);
138
            if ($id = $reflection->getConstant('ID')) {
139
                $this->addons[] = $id;
140
                $this->bind($id, function () use ($addon) {
141
                    return $addon;
142
                });
143
                $addon->init();
144
            }
145
        } catch (\ReflectionException $e) {
146
            glsr_log()->error('Attempted to register an invalid addon.');
147
        }
148
    }
149
150
    /**
151
     * @return void
152
     */
153
    public function storeDefaults()
154
    {
155
        if (empty($this->defaults)) {
156
            $defaults = $this->make(DefaultsManager::class)->get();
157
            $this->defaults = $this->filterArray('get/defaults', $defaults);
158
        }
159
        if (empty(get_option(OptionManager::databaseKey()))) {
160
            update_option(OptionManager::databaseKey(), $this->defaults);
161
        }
162
    }
163
}
164