GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 4c3b7e...cb6ff1 )
by Leonardo
03:37
created

FunctionalityModuleResolver::getDescription()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 22
c 2
b 0
f 0
nc 10
nop 1
dl 0
loc 28
ccs 0
cts 26
cp 0
crap 110
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLAPI\GraphQLAPI\ModuleResolvers;
6
7
use GraphQLAPI\GraphQLAPI\Plugin;
8
use GraphQLAPI\GraphQLAPI\ModuleSettings\Properties;
9
use GraphQLAPI\GraphQLAPI\Security\UserAuthorization;
10
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade;
11
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ModuleResolverTrait;
12
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLSchemaConfigurationPostType;
13
use PoP\GraphQLEndpointForWP\ComponentConfiguration as GraphQLEndpointForWPComponentConfiguration;
14
15
class FunctionalityModuleResolver extends AbstractFunctionalityModuleResolver
16
{
17
    use ModuleResolverTrait;
18
19
    // public const MAIN = Plugin::NAMESPACE . '\main';
20
    public const SCHEMA_EDITING_ACCESS = Plugin::NAMESPACE . '\schema-editing-access';
21
    public const SINGLE_ENDPOINT = Plugin::NAMESPACE . '\single-endpoint';
22
    public const PERSISTED_QUERIES = Plugin::NAMESPACE . '\persisted-queries';
23
    public const CUSTOM_ENDPOINTS = Plugin::NAMESPACE . '\custom-endpoints';
24
    public const SCHEMA_CONFIGURATION = Plugin::NAMESPACE . '\schema-configuration';
25
    public const SCHEMA_NAMESPACING = Plugin::NAMESPACE . '\schema-namespacing';
26
    public const CACHE_CONTROL = Plugin::NAMESPACE . '\cache-control';
27
    public const FIELD_DEPRECATION = Plugin::NAMESPACE . '\field-deprecation';
28
    public const API_HIERARCHY = Plugin::NAMESPACE . '\api-hierarchy';
29
30
    /**
31
     * Setting options
32
     */
33
    public const OPTION_EDITING_ACCESS_SCHEME = 'editing-access-scheme';
34
    public const OPTION_PATH = 'path';
35
    public const OPTION_SCHEMA_CONFIGURATION_ID = 'schema-configuration-id';
36
    public const OPTION_USE_NAMESPACING = 'use-namespacing';
37
    public const OPTION_MAX_AGE = 'max-age';
38
39
    /**
40
     * Setting option values
41
     */
42
    public const OPTION_VALUE_NO_VALUE_ID = 0;
43
44
    public static function getModulesToResolve(): array
45
    {
46
        return [
47
            // self::MAIN,
48
            self::SINGLE_ENDPOINT,
49
            self::PERSISTED_QUERIES,
50
            self::CUSTOM_ENDPOINTS,
51
            self::SCHEMA_CONFIGURATION,
52
            self::SCHEMA_NAMESPACING,
53
            self::CACHE_CONTROL,
54
            self::FIELD_DEPRECATION,
55
            self::API_HIERARCHY,
56
            self::SCHEMA_EDITING_ACCESS,
57
        ];
58
    }
59
60
    public function getDependedModuleLists(string $module): array
61
    {
62
        switch ($module) {
63
            case self::PERSISTED_QUERIES:
64
            case self::SINGLE_ENDPOINT:
65
            case self::CUSTOM_ENDPOINTS:
66
                return [];
67
            case self::SCHEMA_CONFIGURATION:
68
            case self::API_HIERARCHY:
69
                return [
70
                    [
71
                        self::PERSISTED_QUERIES,
72
                        self::CUSTOM_ENDPOINTS,
73
                    ],
74
                ];
75
            case self::SCHEMA_NAMESPACING:
76
            case self::FIELD_DEPRECATION:
77
                return [
78
                    [
79
                        self::SCHEMA_CONFIGURATION,
80
                    ],
81
                ];
82
            case self::CACHE_CONTROL:
83
                return [
84
                    [
85
                        self::SCHEMA_CONFIGURATION,
86
                    ],
87
                    [
88
                        self::PERSISTED_QUERIES,
89
                    ],
90
                ];
91
        }
92
        return parent::getDependedModuleLists($module);
93
    }
94
95
    // public function canBeDisabled(string $module): bool
96
    // {
97
    //     switch ($module) {
98
    //         case self::SCHEMA_EDITING_ACCESS:
99
    //         // case self::MAIN:
100
    //             return false;
101
    //     }
102
    //     return parent::canBeDisabled($module);
103
    // }
104
105
    // public function isHidden(string $module): bool
106
    // {
107
    //     switch ($module) {
108
    //         case self::MAIN:
109
    //             return true;
110
    //     }
111
    //     return parent::isHidden($module);
112
    // }
113
114
    public function getName(string $module): string
115
    {
116
        $names = [
117
            // self::MAIN => \__('Main', 'graphql-api'),
118
            self::SCHEMA_EDITING_ACCESS => \__('Schema Editing Access', 'graphql-api'),
119
            self::SINGLE_ENDPOINT => \__('Single Endpoint', 'graphql-api'),
120
            self::PERSISTED_QUERIES => \__('Persisted Queries', 'graphql-api'),
121
            self::CUSTOM_ENDPOINTS => \__('Custom Endpoints', 'graphql-api'),
122
            self::SCHEMA_CONFIGURATION => \__('Schema Configuration', 'graphql-api'),
123
            self::SCHEMA_NAMESPACING => \__('Schema Namespacing', 'graphql-api'),
124
            self::CACHE_CONTROL => \__('Cache Control', 'graphql-api'),
125
            self::FIELD_DEPRECATION => \__('Field Deprecation', 'graphql-api'),
126
            self::API_HIERARCHY => \__('API Hierarchy', 'graphql-api'),
127
        ];
128
        return $names[$module] ?? $module;
129
    }
130
131
    public function getDescription(string $module): string
132
    {
133
        switch ($module) {
134
            // case self::MAIN:
135
            //     return \__('Artificial module for defining the main settings', 'graphql-api');
136
            case self::SCHEMA_EDITING_ACCESS:
137
                return \__('Grant access to users other than admins to edit the GraphQL schema', 'graphql-api');
138
            case self::SINGLE_ENDPOINT:
139
                return \sprintf(
140
                    \__('Expose a single GraphQL endpoint under <code>%s</code>, with unrestricted access', 'graphql-api'),
141
                    GraphQLEndpointForWPComponentConfiguration::getGraphQLAPIEndpoint()
142
                );
143
            case self::PERSISTED_QUERIES:
144
                return \__('Expose predefined responses through a custom URL, akin to using GraphQL queries to publish REST endpoints', 'graphql-api');
145
            case self::CUSTOM_ENDPOINTS:
146
                return \__('Expose different subsets of the schema for different targets, such as users (clients, employees, etc), applications (website, mobile app, etc), context (weekday, weekend, etc), and others', 'graphql-api');
147
            case self::SCHEMA_CONFIGURATION:
148
                return \__('Customize the schema accessible to different Custom Endpoints and Persisted Queries, by applying a custom configuration (involving namespacing, access control, cache control, and others) to the grand schema', 'graphql-api');
149
            case self::SCHEMA_NAMESPACING:
150
                return \__('Automatically namespace types and interfaces with a vendor/project name, to avoid naming collisions', 'graphql-api');
151
            case self::CACHE_CONTROL:
152
                return \__('Provide HTTP Caching for Persisted Queries, sending the Cache-Control header with a max-age value calculated from all fields in the query', 'graphql-api');
153
            case self::FIELD_DEPRECATION:
154
                return \__('Deprecate fields, and explain how to replace them, through a user interface', 'graphql-api');
155
            case self::API_HIERARCHY:
156
                return \__('Create a hierarchy of API endpoints extending from other endpoints, and inheriting their properties', 'graphql-api');
157
        }
158
        return parent::getDescription($module);
159
    }
160
161
    public function isEnabledByDefault(string $module): bool
162
    {
163
        switch ($module) {
164
            case self::SINGLE_ENDPOINT:
165
            case self::SCHEMA_NAMESPACING:
166
            case self::FIELD_DEPRECATION:
167
                return false;
168
        }
169
        return parent::isEnabledByDefault($module);
170
    }
171
172
    /**
173
     * Default value for an option set by the module
174
     *
175
     * @param string $module
176
     * @param string $option
177
     * @return mixed Anything the setting might be: an array|string|bool|int|null
178
     */
179
    public function getSettingsDefaultValue(string $module, string $option)
180
    {
181
        $defaultValues = [
182
            self::SCHEMA_EDITING_ACCESS => [
183
                self::OPTION_EDITING_ACCESS_SCHEME => UserAuthorization::ACCESS_SCHEME_ADMIN_ONLY,
184
            ],
185
            self::SINGLE_ENDPOINT => [
186
                self::OPTION_PATH => '/graphql/',
187
            ],
188
            self::CUSTOM_ENDPOINTS => [
189
                self::OPTION_PATH => 'graphql',
190
            ],
191
            self::PERSISTED_QUERIES => [
192
                self::OPTION_PATH => 'graphql-query',
193
            ],
194
            self::SCHEMA_CONFIGURATION => [
195
                self::OPTION_SCHEMA_CONFIGURATION_ID => self::OPTION_VALUE_NO_VALUE_ID,
196
            ],
197
            self::SCHEMA_NAMESPACING => [
198
                self::OPTION_USE_NAMESPACING => false,
199
            ],
200
            self::CACHE_CONTROL => [
201
                self::OPTION_MAX_AGE => 86400, // 1 day
202
            ],
203
        ];
204
        return $defaultValues[$module][$option];
205
    }
206
207
    /**
208
     * Array with the inputs to show as settings for the module
209
     *
210
     * @param string $module
211
     * @return array
212
     */
213
    public function getSettings(string $module): array
214
    {
215
        $moduleSettings = parent::getSettings($module);
216
        $moduleRegistry = ModuleRegistryFacade::getInstance();
217
        // Do the if one by one, so that the SELECT do not get evaluated unless needed
218
        if ($module == self::SCHEMA_EDITING_ACCESS) {
219
            /**
220
             * Write Access Scheme
221
             * If `"admin"`, only the admin can compose a GraphQL query and endpoint
222
             * If `"post"`, the workflow from creating posts is employed (i.e. Author role can create
223
             * but not publish the query, Editor role can publish it, etc)
224
             */
225
            $option = self::OPTION_EDITING_ACCESS_SCHEME;
226
            $moduleSettings[] = [
227
                Properties::INPUT => $option,
228
                Properties::NAME => $this->getSettingOptionName(
229
                    $module,
230
                    $option
231
                ),
232
                Properties::TITLE => \__('Editing Access Scheme', 'graphql-api'),
233
                Properties::DESCRIPTION => \__('Scheme to decide which users can edit the schema (Persisted Queries, Custom Endpoints and related post types) and with what permissions', 'graphql-api'),
234
                Properties::TYPE => Properties::TYPE_STRING,
235
                Properties::POSSIBLE_VALUES => [
236
                    UserAuthorization::ACCESS_SCHEME_ADMIN_ONLY => \__('Admin user(s) only', 'graphql-api'),
237
                    UserAuthorization::ACCESS_SCHEME_POST => \__('Use same access workflow as for editing posts', 'graphql-api'),
238
                ],
239
            ];
240
        } elseif ($module == self::SINGLE_ENDPOINT) {
241
            $option = self::OPTION_PATH;
242
            $moduleSettings[] = [
243
                Properties::INPUT => $option,
244
                Properties::NAME => $this->getSettingOptionName(
245
                    $module,
246
                    $option
247
                ),
248
                Properties::TITLE => \__('Endpoint path', 'graphql-api'),
249
                Properties::DESCRIPTION => \__('URL path to expose the single GraphQL endpoint', 'graphql-api'),
250
                Properties::TYPE => Properties::TYPE_STRING,
251
            ];
252
        } elseif ($module == self::CUSTOM_ENDPOINTS) {
253
            $option = self::OPTION_PATH;
254
            $moduleSettings[] = [
255
                Properties::INPUT => $option,
256
                Properties::NAME => $this->getSettingOptionName(
257
                    $module,
258
                    $option
259
                ),
260
                Properties::TITLE => \__('Base path', 'graphql-api'),
261
                Properties::DESCRIPTION => \__('URL base path to expose the Custom Endpoint', 'graphql-api'),
262
                Properties::TYPE => Properties::TYPE_STRING,
263
            ];
264
        } elseif ($module == self::PERSISTED_QUERIES) {
265
            $option = self::OPTION_PATH;
266
            $moduleSettings[] = [
267
                Properties::INPUT => $option,
268
                Properties::NAME => $this->getSettingOptionName(
269
                    $module,
270
                    $option
271
                ),
272
                Properties::TITLE => \__('Base path', 'graphql-api'),
273
                Properties::DESCRIPTION => \__('URL base path to expose the Persisted Query', 'graphql-api'),
274
                Properties::TYPE => Properties::TYPE_STRING,
275
            ];
276
        } elseif ($module == self::SCHEMA_CONFIGURATION) {
277
            $whereModules = [];
278
            $maybeWhereModules = [
279
                self::CUSTOM_ENDPOINTS,
280
                self::PERSISTED_QUERIES,
281
            ];
282
            foreach ($maybeWhereModules as $maybeWhereModule) {
283
                if ($moduleRegistry->isModuleEnabled($maybeWhereModule)) {
284
                    $whereModules[] = '▹ ' . $this->getName($maybeWhereModule);
285
                }
286
            }
287
            // Build all the possible values by fetching all the Schema Configuration posts
288
            $possibleValues = [
289
                self::OPTION_VALUE_NO_VALUE_ID => \__('None', 'graphql-api'),
290
            ];
291
            if ($customPosts = \get_posts([
292
                    'posts_per_page' => -1,
293
                    'post_type' => GraphQLSchemaConfigurationPostType::POST_TYPE,
294
                    'post_status' => 'publish',
295
                ])
296
            ) {
297
                foreach ($customPosts as $customPost) {
298
                    $possibleValues[$customPost->ID] = $customPost->post_title;
299
                }
300
            }
301
            $option = self::OPTION_SCHEMA_CONFIGURATION_ID;
302
            $moduleSettings[] = [
303
                Properties::INPUT => $option,
304
                Properties::NAME => $this->getSettingOptionName(
305
                    $module,
306
                    $option
307
                ),
308
                Properties::TITLE => \__('Default Schema Configuration', 'graphql-api'),
309
                Properties::DESCRIPTION => sprintf(
310
                    \__('Schema Configuration to use when option <code>"Default"</code> is selected (in %s)', 'graphql-api'),
311
                    implode(
312
                        \__(', ', 'graphql-api'),
313
                        $whereModules
314
                    )
315
                ),
316
                Properties::TYPE => Properties::TYPE_INT,
317
                // Fetch all Schema Configurations from the DB
318
                Properties::POSSIBLE_VALUES => $possibleValues,
319
            ];
320
        } elseif ($module == self::SCHEMA_NAMESPACING) {
321
            $option = self::OPTION_USE_NAMESPACING;
322
            $moduleSettings[] = [
323
                Properties::INPUT => $option,
324
                Properties::NAME => $this->getSettingOptionName(
325
                    $module,
326
                    $option
327
                ),
328
                Properties::TITLE => \__('Use namespacing?', 'graphql-api'),
329
                Properties::DESCRIPTION => \__('Automatically namespace types and interfaces in the schema', 'graphql-api'),
330
                Properties::TYPE => Properties::TYPE_BOOL,
331
            ];
332
        } elseif ($module == self::CACHE_CONTROL) {
333
            $option = self::OPTION_MAX_AGE;
334
            $moduleSettings[] = [
335
                Properties::INPUT => $option,
336
                Properties::NAME => $this->getSettingOptionName(
337
                    $module,
338
                    $option
339
                ),
340
                Properties::TITLE => \__('Default max-age', 'graphql-api'),
341
                Properties::DESCRIPTION => \__('Default max-age value (in seconds) for the Cache-Control header, for all fields and directives in the schema', 'graphql-api'),
342
                Properties::TYPE => Properties::TYPE_INT,
343
                Properties::MIN_NUMBER => 0,
344
            ];
345
        }
346
        return $moduleSettings;
347
    }
348
}
349