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 ( c30996...045419 )
by Leonardo
03:04
created

getDependedModuleLists()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
nc 6
nop 1
dl 0
loc 17
ccs 0
cts 17
cp 0
crap 42
rs 9.2222
c 1
b 0
f 0
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 GraphQLByPoP\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 API_HIERARCHY = Plugin::NAMESPACE . '\api-hierarchy';
26
27
    /**
28
     * Setting options
29
     */
30
    public const OPTION_EDITING_ACCESS_SCHEME = 'editing-access-scheme';
31
    public const OPTION_PATH = 'path';
32
    public const OPTION_SCHEMA_CONFIGURATION_ID = 'schema-configuration-id';
33
34
    /**
35
     * Setting option values
36
     */
37
    public const OPTION_VALUE_NO_VALUE_ID = 0;
38
39
    public static function getModulesToResolve(): array
40
    {
41
        return [
42
            // self::MAIN,
43
            self::SINGLE_ENDPOINT,
44
            self::PERSISTED_QUERIES,
45
            self::CUSTOM_ENDPOINTS,
46
            self::SCHEMA_CONFIGURATION,
47
            self::API_HIERARCHY,
48
            self::SCHEMA_EDITING_ACCESS,
49
        ];
50
    }
51
52
    public function getDependedModuleLists(string $module): array
53
    {
54
        switch ($module) {
55
            case self::PERSISTED_QUERIES:
56
            case self::SINGLE_ENDPOINT:
57
            case self::CUSTOM_ENDPOINTS:
58
                return [];
59
            case self::SCHEMA_CONFIGURATION:
60
            case self::API_HIERARCHY:
61
                return [
62
                    [
63
                        self::PERSISTED_QUERIES,
64
                        self::CUSTOM_ENDPOINTS,
65
                    ],
66
                ];
67
        }
68
        return parent::getDependedModuleLists($module);
69
    }
70
71
    // public function canBeDisabled(string $module): bool
72
    // {
73
    //     switch ($module) {
74
    //         case self::SCHEMA_EDITING_ACCESS:
75
    //         // case self::MAIN:
76
    //             return false;
77
    //     }
78
    //     return parent::canBeDisabled($module);
79
    // }
80
81
    // public function isHidden(string $module): bool
82
    // {
83
    //     switch ($module) {
84
    //         case self::MAIN:
85
    //             return true;
86
    //     }
87
    //     return parent::isHidden($module);
88
    // }
89
90
    public function getName(string $module): string
91
    {
92
        $names = [
93
            // self::MAIN => \__('Main', 'graphql-api'),
94
            self::SCHEMA_EDITING_ACCESS => \__('Schema Editing Access', 'graphql-api'),
95
            self::SINGLE_ENDPOINT => \__('Single Endpoint', 'graphql-api'),
96
            self::PERSISTED_QUERIES => \__('Persisted Queries', 'graphql-api'),
97
            self::CUSTOM_ENDPOINTS => \__('Custom Endpoints', 'graphql-api'),
98
            self::SCHEMA_CONFIGURATION => \__('Schema Configuration', 'graphql-api'),
99
            self::API_HIERARCHY => \__('API Hierarchy', 'graphql-api'),
100
        ];
101
        return $names[$module] ?? $module;
102
    }
103
104
    public function getDescription(string $module): string
105
    {
106
        switch ($module) {
107
            // case self::MAIN:
108
            //     return \__('Artificial module for defining the main settings', 'graphql-api');
109
            case self::SCHEMA_EDITING_ACCESS:
110
                return \__('Grant access to users other than admins to edit the GraphQL schema', 'graphql-api');
111
            case self::SINGLE_ENDPOINT:
112
                return \sprintf(
113
                    \__('Expose a single GraphQL endpoint under <code>%s</code>, with unrestricted access', 'graphql-api'),
114
                    GraphQLEndpointForWPComponentConfiguration::getGraphQLAPIEndpoint()
115
                );
116
            case self::PERSISTED_QUERIES:
117
                return \__('Expose predefined responses through a custom URL, akin to using GraphQL queries to publish REST endpoints', 'graphql-api');
118
            case self::CUSTOM_ENDPOINTS:
119
                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');
120
            case self::SCHEMA_CONFIGURATION:
121
                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');
122
            case self::API_HIERARCHY:
123
                return \__('Create a hierarchy of API endpoints extending from other endpoints, and inheriting their properties', 'graphql-api');
124
        }
125
        return parent::getDescription($module);
126
    }
127
128
    public function isEnabledByDefault(string $module): bool
129
    {
130
        switch ($module) {
131
            case self::SINGLE_ENDPOINT:
132
                return false;
133
        }
134
        return parent::isEnabledByDefault($module);
135
    }
136
137
    /**
138
     * Default value for an option set by the module
139
     *
140
     * @param string $module
141
     * @param string $option
142
     * @return mixed Anything the setting might be: an array|string|bool|int|null
143
     */
144
    public function getSettingsDefaultValue(string $module, string $option)
145
    {
146
        $defaultValues = [
147
            self::SCHEMA_EDITING_ACCESS => [
148
                self::OPTION_EDITING_ACCESS_SCHEME => UserAuthorization::ACCESS_SCHEME_ADMIN_ONLY,
149
            ],
150
            self::SINGLE_ENDPOINT => [
151
                self::OPTION_PATH => '/graphql/',
152
            ],
153
            self::CUSTOM_ENDPOINTS => [
154
                self::OPTION_PATH => 'graphql',
155
            ],
156
            self::PERSISTED_QUERIES => [
157
                self::OPTION_PATH => 'graphql-query',
158
            ],
159
            self::SCHEMA_CONFIGURATION => [
160
                self::OPTION_SCHEMA_CONFIGURATION_ID => self::OPTION_VALUE_NO_VALUE_ID,
161
            ],
162
        ];
163
        return $defaultValues[$module][$option];
164
    }
