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

PriceConnectionResolver::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_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
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
19
    public function get_loader_name()
20
    {
21
        return 'espresso_price';
22
    }
23
24
    /**
25
     * @return EEM_Price
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_Price::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
     * Determine whether the Query should execute. If it's determined that the query should
54
     * not be run based on context such as, but not limited to, who the user is, where in the
55
     * ResolveTree the Query is, the relation to the node the Query is connected to, etc
56
     * Return false to prevent the query from executing.
57
     *
58
     * @return bool
59
     */
60
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
61
    public function should_execute()
62
    {
63
        if ($this->should_execute === false) {
64
            return false;
65
        }
66
67
        return $this->should_execute;
68
    }
69
70
71
    /**
72
     * Here, we map the args from the input, then we make sure that we're only querying
73
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
74
     * handle batch resolution of the posts.
75
     *
76
     * @return array
77
     * @throws EE_Error
78
     * @throws InvalidArgumentException
79
     * @throws ReflectionException
80
     * @throws InvalidDataTypeException
81
     * @throws InvalidInterfaceException
82
     */
83
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
84
    public function get_query_args()
85
    {
86
        $where_params = [];
87
        $query_args   = [];
88
89
        $query_args['limit'] = $this->getLimit();
90
91
        // Avoid multiple entries by join.
92
        $query_args['group_by'] = 'PRC_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['Ticket.TKT_ID']) && is_array($input_fields['Ticket.TKT_ID'])) {
103
                $input_fields['Ticket.TKT_ID'] = ['in', $input_fields['Ticket.TKT_ID']];
104
            }
105 View Code Duplication
            if (! empty($input_fields['Price_Type.PBT_ID']) && is_array($input_fields['Price_Type.PBT_ID'])) {
106
                $input_fields['Price_Type.PBT_ID'] = ['in', $input_fields['Price_Type.PBT_ID']];
107
            }
108 View Code Duplication
            if (! empty($input_fields['Price_Type.PRT_ID']) && is_array($input_fields['Price_Type.PRT_ID'])) {
109
                $input_fields['Price_Type.PRT_ID'] = ['in', $input_fields['Price_Type.PRT_ID']];
110
            }
111
        }
112
113
        /**
114
         * Determine where we're at in the Graph and adjust the query context appropriately.
115
         */
116
        if ($this->source instanceof EE_Ticket) {
117
            $where_params['Ticket.TKT_ID'] = $this->source->ID();
118
        }
119
120
        /**
121
         * Merge the input_fields with the default query_args
122
         */
123
        if (! empty($input_fields)) {
124
            $where_params = array_merge($where_params, $input_fields);
125
        }
126
127
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'PRC_ID');
128
129
        $query_args[] = $where_params;
130
131
        /**
132
         * Return the $query_args
133
         */
134
        return $query_args;
135
    }
136
137
138
    /**
139
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
140
     * friendly keys.
141
     *
142
     * @param array $where_args
143
     * @return array
144
     */
145
    public function sanitizeInputFields(array $where_args)
146
    {
147
        $arg_mapping = [
148
            'ticket'          => 'Ticket.TKT_ID',
149
            'ticketIn'        => 'Ticket.TKT_ID',
150
            'ticketIdIn'      => 'Ticket.TKT_ID',
151
            'ticketId'        => 'Ticket.TKT_ID', // priority.
152
            'priceType'       => 'Price_Type.PRT_ID',
153
            'priceTypeIn'     => 'Price_Type.PRT_ID',
154
            'priceTypeIdIn'   => 'Price_Type.PRT_ID',
155
            'priceTypeId'     => 'Price_Type.PRT_ID', // priority.
156
            'priceBaseType'   => 'Price_Type.PBT_ID',
157
            'priceBaseTypeIn' => 'Price_Type.PBT_ID',
158
        ];
159
        return $this->sanitizeWhereArgsForInputFields(
160
            $where_args,
161
            $arg_mapping,
162
            ['ticket', 'ticketIn', 'priceType', 'priceTypeIn']
163
        );
164
    }
165
}
166