Completed
Branch EDTR/refactor-master (bcaf81)
by
unknown
41:30 queued 30:37
created

TicketConnectionResolver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 9.92 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 13
loc 131
rs 10
c 0
b 0
f 0
wmc 15
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 49 7
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
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use EE_Datetime;
6
use EE_Error;
7
use EEM_Ticket;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use ReflectionException;
12
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
13
14
/**
15
 * Class DatetimeConnectionResolver
16
 *
17
 */
18
class TicketConnectionResolver extends AbstractConnectionResolver {
19
20
    /**
21
     * @return EEM_Ticket
22
     * @throws EE_Error
23
     * @throws InvalidArgumentException
24
     * @throws InvalidDataTypeException
25
     * @throws InvalidInterfaceException
26
     */
27
	public function get_query() {
28
		return EEM_Ticket::instance();
29
	}
30
31
	/**
32
	 * Return an array of items from the query
33
	 *
34
	 * @return array
35
	 */
36
	public function get_items() {
37
38
		$results = $this->query->get_col( $this->query_args );
39
40
		return ! empty( $results ) ? $results : [];
41
	}
42
43
	/**
44
	 * Determine whether the Query should execute. If it's determined that the query should
45
	 * not be run based on context such as, but not limited to, who the user is, where in the
46
	 * ResolveTree the Query is, the relation to the node the Query is connected to, etc
47
	 *
48
	 * Return false to prevent the query from executing.
49
	 *
50
	 * @return bool
51
	 */
52
	public function should_execute() {
53
54
		if ( false === $this->should_execute ) {
55
			return false;
56
		}
57
58
		return $this->should_execute;
59
	}
60
61
62
    /**
63
     * Here, we map the args from the input, then we make sure that we're only querying
64
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
65
     * handle batch resolution of the posts.
66
     *
67
     * @return array
68
     * @throws EE_Error
69
     * @throws InvalidArgumentException
70
     * @throws ReflectionException
71
     * @throws InvalidDataTypeException
72
     * @throws InvalidInterfaceException
73
     */
74
	public function get_query_args() {
75
76
		$query_args = [];
77
78
		/**
79
		 * Prepare for later use
80
		 */
81
		$last  = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
82
		$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
83
84
		/**
85
		 * Set limit the highest value of $first and $last, with a (filterable) max of 100
86
		 */
87
		$query_args['limit'] = min(
88
		    max( absint( $first ), absint( $last ), 10 ),
89
            $this->query_amount
90
        ) + 1;
91
92
		/**
93
		 * Collect the input_fields and sanitize them to prepare them for sending to the Query
94
		 */
95
		$input_fields = [];
96
		if ( ! empty( $this->args['where'] ) ) {
97
			$input_fields = $this->sanitize_input_fields( $this->args['where'] );
98
		}
99
100
		/**
101
		 * Determine where we're at in the Graph and adjust the query context appropriately.
102
		 */
103
		if (is_object( $this->source )) {
104
			switch (true) {
105
				case $this->source instanceof EE_Datetime:
106
					$query_args[] = [ 'Datetime.DTT_ID' => $this->source->ID() ];
107
					break;
108
			}
109
		}
110
111
		/**
112
		 * Merge the input_fields with the default query_args
113
		 */
114
		if ( ! empty( $input_fields ) ) {
115
			$query_args = array_merge( $query_args, $input_fields );
116
		}
117
118
		/**
119
		 * Return the $query_args
120
		 */
121
		return $query_args;
122
	}
123
124
125
    /**
126
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
127
     * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
128
     * this was quick. I'd be down to explore more dynamic ways to map this, but for
129
     * now this gets the job done.
130
     *
131
     * @param array $query_args
132
     * @return array
133
     */
134 View Code Duplication
	public function sanitize_input_fields(array $query_args) {
135
136
		$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...
137
			'orderBy' => 'order_by',
138
			'order'   => 'order',
139
		];
140
141
		/**
142
		 * Return the Query Args
143
		 */
144
		return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
145
146
	}
147
148
}
149