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 ( 045419...f7c842 )
by Leonardo
02:57
created

getSettingsDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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