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 ( cf7f24...60ace9 )
by Leonardo
04:40
created

SchemaModuleResolver::getDependedModuleLists()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
c 3
b 0
f 0
nc 11
nop 1
dl 0
loc 40
ccs 0
cts 40
cp 0
crap 132
rs 7.3166

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 PoPSchema\Pages\TypeResolvers\PageTypeResolver;
9
use PoPSchema\Posts\TypeResolvers\PostTypeResolver;
10
use PoPSchema\Users\TypeResolvers\UserTypeResolver;
11
use PoPSchema\Media\TypeResolvers\MediaTypeResolver;
12
use PoPSchema\Comments\TypeResolvers\CommentTypeResolver;
13
use PoPSchema\PostTags\TypeResolvers\PostTagTypeResolver;
14
use GraphQLAPI\GraphQLAPI\ModuleSettings\Properties;
15
use PoPSchema\UserRolesWP\TypeResolvers\UserRoleTypeResolver;
16
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLEndpointPostType;
17
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ModuleResolverTrait;
18
use PoPSchema\CustomPosts\TypeResolvers\CustomPostUnionTypeResolver;
19
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLPersistedQueryPostType;
20
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLCacheControlListPostType;
21
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLAccessControlListPostType;
22
use GraphQLAPI\GraphQLAPI\ModuleResolvers\FunctionalityModuleResolver;
23
use GraphQLAPI\GraphQLAPI\ModuleResolvers\AbstractSchemaModuleResolver;
24
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLSchemaConfigurationPostType;
25
use PoPSchema\GenericCustomPosts\TypeResolvers\GenericCustomPostTypeResolver;
26
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLFieldDeprecationListPostType;
27
28
class SchemaModuleResolver extends AbstractSchemaModuleResolver
29
{
30
    use ModuleResolverTrait {
31
        ModuleResolverTrait::hasDocumentation as upstreamHasDocumentation;
32
    }
33
34
    public const SCHEMA_CUSTOMPOSTS = Plugin::NAMESPACE . '\schema-customposts';
35
    public const SCHEMA_GENERIC_CUSTOMPOSTS = Plugin::NAMESPACE . '\schema-generic-customposts';
36
    public const SCHEMA_POSTS = Plugin::NAMESPACE . '\schema-posts';
37
    public const SCHEMA_COMMENTS = Plugin::NAMESPACE . '\schema-comments';
38
    public const SCHEMA_USERS = Plugin::NAMESPACE . '\schema-users';
39
    public const SCHEMA_USER_ROLES = Plugin::NAMESPACE . '\schema-user-roles';
40
    public const SCHEMA_PAGES = Plugin::NAMESPACE . '\schema-pages';
41
    public const SCHEMA_MEDIA = Plugin::NAMESPACE . '\schema-media';
42
    public const SCHEMA_TAGS = Plugin::NAMESPACE . '\schema-tags';
43
    public const SCHEMA_POST_TAGS = Plugin::NAMESPACE . '\schema-post-tags';
44
45
    /**
46
     * Setting options
47
     */
48
    public const OPTION_LIST_DEFAULT_LIMIT = 'list-default-limit';
49
    public const OPTION_LIST_MAX_LIMIT = 'list-max-limit';
50
    public const OPTION_ADD_TYPE_TO_CUSTOMPOST_UNION_TYPE = 'add-type-to-custompost-union-type';
51
    public const OPTION_USE_SINGLE_TYPE_INSTEAD_OF_UNION_TYPE = 'use-single-type-instead-of-union-type';
52
    public const OPTION_CUSTOMPOST_TYPES = 'custompost-types';
53
54
    /**
55
     * Hooks
56
     */
57
    public const HOOK_GENERIC_CUSTOMPOST_TYPES = __CLASS__ . ':generic-custompost-types';
58
59
    public static function getModulesToResolve(): array
60
    {
61
        return [
62
            self::SCHEMA_CUSTOMPOSTS,
63
            self::SCHEMA_GENERIC_CUSTOMPOSTS,
64
            self::SCHEMA_POSTS,
65
            self::SCHEMA_PAGES,
66
            self::SCHEMA_USERS,
67
            self::SCHEMA_USER_ROLES,
68
            self::SCHEMA_COMMENTS,
69
            self::SCHEMA_TAGS,
70
            self::SCHEMA_POST_TAGS,
71
            self::SCHEMA_MEDIA,
72
        ];
73
    }
74
75
    public function getDependedModuleLists(string $module): array
76
    {
77
        switch ($module) {
78
            case self::SCHEMA_USERS:
79
            case self::SCHEMA_MEDIA:
80
            case self::SCHEMA_CUSTOMPOSTS:
81
                return [
82
                    [
83
                        FunctionalityModuleResolver::SINGLE_ENDPOINT,
84
                        FunctionalityModuleResolver::PERSISTED_QUERIES,
85
                        FunctionalityModuleResolver::CUSTOM_ENDPOINTS,
86
                    ],
87
                ];
88
            case self::SCHEMA_USER_ROLES:
89
                return [
90
                    [
91
                        self::SCHEMA_USERS,
92
                    ],
93
                ];
94
            case self::SCHEMA_GENERIC_CUSTOMPOSTS:
95
            case self::SCHEMA_POSTS:
96
            case self::SCHEMA_PAGES:
97
            case self::SCHEMA_COMMENTS:
98
            case self::SCHEMA_TAGS:
99
                return [
100
                    [
101
                        self::SCHEMA_CUSTOMPOSTS,
102
                    ],
103
                ];
104
            case self::SCHEMA_POST_TAGS:
105
                return [
106
                    [
107
                        self::SCHEMA_POSTS,
108
                    ],
109
                    [
110
                        self::SCHEMA_TAGS,
111
                    ],
112
                ];
113
        }
114
        return parent::getDependedModuleLists($module);
115
    }
116
117
    public function getName(string $module): string
118
    {
119
        $names = [
120
            self::SCHEMA_GENERIC_CUSTOMPOSTS => \__('Schema Generic Custom Posts', 'graphql-api'),
121
            self::SCHEMA_POSTS => \__('Schema Posts', 'graphql-api'),
122
            self::SCHEMA_COMMENTS => \__('Schema Comments', 'graphql-api'),
123
            self::SCHEMA_USERS => \__('Schema Users', 'graphql-api'),
124
            self::SCHEMA_USER_ROLES => \__('Schema User Roles', 'graphql-api'),
125
            self::SCHEMA_PAGES => \__('Schema Pages', 'graphql-api'),
126
            self::SCHEMA_MEDIA => \__('Schema Media', 'graphql-api'),
127
            self::SCHEMA_TAGS => \__('Schema Tags', 'graphql-api'),
128
            self::SCHEMA_POST_TAGS => \__('Schema Post Tags', 'graphql-api'),
129
            self::SCHEMA_CUSTOMPOSTS => \__('Schema Custom Posts', 'graphql-api'),
130
        ];
131
        return $names[$module] ?? $module;
132
    }
133
134
    public function getDescription(string $module): string
135
    {
136
        switch ($module) {
137
            case self::SCHEMA_GENERIC_CUSTOMPOSTS:
138
                return sprintf(
139
                    \__('Query any custom post type (added to the schema or not), through a generic type <code>%1$s</code>', 'graphql-api'),
140
                    GenericCustomPostTypeResolver::NAME
141
                );
142
            case self::SCHEMA_POSTS:
143
                return sprintf(
144
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
145
                    \__('posts', 'graphql-api'),
146
                    PostTypeResolver::NAME
147
                );
148
            case self::SCHEMA_USERS:
149
                return sprintf(
150
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
151
                    \__('users', 'graphql-api'),
152
                    UserTypeResolver::NAME
153
                );
154
            case self::SCHEMA_USER_ROLES:
155
                return sprintf(
156
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
157
                    \__('user roles', 'graphql-api'),
158
                    UserRoleTypeResolver::NAME
159
                );
160
            case self::SCHEMA_PAGES:
161
                return sprintf(
162
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
163
                    \__('pages', 'graphql-api'),
164
                    PageTypeResolver::NAME
165
                );
166
            case self::SCHEMA_MEDIA:
167
                return sprintf(
168
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
169
                    \__('media elements', 'graphql-api'),
170
                    MediaTypeResolver::NAME
171
                );
172
            case self::SCHEMA_COMMENTS:
173
                return sprintf(
174
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
175
                    \__('comments', 'graphql-api'),
176
                    CommentTypeResolver::NAME
177
                );
178
            case self::SCHEMA_POST_TAGS:
179
                return sprintf(
180
                    \__('Query %1$s, through type <code>%2$s</code> added to the schema', 'graphql-api'),
181
                    \__('post tags', 'graphql-api'),
182
                    PostTagTypeResolver::NAME
183
                );
184
            case self::SCHEMA_CUSTOMPOSTS:
185
                return \__('Base functionality for all custom posts', 'graphql-api');
186
            case self::SCHEMA_TAGS:
187
                return \__('Base functionality for all tags', 'graphql-api');
188
        }
189
        return parent::getDescription($module);
190
    }
191
192
    /**
193
     * Does the module have HTML Documentation?
194
     */
195
    public function hasDocumentation(string $module): bool
196
    {
197
        switch ($module) {
198
            case self::SCHEMA_POSTS:
199
            case self::SCHEMA_PAGES:
200
            case self::SCHEMA_USERS:
201
            case self::SCHEMA_USER_ROLES:
202
            case self::SCHEMA_COMMENTS:
203
            case self::SCHEMA_TAGS:
204
            case self::SCHEMA_POST_TAGS:
205
            case self::SCHEMA_MEDIA:
206
                return false;
207
        }
208
        return $this->upstreamHasDocumentation($module);
209
    }
210
211
    /**
212
     * Indicate if the given value is valid for that option
213
     *
214
     * @param string $module
215
     * @param string $option
216
     * @param mixed $value
217
     * @return bool
218
     */
219
    public function isValidValue(string $module, string $option, $value): bool
220
    {
221
        if (in_array(
222
            $module,
223
            [
224
                self::SCHEMA_CUSTOMPOSTS,
225
                // self::SCHEMA_GENERIC_CUSTOMPOSTS,
226
                // self::SCHEMA_POSTS,
227
                self::SCHEMA_USERS,
228
                self::SCHEMA_TAGS,
229
                // self::SCHEMA_PAGES,
230
            ]
231
        ) && in_array(
232
            $option,
233
            [
234
                self::OPTION_LIST_DEFAULT_LIMIT,
235
                self::OPTION_LIST_MAX_LIMIT,
236
            ]
237
        )) {
238
            // It can't be less than -1, or 0
239
            if ($value < -1 or $value === 0) {
240
                return false;
241
            }
242
        }
243
        return parent::isValidValue($module, $option, $value);
244
    }
245
246
    /**
247
     * Default value for an option set by the module
248
     *
249
     * @param string $module
250
     * @param string $option
251
     * @return mixed Anything the setting might be: an array|string|bool|int|null
252
     */
253
    public function getSettingsDefaultValue(string $module, string $option)
254
    {
255
        $defaultValues = [
256
            self::SCHEMA_CUSTOMPOSTS => [
257
                self::OPTION_LIST_DEFAULT_LIMIT => 10,
258
                self::OPTION_LIST_MAX_LIMIT => 100,
259
                self::OPTION_USE_SINGLE_TYPE_INSTEAD_OF_UNION_TYPE => false,
260
            ],
261
            self::SCHEMA_GENERIC_CUSTOMPOSTS => [
262
                // self::OPTION_LIST_DEFAULT_LIMIT => 10,
263
                // self::OPTION_LIST_MAX_LIMIT => 100,
264
                self::OPTION_CUSTOMPOST_TYPES => ['post'],
265
            ],
266
            self::SCHEMA_POSTS => [
267
                // self::OPTION_LIST_DEFAULT_LIMIT => 10,
268
                // self::OPTION_LIST_MAX_LIMIT => 100,
269
                self::OPTION_ADD_TYPE_TO_CUSTOMPOST_UNION_TYPE => true,
270
            ],
271
            self::SCHEMA_PAGES => [
272
                // self::OPTION_LIST_DEFAULT_LIMIT => 10,
273
                // self::OPTION_LIST_MAX_LIMIT => 100,
274
                self::OPTION_ADD_TYPE_TO_CUSTOMPOST_UNION_TYPE => false,
275
            ],
276
            self::SCHEMA_USERS => [
277
                self::OPTION_LIST_DEFAULT_LIMIT => 10,
278
                self::OPTION_LIST_MAX_LIMIT => 100,
279
            ],
280
            self::SCHEMA_TAGS => [
281
                self::OPTION_LIST_DEFAULT_LIMIT => 20,
282
                self::OPTION_LIST_MAX_LIMIT => 200,
283
            ],
284
        ];
285
        return $defaultValues[$module][$option];
286
    }
287
288
    /**
289
     * Array with the inputs to show as settings for the module
290
     *
291
     * @param string $module
292
     * @return array
293
     */
294
    public function getSettings(string $module): array
295
    {
296
        $moduleSettings = parent::getSettings($module);
297
        // Common variables to set the limit on the schema types
298
        $limitArg = 'limit';
299
        $unlimitedValue = -1;
300
        $defaultLimitMessagePlaceholder = \__('Number of results from querying %s when argument <code>%s</code> is not provided. Use <code>%s</code> for unlimited', 'graphql-api');
301
        $maxLimitMessagePlaceholder = \__('Maximum number of results from querying %s. Use <code>%s</code> for unlimited', 'graphql-api');
302
        // Do the if one by one, so that the SELECT do not get evaluated unless needed
303
        if (in_array($module, [
304
                self::SCHEMA_CUSTOMPOSTS,
305
                // self::SCHEMA_GENERIC_CUSTOMPOSTS,
306
                // self::SCHEMA_POSTS,
307
                self::SCHEMA_USERS,
308
                self::SCHEMA_TAGS,
309
                // self::SCHEMA_PAGES,
310
            ])
311
        ) {
312
            $moduleEntries = [
313
                self::SCHEMA_CUSTOMPOSTS => [
314
                    'entities' => \__('custom posts', 'graphql-api'),
315
                ],
316
                // self::SCHEMA_GENERIC_CUSTOMPOSTS => [
317
                //     'genericCustomPosts' => null,
318
                // ],
319
                // self::SCHEMA_POSTS => [
320
                //     'posts' => null,
321
                // ],
322
                self::SCHEMA_USERS => [
323
                    'entities' => \__('users', 'graphql-api'),
324
                ],
325
                self::SCHEMA_TAGS => [
326
                    'entities' => \__('tags', 'graphql-api'),
327
                ],
328
                // self::SCHEMA_PAGES => [
329
                //     'pages' => null,
330
                // ],
331
            ];
332
            $moduleEntry =$moduleEntries[$module];
333
            // If the options is not provided, use the default one
334
            $entities = $moduleEntry['entities'];
335
            $options = $moduleEntry['options'] ?? [
336
                self::OPTION_LIST_DEFAULT_LIMIT,
337
                self::OPTION_LIST_MAX_LIMIT,
338
            ];
339
            list(
340
                $defaultLimitOption,
341
                $maxLimitOption,
342
            ) = $options;
343
            $moduleSettings[] = [
344
                Properties::INPUT => $defaultLimitOption,
345
                Properties::NAME => $this->getSettingOptionName(
346
                    $module,
347
                    $defaultLimitOption
348
                ),
349
                Properties::TITLE => sprintf(
350
                    \__('Default limit for %s', 'graphql-api'),
351
                    $entities
352
                ),
353
                Properties::DESCRIPTION => sprintf(
354
                    $defaultLimitMessagePlaceholder,
355
                    $entities,
356
                    $limitArg,
357
                    $unlimitedValue
358
                ),
359
                Properties::TYPE => Properties::TYPE_INT,
360
                Properties::MIN_NUMBER => -1,
361
            ];
362
            $moduleSettings[] = [
363
                Properties::INPUT => $maxLimitOption,
364
                Properties::NAME => $this->getSettingOptionName(
365
                    $module,
366
                    $maxLimitOption
367
                ),
368
                Properties::TITLE => sprintf(
369
                    \__('Max limit for %s', 'graphql-api'),
370
                    $entities
371
                ),
372
                Properties::DESCRIPTION => sprintf(
373
                    $maxLimitMessagePlaceholder,
374
                    $entities,
375
                    $unlimitedValue
376
                ),
377
                Properties::TYPE => Properties::TYPE_INT,
378
                Properties::MIN_NUMBER => -1,
379
            ];
380
381
            if ($module == self::SCHEMA_CUSTOMPOSTS) {
382
                $option = self::OPTION_USE_SINGLE_TYPE_INSTEAD_OF_UNION_TYPE;
383
                $moduleSettings[] = [
384
                    Properties::INPUT => $option,
385
                    Properties::NAME => $this->getSettingOptionName(
386
                        $module,
387
                        $option
388
                    ),
389
                    Properties::TITLE => \__('Use single type instead of union type?', 'graphql-api'),
390
                    Properties::DESCRIPTION => sprintf(
391
                        \__('If type <code>%s</code> is composed of only one type (eg: <code>%s</code>), then return this single type directly in field <code>%s</code>?', 'graphql-api'),
392
                        CustomPostUnionTypeResolver::NAME,
393
                        PostTypeResolver::NAME,
394
                        'customPosts'
395
                    ),
396
                    Properties::TYPE => Properties::TYPE_BOOL,
397
                ];
398
            }
399
        } elseif (in_array($module, [
400
                self::SCHEMA_POSTS,
401
                self::SCHEMA_PAGES,
402
            ])
403
        ) {
404
            $titlePlaceholder = sprintf(
405
                \__('Include type <code>%1$s</code> in <code>%2$s</code>?', 'graphql-api'),
406
                '%1$s',
407
                CustomPostUnionTypeResolver::NAME
408
            );
409
            $moduleTitles = [
410
                self::SCHEMA_POSTS => sprintf(
411
                    $titlePlaceholder,
412
                    PostTypeResolver::NAME
413
                ),
414
                self::SCHEMA_PAGES => sprintf(
415
                    $titlePlaceholder,
416
                    PageTypeResolver::NAME
417
                ),
418
            ];
419
            $descriptionPlaceholder = sprintf(
420
                \__('Results of type <code>%1$s</code> will be included when querying a field of type <code>%2$s</code> (such as <code>%3$s</code>)', 'graphql-api'),
421
                '%1$s',
422
                CustomPostUnionTypeResolver::NAME,
423
                'customPosts'
424
            );
425
            $moduleDescriptions = [
426
                self::SCHEMA_POSTS => sprintf(
427
                    $descriptionPlaceholder,
428
                    PostTypeResolver::NAME
429
                ),
430
                self::SCHEMA_PAGES => sprintf(
431
                    $descriptionPlaceholder,
432
                    PageTypeResolver::NAME
433
                ),
434
            ];
435
            $option = self::OPTION_ADD_TYPE_TO_CUSTOMPOST_UNION_TYPE;
436
            $moduleSettings[] = [
437
                Properties::INPUT => $option,
438
                Properties::NAME => $this->getSettingOptionName(
439
                    $module,
440
                    $option
441
                ),
442
                Properties::TITLE => $moduleTitles[$module],
443
                Properties::DESCRIPTION => $moduleDescriptions[$module],
444
                Properties::TYPE => Properties::TYPE_BOOL,
445
            ];
446
        } elseif ($module == self::SCHEMA_GENERIC_CUSTOMPOSTS) {
447
            // Get the list of custom post types from the system
448
            $genericCustomPostTypes = \get_post_types();
449
            // Not all custom post types make sense or are allowed.
450
            // Remove the ones that do not
451
            $genericCustomPostTypes = array_values(array_diff(
452
                $genericCustomPostTypes,
453
                [
454
                    // Post Types from GraphQL API that contain private data
455
                    GraphQLAccessControlListPostType::POST_TYPE,
456
                    GraphQLCacheControlListPostType::POST_TYPE,
457
                    GraphQLFieldDeprecationListPostType::POST_TYPE,
458
                    GraphQLSchemaConfigurationPostType::POST_TYPE,
459
                    GraphQLEndpointPostType::POST_TYPE,
460
                    GraphQLPersistedQueryPostType::POST_TYPE,
461
                    // WordPress internal CPTs
462
                    // Attachment not allowed because its post_status="inherit",
463
                    // not "publish", and the API filters by "publish" entries
464
                    'attachment',
465
                    'revision',
466
                    'nav_menu_item',
467
                    'custom_css',
468
                    'customize_changeset',
469
                    'oembed_cache',
470
                    'user_request',
471
                    'wp_block',
472
                    'wp_area',
473
                ]
474
            ));
475
            // Allow plugins to remove their own unwanted custom post types
476
            $genericCustomPostTypes = \apply_filters(
477
                self::HOOK_GENERIC_CUSTOMPOST_TYPES,
478
                $genericCustomPostTypes
479
            );
480
            // The possible values must have key and value
481
            $possibleValues = [];
482
            foreach ($genericCustomPostTypes as $genericCustomPostType) {
483
                $possibleValues[$genericCustomPostType] = $genericCustomPostType;
484
            }
485
            // Set the setting
486
            $option = self::OPTION_CUSTOMPOST_TYPES;
487
            $moduleSettings[] = [
488
                Properties::INPUT => $option,
489
                Properties::NAME => $this->getSettingOptionName(
490
                    $module,
491
                    $option
492
                ),
493
                Properties::TITLE => \__('Included custom post types', 'graphql-api'),
494
                Properties::DESCRIPTION => sprintf(
495
                    \__('Results from these custom post types will be included when querying a field with type <code>%s</code> (such as <code>%s</code>)<br/>Press <code>ctrl</code> or <code>shift</code> keys to select more than one', 'graphql-api'),
496
                    GenericCustomPostTypeResolver::NAME,
497
                    'genericCustomPosts'
498
                ),
499
                Properties::TYPE => Properties::TYPE_ARRAY,
500
                // Fetch all Schema Configurations from the DB
501
                Properties::POSSIBLE_VALUES => $possibleValues,
502
                Properties::IS_MULTIPLE => true,
503
            ];
504
        }
505
506
        return $moduleSettings;
507
    }
508
}
509