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

TicketConnectionResolver::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_Datetime;
6
use EE_Error;
7
use EEM_Ticket;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use ReflectionException;
12
13
/**
14
 * Class TicketConnectionResolver
15
 */
16
class TicketConnectionResolver extends AbstractConnectionResolver
17
{
18
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
19
    public function get_loader_name()
20
    {
21
        return 'espresso_ticket';
22
    }
23
24
    /**
25
     * @return EEM_Ticket
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_Ticket::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
     * @throws EE_Error
59
     * @throws InvalidArgumentException
60
     * @throws ReflectionException
61
     * @throws InvalidDataTypeException
62
     * @throws InvalidInterfaceException
63
     */
64
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
65
    public function get_query_args()
66
    {
67
        $where_params = ['TKT_deleted' => ['IN', [true, false]]];
68
        $query_args   = [];
69
70
        $query_args['limit'] = $this->getLimit();
71
72
        // Avoid multiple entries by join.
73
        $query_args['group_by'] = 'TKT_ID';
74
75
        $query_args['default_where_conditions'] = 'minimum';
76
77
        /**
78
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
79
         */
80
        $input_fields = [];
81
        if (! empty($this->args['where'])) {
82
            $input_fields = $this->sanitizeInputFields($this->args['where']);
83
84
            // Use the proper operator.
85 View Code Duplication
            if (! empty($input_fields['Datetime.DTT_ID']) && is_array($input_fields['Datetime.DTT_ID'])) {
86
                $input_fields['Datetime.DTT_ID'] = ['in', $input_fields['Datetime.DTT_ID']];
87
            }
88
        }
89
90
        /**
91
         * Determine where we're at in the Graph and adjust the query context appropriately.
92
         */
93
        if ($this->source instanceof EE_Datetime) {
94
            $where_params['Datetime.DTT_ID'] = $this->source->ID();
95
        }
96
97
        /**
98
         * Merge the input_fields with the default query_args
99
         */
100
        if (! empty($input_fields)) {
101
            $where_params = array_merge($where_params, $input_fields);
102
        }
103
104
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'TKT_ID');
105
106
        $query_args[] = $where_params;
107
108
        /**
109
         * Return the $query_args
110
         */
111
        return $query_args;
112
    }
113
114
115
    /**
116
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
117
     * friendly keys.
118
     *
119
     * @param array $where_args
120
     * @return array
121
     */
122
    public function sanitizeInputFields(array $where_args)
123
    {
124
        $arg_mapping = [
125
            'datetime'     => 'Datetime.DTT_ID',
126
            'datetimeIn'   => 'Datetime.DTT_ID',
127
            'datetimeIdIn' => 'Datetime.DTT_ID',
128
            'datetimeId'   => 'Datetime.DTT_ID', // priority.
129
        ];
130
        return $this->sanitizeWhereArgsForInputFields(
131
            $where_args,
132
            $arg_mapping,
133
            ['datetime', 'datetimeIn']
134
        );
135
    }
136
}
137