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

DatetimeConnectionResolver::get_ids()   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
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use EE_Error;
6
use EEM_Datetime;
7
use EE_Event;
8
use EE_Ticket;
9
use EE_Checkin;
10
use EventEspresso\core\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidInterfaceException;
12
use InvalidArgumentException;
13
use WPGraphQL\Model\Post;
14
15
/**
16
 * Class DatetimeConnectionResolver
17
 *
18
 */
19
class DatetimeConnectionResolver extends AbstractConnectionResolver
20
{
21
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
22
    public function get_loader_name()
23
    {
24
        return 'espresso_datetime';
25
    }
26
27
    /**
28
     * @return EEM_Datetime
29
     * @throws EE_Error
30
     * @throws InvalidArgumentException
31
     * @throws InvalidDataTypeException
32
     * @throws InvalidInterfaceException
33
     */
34
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
35
    public function get_query()
36
    {
37
        return EEM_Datetime::instance();
38
    }
39
40
    /**
41
     * Return an array of item IDs from the query
42
     *
43
     * @return array
44
     */
45
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
46
    public function get_ids()
47
    {
48
        $results = $this->query->get_col($this->query_args);
49
50
        return ! empty($results) ? $results : [];
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
     *
58
     * Return false to prevent the query from executing.
59
     *
60
     * @return bool
61
     */
62
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
63
    public function should_execute()
64
    {
65
        if (false === $this->should_execute) {
66
            return false;
67
        }
68
69
        return $this->should_execute;
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
     * @throws EE_Error
79
     * @throws InvalidArgumentException
80
     * @throws InvalidDataTypeException
81
     * @throws InvalidInterfaceException
82
     */
83
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
84
    public function get_query_args()
85
    {
86
        $where_params = ['DTT_deleted' => ['IN', [true, false]]];
87
        $query_args   = [];
88
89
        $query_args['limit'] = $this->getLimit();
90
91
        // Avoid multiple entries by join.
92
        $query_args['group_by'] = 'DTT_ID';
93
94
        /**
95
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
96
         */
97
        $input_fields = [];
98
        if (! empty($this->args['where'])) {
99
            $input_fields = $this->sanitizeInputFields($this->args['where']);
100
101
            // Use the proper operator.
102 View Code Duplication
            if (! empty($input_fields['EVT_ID']) && is_array($input_fields['EVT_ID'])) {
103
                $input_fields['EVT_ID'] = ['in', $input_fields['EVT_ID']];
104
            }
105 View Code Duplication
            if (! empty($input_fields['Ticket.TKT_ID']) && is_array($input_fields['Ticket.TKT_ID'])) {
106
                $input_fields['Ticket.TKT_ID'] = ['in', $input_fields['Ticket.TKT_ID']];
107
            }
108
        }
109
110
        /**
111
         * Determine where we're at in the Graph and adjust the query context appropriately.
112
         *
113
         * For example, if we're querying for datetime as a field of event query, this will automatically
114
         * set the query to pull datetimes that belong to that event.
115
         * We can set more cases for other source types.
116
         */
117
        if (is_object($this->source)) {
118
            switch (true) {
119
                // It's surely an event
120
                case $this->source instanceof Post:
121
                    $where_params['EVT_ID'] = $this->source->ID;
122
                    break;
123
                case $this->source instanceof EE_Event:
124
                    $where_params['EVT_ID'] = $this->source->ID();
125
                    break;
126
                case $this->source instanceof EE_Ticket:
127
                    $where_params['Ticket.TKT_ID'] = $this->source->ID();
128
                    break;
129
                case $this->source instanceof EE_Checkin:
130
                    $where_params['Checkin.CHK_ID'] = $this->source->ID();
131
                    break;
132
            }
133
        }
134
135
        /**
136
         * Merge the input_fields with the default query_args
137
         */
138
        if (! empty($input_fields)) {
139
            $where_params = array_merge($where_params, $input_fields);
140
        }
141
142
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'DTT_ID');
143
144 View Code Duplication
        if (! empty($this->args['where']['upcoming'])) {
145
            $where_params['DTT_EVT_start'] = array(
146
                '>',
147
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start')
148
            );
149
        }
150
151
        if (! empty($this->args['where']['active'])) {
152
            $where_params['DTT_EVT_start'] = array(
153
                '<',
154
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start')
155
            );
156
            $where_params['DTT_EVT_end'] = array(
157
                '>',
158
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')
159
            );
160
        }
161
162 View Code Duplication
        if (! empty($this->args['where']['expired'])) {
163
            $where_params['DTT_EVT_end'] = array(
164
                '<',
165
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')
166
            );
167
        }
168
169
        $query_args[] = $where_params;
170
171
        /**
172
         * Return the $query_args
173
         */
174
        return $query_args;
175
    }
176
177
178
    /**
179
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
180
     * friendly keys.
181
     *
182
     * @param array $where_args
183
     * @return array
184
     */
185
    public function sanitizeInputFields(array $where_args)
186
    {
187
        $arg_mapping = [
188
            'event'      => 'EVT_ID',
189
            'eventIn'    => 'EVT_ID',
190
            'eventId'    => 'EVT_ID',
191
            'eventIdIn'  => 'EVT_ID',
192
            'ticket'     => 'Ticket.TKT_ID',
193
            'ticketIn'   => 'Ticket.TKT_ID',
194
            'ticketId'   => 'Ticket.TKT_ID',
195
            'ticketIdIn' => 'Ticket.TKT_ID',
196
        ];
197
        return $this->sanitizeWhereArgsForInputFields(
198
            $where_args,
199
            $arg_mapping,
200
            ['event', 'eventIn', 'ticket', 'ticketIn']
201
        );
202
    }
203
}
204