Completed
Branch EDTR/master (46772d)
by
unknown
19:53 queued 09:36
created

VenueConnectionResolver::should_execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: is_valid_offset, should_execute
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
     * Here, we map the args from the input, then we make sure that we're only querying
55
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
56
     * handle batch resolution of the posts.
57
     *
58
     * @return array
59
     */
60
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
61
    public function get_query_args()
62
    {
63
        $query_args = [];
64
65
        /**
66
         * Prepare for later use
67
         */
68
        $last = ! empty($this->args['last']) ? $this->args['last'] : null;
69
        $first = ! empty($this->args['first']) ? $this->args['first'] : null;
70
71
        /**
72
         * Set limit the highest value of $first and $last, with a (filterable) max of 100
73
         */
74
        $query_args['limit'] = min(
75
            max(absint($first), absint($last), 10),
76
            $this->query_amount
77
        ) + 1;
78
79
        /**
80
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
81
         */
82
        $input_fields = [];
83
        if (! empty($this->args['where'])) {
84
            $input_fields = $this->sanitize_input_fields($this->args['where']);
85
        }
86
87
        /**
88
         * Determine where we're at in the Graph and adjust the query context appropriately.
89
         * For example, if we're querying for datetime as a field of event query, this will automatically
90
         * set the query to pull datetimes that belong to that event.
91
         * We can set more cases for other source types.
92
         */
93
        if (is_object($this->source)) {
94
            switch (true) {
95
                // Assumed to be an event
96
                case $this->source instanceof Post:
97
                    $query_args[] = ['Event.EVT_ID' => $this->source->ID];
98
                    break;
99
                case $this->source instanceof EE_Event:
100
                    $query_args[] = ['Event.EVT_ID' => $this->source->ID()];
101
                    break;
102
            }
103
        }
104
105
        /**
106
         * Merge the input_fields with the default query_args
107
         */
108
        if (! empty($input_fields)) {
109
            $query_args = array_merge($query_args, $input_fields);
110
        }
111
112
        /**
113
         * Return the $query_args
114
         */
115
        return $query_args;
116
    }
117
118
119
    /**
120
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
121
     * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
122
     * this was quick. I'd be down to explore more dynamic ways to map this, but for
123
     * now this gets the job done.
124
     *
125
     * @param array $query_args
126
     * @return array
127
     */
128
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
129
    public function sanitize_input_fields(array $query_args)
130
    {
131
        $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...
132
            'orderBy' => 'order_by',
133
            'order'   => 'order',
134
        ];
135
136
        /**
137
         * Return the Query Args
138
         */
139
        return ! empty($query_args) && is_array($query_args) ? $query_args : [];
140
    }
141
}
142