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

TicketConnectionResolver::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_Datetime;
43
use EE_Error;
44
use EEM_Ticket;
45
use EventEspresso\core\exceptions\InvalidDataTypeException;
46
use EventEspresso\core\exceptions\InvalidInterfaceException;
47
use InvalidArgumentException;
48
use ReflectionException;
49
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
50
51
/**
52
 * Class DatetimeConnectionResolver
53
 *
54
 */
55
class TicketConnectionResolver extends AbstractConnectionResolver {
56
57
    /**
58
     * @return EEM_Ticket
59
     * @throws EE_Error
60
     * @throws InvalidArgumentException
61
     * @throws InvalidDataTypeException
62
     * @throws InvalidInterfaceException
63
     */
64
	public function get_query() {
65
		return EEM_Ticket::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
    /**
100
     * Here, we map the args from the input, then we make sure that we're only querying
101
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
102
     * handle batch resolution of the posts.
103
     *
104
     * @return array
105
     * @throws EE_Error
106
     * @throws InvalidArgumentException
107
     * @throws ReflectionException
108
     * @throws InvalidDataTypeException
109
     * @throws InvalidInterfaceException
110
     */
111
	public function get_query_args() {
112
113
		$query_args = [];
114
115
		/**
116
		 * Prepare for later use
117
		 */
118
		$last  = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
119
		$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
120
121
		/**
122
		 * Set limit the highest value of $first and $last, with a (filterable) max of 100
123
		 */
124
		$query_args['limit'] = min(
125
		    max( absint( $first ), absint( $last ), 10 ),
126
            $this->query_amount
127
        ) + 1;
128
129
		/**
130
		 * Collect the input_fields and sanitize them to prepare them for sending to the Query
131
		 */
132
		$input_fields = [];
133
		if ( ! empty( $this->args['where'] ) ) {
134
			$input_fields = $this->sanitize_input_fields( $this->args['where'] );
135
		}
136
137
		/**
138
		 * Determine where we're at in the Graph and adjust the query context appropriately.
139
		 */
140
		if (is_object( $this->source )) {
141
			switch (true) {
142
				case $this->source instanceof EE_Datetime:
143
					$query_args[] = [ 'Datetime.DTT_ID' => $this->source->ID() ];
144
					break;
145
			}
146
		}
147
148
		/**
149
		 * Merge the input_fields with the default query_args
150
		 */
151
		if ( ! empty( $input_fields ) ) {
152
			$query_args = array_merge( $query_args, $input_fields );
153
		}
154
155
		/**
156
		 * Return the $query_args
157
		 */
158
		return $query_args;
159
	}
160
161
162
    /**
163
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
164
     * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
165
     * this was quick. I'd be down to explore more dynamic ways to map this, but for
166
     * now this gets the job done.
167
     *
168
     * @param array $query_args
169
     * @return array
170
     */
171 View Code Duplication
	public function sanitize_input_fields(array $query_args) {
172
173
		$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...
174
			'orderBy' => 'order_by',
175
			'order'   => 'order',
176
		];
177
178
		/**
179
		 * Return the Query Args
180
		 */
181
		return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
182
183
	}
184
185
}
186