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 ( b3aed9...f2c63f )
by Leonardo
03:02
created

CPTFieldResolver::getFieldNamesToResolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLAPI\GraphQLAPI\Admin\FieldResolvers;
6
7
use PoP\CustomPosts\Facades\CustomPostTypeAPIFacade;
8
use PoP\CustomPosts\TypeResolvers\CustomPostTypeResolver;
9
use PoP\Engine\TypeResolvers\RootTypeResolver;
10
use PoP\ComponentModel\Schema\SchemaDefinition;
11
use PoP\ComponentModel\Schema\TypeCastingHelpers;
12
use PoP\Translation\Facades\TranslationAPIFacade;
13
use PoP\ComponentModel\TypeResolvers\TypeResolverInterface;
14
use PoP\ComponentModel\FieldResolvers\AbstractQueryableFieldResolver;
15
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLCacheControlListPostType;
16
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLAccessControlListPostType;
17
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLSchemaConfigurationPostType;
18
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLFieldDeprecationListPostType;
19
use PoP\CustomPosts\Types\Status;
20
21
/**
22
 * FieldResolver for the Custom Post Types from this plugin
23
 */
24
class CPTFieldResolver extends AbstractQueryableFieldResolver
25
{
26
    /**
27
     * Option to tell the hook to not remove the private CPTs when querying
28
     */
29
    public const QUERY_OPTION_ALLOW_QUERYING_PRIVATE_CPTS = 'allow-querying-private-cpts';
30
31
    public static function getClassesToAttachTo(): array
32
    {
33
        return array(RootTypeResolver::class);
34
    }
35
36
    public static function getFieldNamesToResolve(): array
37
    {
38
        return [
39
            'accessControlLists',
40
            'cacheControlLists',
41
            'fieldDeprecationLists',
42
            'schemaConfigurations',
43
        ];
44
    }
45
46
    /**
47
     * These fields are used only by the application, so no need to
48
     * expose them to the user
49
     *
50
     * @param TypeResolverInterface $typeResolver
51
     * @param string $fieldName
52
     * @return boolean
53
     */
54
    public function skipAddingToSchemaDefinition(TypeResolverInterface $typeResolver, string $fieldName): bool
55
    {
56
        return true;
57
    }
58
59
    public function getSchemaFieldType(TypeResolverInterface $typeResolver, string $fieldName): ?string
60
    {
61
        $types = [
62
            'accessControlLists' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID),
63
            'cacheControlLists' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID),
64
            'fieldDeprecationLists' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID),
65
            'schemaConfigurations' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID),
66
        ];
67
        return $types[$fieldName] ?? parent::getSchemaFieldType($typeResolver, $fieldName);
68
    }
69
70
    public function getSchemaFieldDescription(TypeResolverInterface $typeResolver, string $fieldName): ?string
71
    {
72
        $translationAPI = TranslationAPIFacade::getInstance();
73
        $descriptions = [
74
            'accessControlLists' => $translationAPI->__('Access Control Lists', 'graphql-api'),
75
            'cacheControlLists' => $translationAPI->__('Cache Control Lists', 'graphql-api'),
76
            'fieldDeprecationLists' => $translationAPI->__('Field Deprecation Lists', 'graphql-api'),
77
            'schemaConfigurations' => $translationAPI->__('Schema Configurations', 'graphql-api'),
78
        ];
79
        return $descriptions[$fieldName] ?? parent::getSchemaFieldDescription($typeResolver, $fieldName);
80
    }
81
82
    public function getSchemaFieldArgs(TypeResolverInterface $typeResolver, string $fieldName): array
83
    {
84
        $schemaFieldArgs = parent::getSchemaFieldArgs($typeResolver, $fieldName);
85
        switch ($fieldName) {
86
            case 'accessControlLists':
87
            case 'cacheControlLists':
88
            case 'fieldDeprecationLists':
89
            case 'schemaConfigurations':
90
                $schemaFieldArgs = array_merge(
91
                    $schemaFieldArgs,
92
                    $this->getFieldArgumentsSchemaDefinitions($typeResolver, $fieldName)
93
                );
94
                // Remove the "customPostTypes" field argument
95
                $schemaFieldArgs = array_filter(
96
                    $schemaFieldArgs,
97
                    function ($schemaFieldArg) {
98
                        return $schemaFieldArg[SchemaDefinition::ARGNAME_NAME] != 'customPostTypes';
99
                    }
100
                );
101
                break;
102
        }
103
        return $schemaFieldArgs;
104
    }