165
166
    /**
167
     * Array with the inputs to show as settings for the module
168
     *
169
     * @param string $module
170
     * @return array
171
     */
172
    public function getSettings(string $module): array
173
    {
174
        $moduleSettings = parent::getSettings($module);
175
        $moduleRegistry = ModuleRegistryFacade::getInstance();
176
        // Do the if one by one, so that the SELECT do not get evaluated unless needed
177
        if ($module == self::SCHEMA_EDITING_ACCESS) {
178
            /**
179
             * Write Access Scheme
180
             * If `"admin"`, only the admin can compose a GraphQL query and endpoint
181
             * If `"post"`, the workflow from creating posts is employed (i.e. Author role can create
182
             * but not publish the query, Editor role can publish it, etc)
183
             */
184
            $option = self::OPTION_EDITING_ACCESS_SCHEME;
185
            $moduleSettings[] = [
186
                Properties::INPUT => $option,
187
                Properties::NAME => $this->getSettingOptionName(
188
                    $module,
189
                    $option
190
                ),
191
                Properties::TITLE => \__('Editing Access Scheme', 'graphql-api'),
192
                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'),
193
                Properties::TYPE => Properties::TYPE_STRING,
194
                Properties::POSSIBLE_VALUES => [
195
                    UserAuthorization::ACCESS_SCHEME_ADMIN_ONLY => \__('Admin user(s) only', 'graphql-api'),
196
                    UserAuthorization::ACCESS_SCHEME_POST => \__('Use same access workflow as for editing posts', 'graphql-api'),
197
                ],
198
            ];
199
        } elseif ($module == self::SINGLE_ENDPOINT) {
200
            $option = self::OPTION_PATH;
201
            $moduleSettings[] = [
202
                Properties::INPUT => $option,
203
                Properties::NAME => $this->getSettingOptionName(
204
                    $module,
205
                    $option
206
                ),
207
                Properties::TITLE => \__('Endpoint path', 'graphql-api'),
208
                Properties::DESCRIPTION => \__('URL path to expose the single GraphQL endpoint', 'graphql-api'),
209
                Properties::TYPE => Properties::TYPE_STRING,
210
            ];
211
        } elseif ($module == self::CUSTOM_ENDPOINTS) {
212
            $option = self::OPTION_PATH;
213
            $moduleSettings[] = [
214
                Properties::INPUT => $option,
215
                Properties::NAME => $this->getSettingOptionName(
216
                    $module,
217
                    $option
218
                ),
219
                Properties::TITLE => \__('Base path', 'graphql-api'),
220
                Properties::DESCRIPTION => \__('URL base path to expose the Custom Endpoint', 'graphql-api'),
221
                Properties::TYPE => Properties::TYPE_STRING,
222
            ];
223
        } elseif ($module == self::PERSISTED_QUERIES) {
224
            $option = self::OPTION_PATH;
225
            $moduleSettings[] = [
226
                Properties::INPUT => $option,
227
                Properties::NAME => $this->getSettingOptionName(
228
                    $module,
229
                    $option
230
                ),
231
                Properties::TITLE => \__('Base path', 'graphql-api'),
232
                Properties::DESCRIPTION => \__('URL base path to expose the Persisted Query', 'graphql-api'),
233
                Properties::TYPE => Properties::TYPE_STRING,
234
            ];
235
        } elseif ($module == self::SCHEMA_CONFIGURATION) {
236
            $whereModules = [];
237
            $maybeWhereModules = [
238
                self::CUSTOM_ENDPOINTS,
239
                self::PERSISTED_QUERIES,
240
            ];
241
            foreach ($maybeWhereModules as $maybeWhereModule) {
242
                if ($moduleRegistry->isModuleEnabled($maybeWhereModule)) {
243
                    $whereModules[] = '▹ ' . $this->getName($maybeWhereModule);
244
                }
245
            }
246
            // Build all the possible values by fetching all the Schema Configuration posts
247
            $possibleValues = [
248
                self::OPTION_VALUE_NO_VALUE_ID => \__('None', 'graphql-api'),
249
            ];
250
            if ($customPosts = \get_posts([
251
                    'posts_per_page' => -1,
252
                    'post_type' => GraphQLSchemaConfigurationPostType::POST_TYPE,
253
                    'post_status' => 'publish',
254
                ])
255
            ) {
256
                foreach ($customPosts as $customPost) {
257
                    $possibleValues[$customPost->ID] = $customPost->post_title;
258
                }
259
            }
260
            $option = self::OPTION_SCHEMA_CONFIGURATION_ID;
261
            $moduleSettings[] = [
262
                Properties::INPUT => $option,
263
                Properties::NAME => $this->getSettingOptionName(
264
                    $module,
265
                    $option
266
                ),
267
                Properties::TITLE => \__('Default Schema Configuration', 'graphql-api'),
268
                Properties::DESCRIPTION => sprintf(
269
                    \__('Schema Configuration to use when option <code>"Default"</code> is selected (in %s)', 'graphql-api'),
270
                    implode(
271
                        \__(', ', 'graphql-api'),
272
                        $whereModules
273
                    )
274
                ),
275
                Properties::TYPE => Properties::TYPE_INT,
276
                // Fetch all Schema Configurations from the DB
277
                Properties::POSSIBLE_VALUES => $possibleValues,
278
            ];
279
        }
280
        return $moduleSettings;
281
    }
282
}
283