Completed
Branch EDTR/master (dbc914)
by
unknown
09:15 queued 40s
created

GraphQLField::args()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\graphql\fields;
4
5
use EE_Base_Class;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use LogicException;
8
use WPGraphQL\AppContext;
9
use EventEspresso\core\exceptions\InvalidDataTypeException;
10
use EventEspresso\core\exceptions\InvalidInterfaceException;
11
use InvalidArgumentException;
12
use ReflectionException;
13
14
/**
15
 * Class GraphQLField
16
 *
17
 * @package       Event Espresso
18
 * @author        Manzoor Wani
19
 */
20
class GraphQLField implements GraphQLFieldInterface
21
{
22
23
    /**
24
     * @var string $name
25
     */
26
    protected $name;
27
28
    /**
29
     * @var string|string[] $type
30
     */
31
    protected $type;
32
33
    /**
34
     * @var string|null $key
35
     */
36
    protected $key;
37
38
    /**
39
     * @var string $description
40
     */
41
    protected $description;
42
43
    /**
44
     * @var callable $formatter
45
     */
46
    protected $formatter;
47
48
    /**
49
     * @var callable $resolve
50
     */
51
    protected $resolver;
52
53
    /**
54
     * @var array $caps
55
     */
56
    protected $caps;
57
58
    /**
59
     * @var bool $use_for_input
60
     */
61
    protected $use_for_input = true;
62
63
    /**
64
     * @var bool $use_for_output
65
     */
66
    protected $use_for_output = true;
67
68
69
    /**
70
     * @param string          $name
71
     * @param string|string[] $type
72
     * @param string|null     $key
73
     * @param string          $description
74
     * @param callable|null   $formatter
75
     * @param callable|null   $resolver
76
     * @param array           $args
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     * @param array           $caps
78
     */
79
    public function __construct(
80
        $name,
81
        $type,
82
        $key = null,
83
        $description = '',
84
        callable $formatter = null,
85
        callable $resolver = null,
86
        array $caps = []
87
    ) {
88
        $this->name = $name;
89
        $this->type = $type;
90
        $this->key = $key;
91
        $this->description = $description;
92
        $this->formatter = $formatter;
93
        $this->resolver = $resolver;
94
        $this->caps = $caps;
95
    }
96
97
98
    /**
99
     * @return array
100
     */
101
    public function caps()
102
    {
103
        return $this->caps;
104
    }
105
106
107
    /**
108
     * @return string
109
     */
110
    public function description()
111
    {
112
        return $this->description;
113
    }
114
115
116
    /**
117
     * @return string
118
     */
119
    public function key()
120
    {
121
        return $this->key;
122
    }
123
124
125
    /**
126
     * @return string
127
     */
128
    public function name()
129
    {
130
        return $this->name;
131
    }
132
133
134
    /**
135
     * @return string|string[]
136
     */
137
    public function type()
138
    {
139
        return $this->type;
140
    }
141
142
143
    /**
144
     * Convert the field to array to be
145
     * able to pass as config to WP GraphQL
146
     *
147
     * @return array
148
     */
149
    public function toArray()
150
    {
151
        return get_object_vars($this);
152
    }
153
154
155
    /**
156
     * Sets the value for use_for_input.
157
     *
158
     * @param bool $use_for_input
159
     */
160
    protected function setUseForInput($use_for_input)
161
    {
162
        $this->use_for_input = filter_var($use_for_input, FILTER_VALIDATE_BOOLEAN);
163
    }
164
165
166
    /**
167
     * Whether the field should be used for
168
     * mutation inputs.
169
     *
170
     * @return bool
171
     */
172
    public function useForInput()
173
    {
174
        return (bool) $this->use_for_input;
175
    }
176
177
178
    /**
179
     * Whether the field should be used for
180
     * query outputs.
181
     *
182
     * @return bool
183
     */
184
    public function useForOutput()
185
    {
186
        return (bool) $this->use_for_output;
187
    }
188
189
190
    /**
191
     * Sets the value for use_for_output
192
     *
193
     * @param bool $use_for_output
194
     */
195
    protected function setUseForOutput($use_for_output)
196
    {
197
        $this->use_for_output = filter_var($use_for_output, FILTER_VALIDATE_BOOLEAN);
198
    }
199
200
201
    /**
202
     * Whether the field should resolve
203
     * based on the user caps etc.
204
     *
205
     * @return boolean
206
     */
207
    public function shouldResolve()
208
    {
209
        foreach ($this->caps as $cap) {
210
            if (! current_user_can($cap)) {
211
                return false;
212
            }
213
        }
214
        return true;
215
    }
216
217
218
    /**
219
     * Whether the field has an explicit resolver set.
220
     *
221
     * @return boolean
222
     */
223
    public function hasInternalResolver()
224
    {
225
        return is_callable($this->resolver);
226
    }
227
228
229
    /**
230
     * Whether the field has an explicit resolver set.
231
     *
232
     * @param mixed       $source  The source that's passed down the GraphQL queries
233
     * @param array       $args    The inputArgs on the field
234
     * @param AppContext  $context The AppContext passed down the GraphQL tree
235
     * @param ResolveInfo $info    The ResolveInfo passed down the GraphQL tree
236
     * @return mixed
237
     * @throws LogicException
238
     */
239
    public function resolve($source, array $args, AppContext $context, ResolveInfo $info)
240
    {
241
        if (! $this->hasInternalResolver()) {
242
            throw new LogicException('GraphQLField has no internal resolver.');
243
        }
244
        // dynamic methods using $this don't play nice
245
        // so capture resolver to a single var first
246
        $resolver = $this->resolver;
247
        return $resolver($source, $args, $context, $info);
248
    }
249
250
251
    /**
252
     * Checks if the format callback is set.
253
     * If yes, then uses it to format the value.
254
     *
255
     * @param mixed         $value
256
     * @param EE_Base_Class $source
257
     * @return mixed The formatted value.
258
     * @throws InvalidDataTypeException
259
     * @throws InvalidInterfaceException
260
     * @throws InvalidArgumentException
261
     * @throws ReflectionException
262
     */
263
    public function mayBeFormatValue($value, EE_Base_Class $source)
264
    {
265
        if (is_callable($this->formatter)) {
266
            // dynamic methods using $this don't play nice
267
            // so capture formatter to a single var first
268
            $formatter = $this->formatter;
269
            return $formatter($value, $source);
270
        }
271
        return $value;
272
    }
273
}
274