Completed
Branch EDTR/master (22f246)
by
unknown
36:30 queued 27:58
created

VenueConnectionResolver::sanitizeInputFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
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\Model\Post;
12
13
/**
14
 * Class VenueConnectionResolver
15
 */
16
class VenueConnectionResolver extends AbstractConnectionResolver
17
{
18
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
19
    public function get_loader_name()
20
    {
21
        return 'espresso_venue';
22
    }
23
24
    /**
25
     * @return EEM_Venue
26
     * @throws EE_Error
27
     * @throws InvalidArgumentException
28
     * @throws InvalidDataTypeException
29
     * @throws InvalidInterfaceException
30
     */
31
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
32
    public function get_query()
33
    {
34
        return EEM_Venue::instance();
35
    }
36
37
38
    /**
39
     * Return an array of item IDs from the query
40
     *
41
     * @return array
42
     */
43
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
44
    public function get_ids()
45
    {
46
        $results = $this->query->get_col($this->query_args);
47
48
        return ! empty($results) ? $results : [];
49
    }
50
51
52
    /**
53
     * Here, we map the args from the input, then we make sure that we're only querying
54
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
55
     * handle batch resolution of the posts.
56
     *
57
     * @return array
58
     */
59
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
60
    public function get_query_args()
61
    {
62
        $where_params = [];
63
        $query_args = [];
64
65
        $query_args['limit'] = $this->getLimit();
66
67
        // Avoid multiple entries by join.
68
        $query_args['group_by'] = 'VNU_ID';
69
70
        $query_args['default_where_conditions'] = 'minimum';
71
72
        /**
73
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
74
         */
75
        $input_fields = [];
76
        if (! empty($this->args['where'])) {
77
            $input_fields = $this->sanitizeInputFields($this->args['where']);
78
        }
79
80
        /**
81
         * Determine where we're at in the Graph and adjust the query context appropriately.
82
         * For example, if we're querying for datetime as a field of event query, this will automatically
83
         * set the query to pull datetimes that belong to that event.
84
         * We can set more cases for other source types.
85
         */
86
        if (is_object($this->source)) {
87
            switch (true) {
88
                // Assumed to be an event
89
                case $this->source instanceof Post:
90
                    $where_params['Event.EVT_ID'] = $this->source->ID;
91
                    break;
92
                case $this->source instanceof EE_Event:
93
                    $where_params['Event.EVT_ID'] = $this->source->ID();
94
                    break;
95
            }
96
        }
97
98
        /**
99
         * Merge the input_fields with the default query_args
100
         */
101
        if (! empty($input_fields)) {
102
            $where_params = array_merge($where_params, $input_fields);
103
        }
104
105
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'VNU_ID');
106
107
        $where_params = apply_filters(
108
            'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__venue_where_params',
109
            $where_params,
110
            $this->source,
111
            $this->args
112
        );
113
114
        $query_args[] = $where_params;
115
116
        /**
117
         * Return the $query_args
118
         */
119
        return apply_filters(
120
            'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__venue_query_args',
121
            $query_args,
122
            $this->source,
123
            $this->args
124
        );
125
    }
126
127
128
    /**
129
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
130
     * friendly keys.
131
     *
132
     * @param array $where_args
133
     * @return array
134
     */
135
    public function sanitizeInputFields(array $where_args)
136
    {
137
        return $this->sanitizeWhereArgsForInputFields(
138
            $where_args,
139
            [],
140
            []
141
        );
142
    }
143
}
144