Completed
Branch EDTR/refactor-fast-api-fetch (cef37c)
by
unknown
09:44 queued 46s
created

VenueConnectionResolver   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 9.85 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 13
loc 132
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_query() 0 3 1
A get_items() 0 6 2
A should_execute() 0 8 2
B get_query_args() 0 57 8
A sanitize_input_fields() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *     Event Espresso
4
 *     Manage events, sell tickets, and receive payments from your WordPress website.
5
 *     Copyright (c) 2008-2019 Event Espresso  All Rights Reserved.
6
 *
7
 *     This program is free software: you can redistribute it and/or modify
8
 *     it under the terms of the GNU General Public License as published by
9
 *     the Free Software Foundation, either version 3 of the License, or
10
 *     (at your option) any later version.
11
 *
12
 *     This program is distributed in the hope that it will be useful,
13
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *     GNU General Public License for more details.
16
 *
17
 *     You should have received a copy of the GNU General Public License
18
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 *     Event Espresso
23
 *     Manage events, sell tickets, and receive payments from your WordPress website.
24
 *     Copyright (c) 2008-2019 Event Espresso  All Rights Reserved.
25
 *
26
 *     This program is free software: you can redistribute it and/or modify
27
 *     it under the terms of the GNU General Public License as published by
28
 *     the Free Software Foundation, either version 3 of the License, or
29
 *     (at your option) any later version.
30
 *
31
 *     This program is distributed in the hope that it will be useful,
32
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 *     GNU General Public License for more details.
35
 *
36
 *     You should have received a copy of the GNU General Public License
37
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 */
39
40
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
41
42
use EE_Error;
43
use EEM_Venue;
44
use EE_Event;
45
use EventEspresso\core\exceptions\InvalidDataTypeException;
46
use EventEspresso\core\exceptions\InvalidInterfaceException;
47
use InvalidArgumentException;
48
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
49
use WPGraphQL\Model\Post;
50
51
/**
52
 * Class VenueConnectionResolver
53
 *
54
 */
55
class VenueConnectionResolver extends AbstractConnectionResolver {
56
57
    /**
58
     * @return EEM_Venue
59
     * @throws EE_Error
60
     * @throws InvalidArgumentException
61
     * @throws InvalidDataTypeException
62
     * @throws InvalidInterfaceException
63
     */
64
	public function get_query() {
65
		return EEM_Venue::instance();
66
	}
67
68
	/**
69
	 * Return an array of items from the query
70
	 *
71
	 * @return array
72
	 */
73
	public function get_items() {
74
75
		$results = $this->query->get_col( $this->query_args );
76
77
		return ! empty( $results ) ? $results : [];
78
	}
79
80
	/**
81
	 * Determine whether the Query should execute. If it's determined that the query should
82
	 * not be run based on context such as, but not limited to, who the user is, where in the
83
	 * ResolveTree the Query is, the relation to the node the Query is connected to, etc
84
	 *
85
	 * Return false to prevent the query from executing.
86
	 *
87
	 * @return bool
88
	 */
89
	public function should_execute() {
90
91
		if ( false === $this->should_execute ) {
92
			return false;
93
		}
94
95
		return $this->should_execute;
96
	}
97
98
	/**
99
	 * Here, we map the args from the input, then we make sure that we're only querying
100
	 * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
101
	 * handle batch resolution of the posts.
102
	 *
103
	 * @return array
104
	 */
105
	public function get_query_args() {
106
107
		$query_args = [];
108
109
		/**
110
		 * Prepare for later use
111
		 */
112
		$last  = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
113
		$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
114
115
		/**
116
		 * Set limit the highest value of $first and $last, with a (filterable) max of 100
117
		 */
118
		$query_args['limit'] = min(
119
		    max( absint( $first ), absint( $last ), 10 ),
120
            $this->query_amount
121
        ) + 1;
122
123
		/**
124
		 * Collect the input_fields and sanitize them to prepare them for sending to the Query
125
		 */
126
		$input_fields = [];
127
		if ( ! empty( $this->args['where'] ) ) {
128
			$input_fields = $this->sanitize_input_fields( $this->args['where'] );
129
		}
130
131
		/**
132
		 * Determine where we're at in the Graph and adjust the query context appropriately.
133
		 *
134
		 * For example, if we're querying for datetime as a field of event query, this will automatically
135
		 * set the query to pull datetimes that belong to that event.
136
		 * We can set more cases for other source types.
137
		 */
138
		if (is_object( $this->source )) {
139
			switch (true) {
140
				// Assumed to be an event
141
				case $this->source instanceof Post:
142
					$query_args[] = ['Event.EVT_ID' => $this->source->ID];
143
					break;
144
				case $this->source instanceof EE_Event:
145
					$query_args[] = ['Event.EVT_ID' => $this->source->ID()];
146
					break;
147
			}
148
		}
149
150
		/**
151
		 * Merge the input_fields with the default query_args
152
		 */
153
		if ( ! empty( $input_fields ) ) {
154
			$query_args = array_merge( $query_args, $input_fields );
155
		}
156
157
		/**
158
		 * Return the $query_args
159
		 */
160
		return $query_args;
161
	}
162
163
	/**
164
	 * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
165
	 * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
166
	 * this was quick. I'd be down to explore more dynamic ways to map this, but for
167
	 * now this gets the job done.
168
	 *
169
     * @param array $query_args
170
	 * @return array
171
	 */
172 View Code Duplication
	public function sanitize_input_fields(array $query_args) {
173
174
		$arg_mapping = [
0 ignored issues
show
Unused Code introduced by
$arg_mapping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
			'orderBy' => 'order_by',
176
			'order'   => 'order',
177
		];
178
179
		/**
180
		 * Return the Query Args
181
		 */
182
		return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
183
184
	}
185
186
}
187