Completed
Branch EDTR/master (bb716e)
by
unknown
57:55 queued 49:04
created

FieldResolver::resolveId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\resolvers;
4
5
use EE_Attendee;
6
use EE_Base_Class;
7
use EE_Event;
8
use EE_Venue;
9
use EE_Datetime;
10
use EE_Ticket;
11
use EE_State;
12
use EEM_State;
13
use EE_Country;
14
use EEM_Country;
15
use EE_Error;
16
use EventEspresso\core\exceptions\InvalidDataTypeException;
17
use EventEspresso\core\exceptions\InvalidInterfaceException;
18
use EventEspresso\core\exceptions\UnexpectedEntityException;
19
use EventEspresso\core\services\graphql\resolvers\ResolverBase;
20
use EventEspresso\core\services\graphql\fields\GraphQLField;
21
use Exception;
22
use InvalidArgumentException;
23
use ReflectionException;
24
use WPGraphQL\Data\DataSource;
25
use GraphQL\Deferred;
26
use GraphQL\Error\UserError;
27
use GraphQL\Type\Definition\ResolveInfo;
28
use GraphQLRelay\Relay;
29
use WPGraphQL\AppContext;
30
31
/**
32
 * Class FieldResolver
33
 * Field resolver for a GraphQL type
34
 *
35
 * @package EventEspresso\core\domain\services\graphql\resolvers
36
 * @author  Manzoor Wani
37
 * @since   $VID:$
38
 */
