Completed
Branch EDTR/master (872e60)
by
unknown
27:04 queued 17:58
created

PriceConnectionResolver::get_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use EE_Ticket;
6
use EE_Error;
7
use EEM_Price;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use ReflectionException;
12
13
/**
14
 * Class PriceConnectionResolver
15
 */
16
class PriceConnectionResolver extends AbstractConnectionResolver
17
{
18
19
    /**
20
     * @return EEM_Price
21
     * @throws EE_Error
22
     * @throws InvalidArgumentException
23
     * @throws InvalidDataTypeException
24
     * @throws InvalidInterfaceException
25
     */
26
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
27
    public function get_query()
28
    {
29
        return EEM_Price::instance();
30
    }
31
32
33
    /**
34
     * Return an array of items from the query
35
     *
36
     * @return array
37
     */
38
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
39
    public function get_items()
40
    {
41
        $results = $this->query->get_col($this->query_args);
42
43
        return ! empty($results) ? $results : [];
44
    }
45
46
47
    /**
48
     * Determine whether the Query should execute. If it's determined that the query should
49
     * not be run based on context such as, but not limited to, who the user is, where in the
50
     * ResolveTree the Query is, the relation to the node the Query is connected to, etc
51
     * Return false to prevent the query from executing.
52
     *
53
     * @return bool
54
     */
55
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
56
    public function should_execute()
57
    {
58
        if ($this->should_execute === false) {
59
            return false;
60
        }
61
62
        return $this->should_execute;
63
    }
64
65
66
    /**
67
     * Here, we map the args from the input, then we make sure that we're only querying
68
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
69
     * handle batch resolution of the posts.
70
     *
71
     * @return array
72
     * @throws EE_Error
73
     * @throws InvalidArgumentException
74
     * @throws ReflectionException
75
     * @throws InvalidDataTypeException
76
     * @throws InvalidInterfaceException
77
     */
78
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
79
    public function get_query_args()
80
    {
81
        $where_params = [];
82
        $query_args   = [];
83
84
        $query_args['limit'] = $this->getLimit();
85
86
        // Avoid multiple entries by join.
87
        $query_args['group_by'] = 'PRC_ID';
88
89
        /**
90
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
91
         */
92
        $input_fields = [];
93
        if (! empty($this->args['where'])) {
94
            $input_fields = $this->sanitizeInputFields($this->args['where']);
95
96
            // Use the proper operator.
97 View Code Duplication
            if (! empty($input_fields['Ticket.TKT_ID']) && is_array($input_fields['Ticket.TKT_ID'])) {
98
                $input_fields['Ticket.TKT_ID'] = ['in', $input_fields['Ticket.TKT_ID']];
99
            }
100 View Code Duplication
            if (! empty($input_fields['Price_Type.PBT_ID']) && is_array($input_fields['Price_Type.PBT_ID'])) {
101
                $input_fields['Price_Type.PBT_ID'] = ['in', $input_fields['Price_Type.PBT_ID']];
102
            }
103 View Code Duplication
            if (! empty($input_fields['Price_Type.PRT_ID']) && is_array($input_fields['Price_Type.PRT_ID'])) {
104
                $input_fields['Price_Type.PRT_ID'] = ['in', $input_fields['Price_Type.PRT_ID']];
105
            }
106
        }
107
108
        /**
109
         * Determine where we're at in the Graph and adjust the query context appropriately.
110
         */
111
        if ($this->source instanceof EE_Ticket) {
112
            $where_params['Ticket.TKT_ID'] = $this->source->ID();
113
        }
114
115
        /**
116
         * Merge the input_fields with the default query_args
117
         */
118
        if (! empty($input_fields)) {
119
            $where_params = array_merge($where_params, $input_fields);
120
        }
121
122
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'PRC_ID');
123
124
        $query_args[] = $where_params;
125
126
        /**
127
         * Return the $query_args
128
         */
129
        return $query_args;
130
    }
131
132
133
    /**
134
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
135
     * friendly keys.
136
     *
137
     * @param array $where_args
138
     * @return array
139
     */
140
    public function sanitizeInputFields(array $where_args)
141
    {
142
        $arg_mapping = [
143
            'ticket'          => 'Ticket.TKT_ID',
144
            'ticketIn'        => 'Ticket.TKT_ID',
145
            'ticketIdIn'      => 'Ticket.TKT_ID',
146
            'ticketId'        => 'Ticket.TKT_ID', // priority.
147
            'priceType'       => 'Price_Type.PRT_ID',
148
            'priceTypeIn'     => 'Price_Type.PRT_ID',
149
            'priceTypeIdIn'   => 'Price_Type.PRT_ID',
150
            'priceTypeId'     => 'Price_Type.PRT_ID', // priority.
151
            'priceBaseType'   => 'Price_Type.PBT_ID',
152
            'priceBaseTypeIn' => 'Price_Type.PBT_ID',
153
        ];
154
        return $this->sanitizeWhereArgsForInputFields(
155
            $where_args,
156
            $arg_mapping,
157
            ['ticket', 'ticketIn', 'priceType', 'priceTypeIn']
158
        );
159
    }
160
}
161