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

DatetimeConnectionResolver::get_items()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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_Datetime;
44
use EE_Event;
45
use EE_Ticket;
46
use EE_Checkin;
47
use EventEspresso\core\exceptions\InvalidDataTypeException;
48
use EventEspresso\core\exceptions\InvalidInterfaceException;
49
use InvalidArgumentException;
50
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
51
use WPGraphQL\Model\Post;
52
53
/**
54
 * Class DatetimeConnectionResolver
55
 *
56
 */
57
class DatetimeConnectionResolver extends AbstractConnectionResolver {
58
59
    /**
60
     * @return EEM_Datetime
61
     * @throws EE_Error
62
     * @throws InvalidArgumentException
63
     * @throws InvalidDataTypeException
64
     * @throws InvalidInterfaceException
65
     */
66
	public function get_query() {
67
		return EEM_Datetime::instance();
68
	}
69
70
	/**
71
	 * Return an array of items from the query
72
	 *
73
	 * @return array
74
	 */
75
	public function get_items() {
76
77
		$results = $this->query->get_col( $this->query_args );
78
79
		return ! empty( $results ) ? $results : [];
80
	}
81
82
	/**
83
	 * Determine whether the Query should execute. If it's determined that the query should
84
	 * not be run based on context such as, but not limited to, who the user is, where in the
85
	 * ResolveTree the Query is, the relation to the node the Query is connected to, etc
86
	 *
87
	 * Return false to prevent the query from executing.
88
	 *
89
	 * @return bool
90
	 */
91
	public function should_execute() {
92
93
		if ( false === $this->should_execute ) {
94
			return false;
95
		}
96
97
		return $this->should_execute;
98
	}
99
100
	/**
101
	 * Here, we map the args from the input, then we make sure that we're only querying
102
	 * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
103
	 * handle batch resolution of the posts.
104
	 *
105
	 * @return array
106
	 */
107
	public function get_query_args() {
108
109
		$query_args = [];
110
111
		/**
112
		 * Prepare for later use
113
		 */
114
		$last  = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
115
		$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
116
117
		/**
118
		 * Set limit the highest value of $first and $last, with a (filterable) max of 100
119
		 */
120
		$query_args['limit'] = min(
121
		    max( absint( $first ), absint( $last ), 10 ),
122
            $this->query_amount
123
        ) + 1;
124
125
		/**
126
		 * Collect the input_fields and sanitize them to prepare them for sending to the Query
127
		 */
128
		$input_fields = [];
129
		if ( ! empty( $this->args['where'] ) ) {
130
			$input_fields = $this->sanitize_input_fields( $this->args['where'] );
131
		}
132
133
		/**
134
		 * Determine where we're at in the Graph and adjust the query context appropriately.
135
		 *
136
		 * For example, if we're querying for datetime as a field of event query, this will automatically
137
		 * set the query to pull datetimes that belong to that event.
138
		 * We can set more cases for other source types.
139
		 */
140
		if (is_object( $this->source )) {
141
			switch (true) {
142
				// It's surely an event
143
				case $this->source instanceof Post:
144
					$query_args[] = ['EVT_ID' => $this->source->ID];
145
					break;
146
				case $this->source instanceof EE_Event:
147
					$query_args[] = ['EVT_ID' => $this->source->ID()];
148
					break;
149
				case $this->source instanceof EE_Ticket:
150
					$query_args[] = ['Ticket.TKT_ID' => $this->source->ID()];
151
					break;
152
				case $this->source instanceof EE_Checkin:
153
					$query_args[] = ['Checkin.CHK_ID' => $this->source->ID()];
154
					break;
155
			}
156
		}
157
158
		/**
159
		 * Merge the input_fields with the default query_args
160
		 */
161
		if ( ! empty( $input_fields ) ) {
162
			$query_args = array_merge( $query_args, $input_fields );
163
		}
164
165
		/**
166
		 * Return the $query_args
167
		 */
168
		return $query_args;
169
	}
170
171
172
    /**
173
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
174
     * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
175
     * this was quick. I'd be down to explore more dynamic ways to map this, but for
176
     * now this gets the job done.
177
     *
178
     * @param array $query_args
179
     * @return array
180
     */
181 View Code Duplication
	public function sanitize_input_fields(array $query_args) {
182
183
		$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...
184
			'orderBy' => 'order_by',
185
			'order'   => 'order',
186
		];
187
188
		/**
189
		 * Return the Query Args
190
		 */
191
		return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
192
193
	}
194
195
}
196