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

AttendeeConnectionResolver::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_Attendee;
7
use EEM_Ticket;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use ReflectionException;
12
use Throwable;
13
14
/**
15
 * Class DatetimeConnectionResolver
16
 */
17
class AttendeeConnectionResolver extends AbstractConnectionResolver
18
{
19
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
20
    public function get_loader_name()
21
    {
22
        return 'espresso_attendee';
23
    }
24
25
    /**
26
     * @return EEM_Attendee
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_Attendee::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
     * @throws EE_Error
60
     * @throws InvalidArgumentException
61
     * @throws ReflectionException
62
     * @throws InvalidDataTypeException
63
     * @throws InvalidInterfaceException
64
     */
65
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
66
    public function get_query_args()
67
    {
68
        $where_params = [];
69
        $query_args   = [];
70
71
        $query_args['limit'] = $this->getLimit();
72
73
        // Avoid multiple entries by join.
74
        $query_args['group_by'] = 'ATT_ID';
75
76
        $query_args['default_where_conditions'] = 'minimum';
77
78
        /**
79
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
80
         */
81
        $input_fields = [];
82
        if (! empty($this->args['where'])) {
83
            $input_fields = $this->sanitizeInputFields($this->args['where']);
84
85
            // Since we do not have any falsy values in query params
86
            // Lets get rid of empty values
87
            $input_fields = array_filter($input_fields);
88
89
            // Use the proper operator.
90 View Code Duplication
            if (! empty($input_fields['Registration.Event.EVT_ID']) && is_array($input_fields['Registration.Event.EVT_ID'])) {
91
                $input_fields['Registration.Event.EVT_ID'] = ['IN', $input_fields['Registration.Event.EVT_ID']];
92
            }
93 View Code Duplication
            if (! empty($input_fields['Registration.Ticket.TKT_ID']) && is_array($input_fields['Registration.Ticket.TKT_ID'])) {
94
                $input_fields['Registration.Ticket.TKT_ID'] = ['IN', $input_fields['Registration.Ticket.TKT_ID']];
95
            }
96
            // If Ticket param is passed, it will have preference over Datetime param
97
            // So, use Datetime param only if a Ticket param is not passed
98
            if (! empty($input_fields['Datetime.DTT_ID']) && empty($input_fields['Registration.Ticket.TKT_ID'])) {
99
                $datetimeIds = $input_fields['Datetime.DTT_ID'];
100
                // Make sure it's an array, ready for "IN" operator
101
                $datetimeIds = is_array($datetimeIds) ? $datetimeIds : [$datetimeIds];
102
103
                try {
104
                    // Get related ticket IDs for the given dates
105
                    $ticketIds = EEM_Ticket::instance()->get_col([
106
                        [
107
                            'Datetime.DTT_ID' => ['IN', $datetimeIds],
108
                            'TKT_deleted'     => ['IN', [true, false]],
109
                        ],
110
                        'default_where_conditions' => 'minimum',
111
                    ]);
112
                } catch (Throwable $th) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
113
                    $ticketIds = [];
114
                }
115
116
                if (!empty($ticketIds)) {
117
                    $input_fields['Registration.Ticket.TKT_ID'] = ['IN', $ticketIds];
118
                }
119
            }
120
            // Since there is no relation between Attendee and Datetime, we need to remove it
121
            unset($input_fields['Datetime.DTT_ID']);
122
        }
123
124
        /**
125
         * Merge the input_fields with the default query_args
126
         */
127
        if (! empty($input_fields)) {
128
            $where_params = array_merge($where_params, $input_fields);
129
        }
130
131
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'ATT_ID');
132
133
        $query_args[] = $where_params;
134
135
        /**
136
         * Return the $query_args
137
         */
138
        return $query_args;
139
    }
140
141
142
    /**
143
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
144
     * friendly keys.
145
     *
146
     * @param array $where_args
147
     * @return array
148
     */
149
    public function sanitizeInputFields(array $where_args)
150
    {
151
        $arg_mapping = [
152
            // There is no direct relation between Attendee and Datetime
153
            // But we will handle it via Tickets related to given dates
154
            'datetime'      => 'Datetime.DTT_ID',
155
            'datetimeIn'    => 'Datetime.DTT_ID',
156
            'event'         => 'Registration.Event.EVT_ID',
157
            'eventIn'       => 'Registration.Event.EVT_ID',
158
            'regTicket'     => 'Registration.Ticket.TKT_ID',
159
            'regTicketIn'   => 'Registration.Ticket.TKT_ID',
160
            'regTicketIdIn' => 'Registration.Ticket.TKT_ID',
161
            'regTicketId'   => 'Registration.Ticket.TKT_ID', // priority.
162
            'regStatus'     => 'Registration.Status.STS_ID',
163
        ];
164
        return $this->sanitizeWhereArgsForInputFields(
165
            $where_args,
166
            $arg_mapping,
167
            ['datetime', 'datetimeIn', 'event', 'eventIn', 'regTicket', 'regTicketIn']
168
        );
169
    }
170
}
171