105
106
    public function enableOrderedSchemaFieldArgs(TypeResolverInterface $typeResolver, string $fieldName): bool
107
    {
108
        switch ($fieldName) {
109
            case 'accessControlLists':
110
            case 'cacheControlLists':
111
            case 'fieldDeprecationLists':
112
            case 'schemaConfigurations':
113
                return false;
114
        }
115
        return parent::enableOrderedSchemaFieldArgs($typeResolver, $fieldName);
116
    }
117
118
    protected function getQuery(
119
        TypeResolverInterface $typeResolver,
120
        $resultItem,
121
        string $fieldName,
122
        array $fieldArgs = []
123
    ): array {
124
        switch ($fieldName) {
125
            case 'accessControlLists':
126
            case 'cacheControlLists':
127
            case 'fieldDeprecationLists':
128
            case 'schemaConfigurations':
129
                $query = [
130
                    'limit' => -1,
131
                    'custom-post-status' => [
132
                        Status::PUBLISHED,
133
                    ],
134
                ];
135
                return $query;
136
        }
137
        return [];
138
    }
139
140
    public function resolveValue(
141
        TypeResolverInterface $typeResolver,
142
        $resultItem,
143
        string $fieldName,
144
        array $fieldArgs = [],
145
        ?array $variables = null,
146
        ?array $expressions = null,
147
        array $options = []
148
    ) {
149
        $customPostTypeAPI = CustomPostTypeAPIFacade::getInstance();
150
        switch ($fieldName) {
151
            case 'accessControlLists':
152
            case 'cacheControlLists':
153
            case 'fieldDeprecationLists':
154
            case 'schemaConfigurations':
155
                // Remove the "customPostTypes" field argument
156
                unset($fieldArgs['customPostTypes']);
157
                $query = $this->getQuery($typeResolver, $resultItem, $fieldName, $fieldArgs);
158
                // Execute for the corresponding field name
159
                $customPostTypes = [
160
                    'accessControlLists' => GraphQLAccessControlListPostType::POST_TYPE,
161
                    'cacheControlLists' => GraphQLCacheControlListPostType::POST_TYPE,
162
                    'fieldDeprecationLists' => GraphQLFieldDeprecationListPostType::POST_TYPE,
163
                    'schemaConfigurations' => GraphQLSchemaConfigurationPostType::POST_TYPE,
164
                ];
165
                $query['custom-post-types'] = [
166
                    $customPostTypes[$fieldName],
167
                ];
168
                $options = [
169
                    'return-type' => POP_RETURNTYPE_IDS,
170
                    // Do not use the limit set in the settings for custom posts
171
                    'skip-max-limit' => true,
172
                    // With this flag, the hook will not remove the private CPTs
173
                    self::QUERY_OPTION_ALLOW_QUERYING_PRIVATE_CPTS => true,
174
                ];
175
                $this->addFilterDataloadQueryArgs($options, $typeResolver, $fieldName, $fieldArgs);
176
                return $customPostTypeAPI->getCustomPosts($query, $options);
177
        }
178
179
        return parent::resolveValue(
180
            $typeResolver,
181
            $resultItem,
182
            $fieldName,
183
            $fieldArgs,
184
            $variables,
185
            $expressions,
186
            $options
187
        );
188
    }
189
190
    public function resolveFieldTypeResolverClass(
191
        TypeResolverInterface $typeResolver,
192
        string $fieldName,
193
        array $fieldArgs = []
194
    ): ?string {
195
        switch ($fieldName) {
196
            case 'accessControlLists':
197
            case 'cacheControlLists':
198
            case 'fieldDeprecationLists':
199
            case 'schemaConfigurations':
200
                return CustomPostTypeResolver::class;
201
        }
202
203
        return parent::resolveFieldTypeResolverClass($typeResolver, $fieldName, $fieldArgs);
204
    }
205
}
206