Completed
Branch EDTR/master (0d7008)
by
unknown
34:37 queued 26:06
created

VenueConnectionResolver   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 17
lcom 3
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_loader_name() 0 4 1
A get_query() 0 4 1
A get_ids() 0 6 2
A should_execute() 0 8 2
B get_query_args() 0 56 8
A sanitize_input_fields() 0 12 3
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use EE_Error;
6
use EEM_Venue;
7
use EE_Event;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EventEspresso\core\domai...tractConnectionResolver.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use WPGraphQL\Model\Post;
13
14
/**
15
 * Class VenueConnectionResolver
16
 */
17
class VenueConnectionResolver extends AbstractConnectionResolver
0 ignored issues
show
Bug introduced by
There is one abstract method is_valid_offset in this class; you could implement it, or declare this class as abstract.
Loading history...
18
{
19
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
20
    public function get_loader_name()
21
    {
22
        return 'espresso_venue';
23
    }
24
25
    /**
26
     * @return EEM_Venue
27
     * @throws EE_Error
28
     * @throws InvalidArgumentException
29
     * @throws InvalidDataTypeException
30
     * @throws InvalidInterfaceException
31
     */
32
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
33
    public function get_query()
34
    {
35
        return EEM_Venue::instance();
36
    }
37
38
39
    /**
40
     * Return an array of item IDs from the query
41
     *
42
     * @return array
43
     */
44
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
45
    public function get_ids()
46
    {
47
        $results = $this->query->get_col($this->query_args);
48
49
        return ! empty($results) ? $results : [];
50
    }
51
52
53
    /**
54
     * Determine whether the Query should execute. If it's determined that the query should
55
     * not be run based on context such as, but not limited to, who the user is, where in the
56
     * ResolveTree the Query is, the relation to the node the Query is connected to, etc
57
     * Return false to prevent the query from executing.
58
     *
59
     * @return bool
60
     */
61
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
62
    public function should_execute()
63
    {
64
        if (false === $this->should_execute) {
65
            return false;
66
        }
67
68
        return $this->should_execute;
69
    }
70
71
72
    /**
73
     * Here, we map the args from the input, then we make sure that we're only querying
74
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
75
     * handle batch resolution of the posts.
76
     *
77
     * @return array
78
     */
79
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
80
    public function get_query_args()
81
    {
82
        $query_args = [];
83
84
        /**
85
         * Prepare for later use
86
         */
87
        $last = ! empty($this->args['last']) ? $this->args['last'] : null;
88
        $first = ! empty($this->args['first']) ? $this->args['first'] : null;
89
90
        /**
91
         * Set limit the highest value of $first and $last, with a (filterable) max of 100
92
         */
93
        $query_args['limit'] = min(
94
            max(absint($first), absint($last), 10),
95
            $this->query_amount
96
        ) + 1;
97
98
        /**
99
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
100
         */
101
        $input_fields = [];
102
        if (! empty($this->args['where'])) {
103
            $input_fields = $this->sanitize_input_fields($this->args['where']);
104
        }
105
106
        /**
107
         * Determine where we're at in the Graph and adjust the query context appropriately.
108
         * For example, if we're querying for datetime as a field of event query, this will automatically
109
         * set the query to pull datetimes that belong to that event.
110
         * We can set more cases for other source types.
111
         */
112
        if (is_object($this->source)) {
113
            switch (true) {
114
                // Assumed to be an event
115
                case $this->source instanceof Post:
116
                    $query_args[] = ['Event.EVT_ID' => $this->source->ID];
117
                    break;
118
                case $this->source instanceof EE_Event:
119
                    $query_args[] = ['Event.EVT_ID' => $this->source->ID()];
120
                    break;
121
            }
122
        }
123
124
        /**
125
         * Merge the input_fields with the default query_args
126
         */
127
        if (! empty($input_fields)) {
128
            $query_args = array_merge($query_args, $input_fields);
129
        }
130
131
        /**
132
         * Return the $query_args
133
         */
134
        return $query_args;
135
    }
136
137
138
    /**
139
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
140
     * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
141
     * this was quick. I'd be down to explore more dynamic ways to map this, but for
142
     * now this gets the job done.
143
     *
144
     * @param array $query_args
145
     * @return array
146
     */
147
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
148
    public function sanitize_input_fields(array $query_args)
149
    {
150
        $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...
151
            'orderBy' => 'order_by',
152
            'order'   => 'order',
153
        ];
154
155
        /**
156
         * Return the Query Args
157
         */
158
        return ! empty($query_args) && is_array($query_args) ? $query_args : [];
159
    }
160
}
161