Test Failed
Push — develop ( 4ba53a...c20cce )
by Paul
10:12
created

ShortcodeOptionManager   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
eloc 102
c 1
b 0
f 0
dl 0
loc 183
rs 9.84

11 Methods

Rating   Name   Duplication   Size   Complexity  
A author() 0 18 4
B __call() 0 30 7
A pagination() 0 6 1
A terms() 0 5 1
A assignedPosts() 0 22 4
A postId() 0 18 3
A assignedUsers() 0 21 4
A type() 0 4 2
A assignedTerms() 0 15 3
A schema() 0 5 1
A hide() 0 7 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Database;
8
use GeminiLabs\SiteReviews\Defaults\ShortcodeApiFetchDefaults;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Helpers\Arr;
11
use GeminiLabs\SiteReviews\Helpers\Str;
12
13
class ShortcodeOptionManager
14
{
15
    /**
16
     * The parameter passed to the called method can be either a shortcode tag,
17
     * an instantiated shortcode class, or an array with key/values found in
18
     * ShortcodeApiFetchDefaults::class
19
     * 
20
     * @return array
21
     */
22
    public function __call(string $name, array $arguments)
23
    {
24
        $name = Str::snakeCase($name);
25
        $shortcode = array_shift($arguments);
26
        if (is_string($shortcode)) {
27
            $shortcode = glsr()->shortcode($shortcode);
28
        }
29
        if ($shortcode instanceof ShortcodeContract) {
30
            $args = [
31
                'option' => $name,
32
                'shortcode' => $shortcode->tag,
0 ignored issues
show
Bug introduced by
Accessing tag on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
            ];
34
        } else {
35
            $args = Arr::consolidate($shortcode);
36
        }
37
        $args = glsr()->args(glsr(ShortcodeApiFetchDefaults::class)->merge($args));
38
        try {
39
            $method = Helper::buildMethodName($name);
40
            $reflection = new \ReflectionMethod($this, $method);
41
            $results = $reflection->isProtected()
42
                ? call_user_func([$this, $method], $args)
43
                : [];
44
        } catch (\ReflectionException $e) {
45
            $results = [];
46
        }
47
        $results = glsr()->filterArray("shortcode/options/{$name}", $results, $args);
48
        if (!empty($results) && !empty($args->placeholder)) {
49
            $results = Arr::prepend($results, esc_attr($args->placeholder), '');
50
        }
51
        return $results;
52
    }
53
54
    protected function assignedPosts(Arguments $args): array
55
    {
56
        $results = [];
57
        if (!empty($args->search)
58
            && !in_array($args->search, ['post_id', 'parent_id'])) {
59
            $results += glsr(Database::class)->posts([
60
                // @see MainController::parseAssignedPostTypesInQuery
61
                'post_type' => glsr()->prefix.'assigned_posts',
62
                'posts_per_page' => 50,
63
                's' => $args->search,
64
            ]);
65
        }
66
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
67
        if (!empty($include)) {
68
            $results += glsr(Database::class)->posts([
69
                'post__in' => $include,
70
            ]);
71
        }
72
        return [
73
            'post_id' => esc_html_x('The Current Page', 'admin-text', 'site-reviews'),
74
            'parent_id' => esc_html_x('The Parent Page', 'admin-text', 'site-reviews'),
75
        ] + $results;
76
    }
77
78
    protected function assignedTerms(Arguments $args): array
79
    {
80
        $results = [];
81
        if (!empty($args->search)) {
82
            $results += glsr(Database::class)->terms([
83
                'number' => 50,
84
                'search' => $args->search,
85
            ]);
86
        }
87
        if (!empty($args->include)) {
88
            $results += glsr(Database::class)->terms([
89
                'term_taxonomy_id' => $args->include,
90
            ]);
91
        }
92
        return $results;
93
    }
94
95
    protected function assignedUsers(Arguments $args): array
96
    {
97
        $results = [];
98
        if (!empty($args->search)
99
            && !in_array($args->search, ['author_id', 'profile_id', 'user_id'])) {
100
            $results += glsr(Database::class)->users([
101
                'number' => 50,
102
                'search_wild' => $args->search,
103
            ]);
104
        }
105
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
106
        if (!empty($include)) {
107
            $results += glsr(Database::class)->users([
108
                'include' => $include,
109
            ]);
110
        }
111
        return [
112
            'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews'),
113
            'author_id' => esc_html_x('The Page Author', 'admin-text', 'site-reviews'),
114
            'profile_id' => esc_html_x('The Profile User', 'admin-text', 'site-reviews'),
115
        ] + $results;
116
    }
117
118
    protected function author(Arguments $args): array
119
    {
120
        $results = [];
121
        if (!empty($args->search) && !in_array($args->search, ['user_id'])) {
122
            $results += glsr(Database::class)->users([
123
                'number' => 50,
124
                'search_wild' => $args->search,
125
            ]);
126
        }
127
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
128
        if (!empty($include)) {
129
            $results += glsr(Database::class)->users([
130
                'include' => $include,
131
            ]);
132
        }
133
        return [
134
            'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews'),
135
        ] + $results;
136
    }
137
138
    protected function hide(Arguments $args): array
139
    {
140
        if ($shortcode = glsr()->shortcode($args->shortcode)) {
141
            $fn = fn () => $this->hideOptions(); // @phpstan-ignore-line
0 ignored issues
show
Bug introduced by
The method hideOptions() does not exist on GeminiLabs\SiteReviews\D...\ShortcodeOptionManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
            $fn = fn () => $this->/** @scrutinizer ignore-call */ hideOptions(); // @phpstan-ignore-line
Loading history...
142
            return $fn->bindTo($shortcode, $shortcode)();
143
        }
144
        return [];
145
    }
146
147
    protected function pagination(): array
148
    {
149
        return [ // order is intentional
150
            'loadmore' => _x('Load More Button', 'admin-text', 'site-reviews'),
151
            'ajax' => _x('Pagination Links (AJAX)', 'admin-text', 'site-reviews'),
152
            'true' => _x('Pagination Links', 'admin-text', 'site-reviews'),
153
        ];
154
    }
155
156
    protected function postId(Arguments $args): array
157
    {
158
        $results = [];
159
        if (!empty($args->search)) {
160
            $results += glsr(Database::class)->posts([
161
                'post_type' => glsr()->post_type,
162
                'posts_per_page' => 50,
163
                's' => $args->search,
164
            ]);
165
        }
166
        $include = array_filter($args->include, fn ($id) => !array_key_exists($id, $results));
167
        if (!empty($include)) {
168
            $results += glsr(Database::class)->posts([
169
                'post_type' => glsr()->post_type,
170
                'post__in' => $include,
171
            ]);
172
        }
173
        return $results;
174
    }
175
176
    protected function schema(): array
177
    {
178
        return [
179
            'true' => _x('Enable rich snippets', 'admin-text', 'site-reviews'),
180
            'false' => _x('Disable rich snippets', 'admin-text', 'site-reviews'),
181
        ];
182
    }
183
184
    protected function terms(): array
185
    {
186
        return [
187
            'true' => _x('Terms were accepted', 'admin-text', 'site-reviews'),
188
            'false' => _x('Terms were not accepted', 'admin-text', 'site-reviews'),
189
        ];
190
    }
191
192
    protected function type(): array
193
    {
194
        $types = glsr()->retrieveAs('array', 'review_types', []);
195
        return 1 < count($types) ? $types : [];
196
    }
197
}
198