39
class FieldResolver extends ResolverBase
40
{
41
42
    /**
43
     * @var mixed $model
44
     */
45
    protected $model;
46
47
    /**
48
     * @var array $fields .
49
     */
50
    protected $fields;
51
52
53
    /**
54
     * FieldResolver constructor.
55
     *
56
     * @param mixed $model  The model instance.
57
     * @param array $fields The fields registered for the type.
58
     */
59
    public function __construct($model, array $fields)
60
    {
61
        $this->model = $model;
62
        $this->fields = $fields;
63
    }
64
65
66
    /**
67
     * @param mixed       $source  The source that's passed down the GraphQL queries
68
     * @param array       $args    The inputArgs on the field
69
     * @param AppContext  $context The AppContext passed down the GraphQL tree
70
     * @param ResolveInfo $info    The ResolveInfo passed down the GraphQL tree
71
     * @return string
72
     * @throws EE_Error
73
     * @throws Exception
74
     * @throws InvalidArgumentException
75
     * @throws InvalidDataTypeException
76
     * @throws InvalidInterfaceException
77
     * @throws ReflectionException
78
     * @throws UserError
79
     * @throws UnexpectedEntityException
80
     * @since $VID:$
81
     */
82
    public function resolve($source, array $args, AppContext $context, ResolveInfo $info)
83
    {
84
        $fieldName = $info->fieldName;
85
        $field = isset($this->fields[ $fieldName ]) ? $this->fields[ $fieldName ] : null;
86
        // Field should exist in teh registered fields
87
        if ($field instanceof GraphQLField) {
88
            // check if the field should be resolved.
89
            if (! $field->shouldResolve()) {
90
                return null;
91
            }
92
            // Give priority to the internal resolver.
93
            if ($field->hasInternalResolver()) {
94
                return $field->resolve($source, $args, $context, $info);
95
            }
96
            // Check if the field has a key mapped to model.
97
            if (! empty($field->key())) {
98
                $value = $source->{$field->key()}();
99
                return $field->mayBeFormatValue($value);
100
            }
101
            switch ($fieldName) {
102
                case 'id': // the global ID
103
                    return $this->resolveId($source);
104
                case 'parent':
105
                    return $this->resolveParent($source);
106
                case 'event':
107
                    return $this->resolveEvent($source, $args, $context);
108
                case 'wpUser':
109
                    return $this->resolveWpUser($source, $args, $context);
110
                case 'state': // Venue
111
                    return $this->resolveState($source);
112
                case 'country': // State, Venue
113
                    return $this->resolveCountry($source);
114
            }
115
        }
116
        return null;
117
    }
118
119
120
    /**
121
     * Resolve the global ID
122
     *
123
     * @param mixed     $source
124
     * @return string|null
125
     * @throws Exception
126
     * @since $VID:$
127
     */
128
    public function resolveId($source)
129
    {
130
        $ID = $source instanceof EE_Base_Class ? $source->ID() : 0;
131
132
        return $ID ? Relay::toGlobalId($this->model->item_name(), $ID) : null;
133
    }
134
135
136
    /**
137
     * @param mixed     $source
138
     * @param           $args
139
     * @param           $context
140
     * @return Deferred|null
141
     * @throws Exception
142
     * @since $VID:$
143
     */
144
    public function resolveWpUser($source, $args, $context)
145
    {
146
        $user_id = $source->wp_user();
147
        return $user_id
148
            ? DataSource::resolve_user($user_id, $context)
149
            : null;
150
    }
151
152
153
    /**
154
     * @param mixed $source
155
     * @return EE_Base_Class|null
156
     * @throws EE_Error
157
     * @throws InvalidArgumentException
158
     * @throws InvalidDataTypeException
159
     * @throws InvalidInterfaceException
160
     * @throws ReflectionException
161
     * @since $VID:$
162
     */
163
    public function resolveParent($source)
164
    {
165
        return $this->model->get_one_by_ID($source->parent());
166
    }
167
168
169
    /**
170
     * @param mixed       $source
171
     * @param             $args
172
     * @param             $context
173
     * @return Deferred|null
174
     * @throws EE_Error
175
     * @throws InvalidArgumentException
176
     * @throws InvalidDataTypeException
177
     * @throws InvalidInterfaceException
178
     * @throws ReflectionException
179
     * @throws UserError
180
     * @throws UnexpectedEntityException
181
     * @since $VID:$
182
     */
183
    public function resolveEvent($source, $args, $context)
184
    {
185
        switch (true) {
186
            case $source instanceof EE_Datetime:
187
                $event = $source->event();
188
                break;
189
            case $source instanceof EE_Venue:
190
            case $source instanceof EE_Ticket:
191
                $event = $source->get_related_event();
0 ignored issues
show
Bug introduced by
The method get_related_event does only exist in EE_Ticket, but not in EE_Venue.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
192
                break;
193
            default:
194
                $event = null;
195
                break;
196
        }
197
        return $event instanceof EE_Event
198
            ? DataSource::resolve_post_object($event->ID(), $context)
199
            : null;
200
    }
201
202
203
    /**
204
     * @param mixed $source The source instance.
205
     * @return EE_Base_Class|null
206
     * @throws EE_Error
207
     * @throws InvalidArgumentException
208
     * @throws InvalidDataTypeException
209
     * @throws InvalidInterfaceException
210
     * @since $VID:$
211
     */
212 View Code Duplication
    public function resolveState($source)
213
    {
214
        switch (true) {
215
            case $source instanceof EE_Attendee:
216
            case $source instanceof EE_Venue:
217
                $state_id = $source->state_ID();
218
                break;
219
            default:
220
                $state_id = null;
221
                break;
222
        }
223
        return $state_id
224
            ? EEM_State::instance()->get_one_by_ID($state_id)
225
            : null;
226
    }
227
228
229
    /**
230
     * @param mixed $source The source instance.
231
     * @return EE_Base_Class|null
232
     * @throws EE_Error
233
     * @throws InvalidArgumentException
234
     * @throws InvalidDataTypeException
235
     * @throws InvalidInterfaceException
236
     * @since $VID:$
237
     */
238 View Code Duplication
    public function resolveCountry($source)
239
    {
240
        switch (true) {
241
            case $source instanceof EE_State:
242
                $country_iso = $source->country_iso();
243
                break;
244
            case $source instanceof EE_Venue:
245
                $country_iso = $source->country_ID();
246
                break;
247
            default:
248
                $country_iso = null;
249
                break;
250
        }
251
252
        return $country_iso
253
            ? EEM_Country::instance()->get_one_by_ID($country_iso)
254
            : null;
255
    }
256
}
257