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

PriceConnectionResolver::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_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
     * 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 = [];
68
        $query_args   = [];
69
70
        $query_args['limit'] = $this->getLimit();
71
72
        // Avoid multiple entries by join.
73
        $query_args['group_by'] = 'PRC_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['PRC_ID']) && is_array($input_fields['PRC_ID'])) {
86
                $input_fields['PRC_ID'] = ['in', $input_fields['PRC_ID']];
87
            }
88 View Code Duplication
            if (! empty($input_fields['Ticket.TKT_ID']) && is_array($input_fields['Ticket.TKT_ID'])) {
89
                $input_fields['Ticket.TKT_ID'] = ['in', $input_fields['Ticket.TKT_ID']];
90
            }
91 View Code Duplication
            if (! empty($input_fields['Price_Type.PBT_ID']) && is_array($input_fields['Price_Type.PBT_ID'])) {
92
                $input_fields['Price_Type.PBT_ID'] = ['in', $input_fields['Price_Type.PBT_ID']];
93
            }
94 View Code Duplication
            if (! empty($input_fields['Price_Type.PRT_ID']) && is_array($input_fields['Price_Type.PRT_ID'])) {
95
                $input_fields['Price_Type.PRT_ID'] = ['in', $input_fields['Price_Type.PRT_ID']];
96
            }
97
        }
98
99
        /**
100
         * Determine where we're at in the Graph and adjust the query context appropriately.
101
         */
102
        if ($this->source instanceof EE_Ticket) {
103
            $where_params['Ticket.TKT_ID'] = $this->source->ID();
104
        }
105
106
        /**
107
         * Merge the input_fields with the default query_args
108
         */
109
        if (! empty($input_fields)) {
110
            $where_params = array_merge($where_params, $input_fields);
111
        }
112
113
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'PRC_ID');
114
115
        // If default prices should be included.
116
        if (! empty($this->args['where']['includeDefaultPrices'])) {
117
            /**
118
             * We need to get each price that
119
             * - satisfies $where_params above
120
             * OR
121
             * - it's a default non trashed price
122
             */
123
            $where_params = [
124
                'OR' => [
125
                    // use extra OR instead of AND to avoid it getting overridden
126
                    'OR' => [
127
                        'AND' => [
128
                            'PRC_deleted'    => 0,
129
                            'PRC_is_default' => 1,
130
                        ]
131
                    ],
132
                    'AND' => $where_params,
133
                ],
134
            ];
135
        }
136
137
        $query_args[] = $where_params;
138
139
        /**
140
         * Return the $query_args
141
         */
142
        return $query_args;
143
    }
144
145
146
    /**
147
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
148
     * friendly keys.
149
     *
150
     * @param array $where_args
151
     * @return array
152
     */
153
    public function sanitizeInputFields(array $where_args)
154
    {
155
        $arg_mapping = [
156
            'in'              => 'PRC_ID',
157
            'idIn'            => 'PRC_ID',
158
            'ticket'          => 'Ticket.TKT_ID',
159
            'ticketIn'        => 'Ticket.TKT_ID',
160
            'ticketIdIn'      => 'Ticket.TKT_ID',
161
            'ticketId'        => 'Ticket.TKT_ID', // priority.
162
            'priceType'       => 'Price_Type.PRT_ID',
163
            'priceTypeIn'     => 'Price_Type.PRT_ID',
164
            'priceTypeIdIn'   => 'Price_Type.PRT_ID',
165
            'priceTypeId'     => 'Price_Type.PRT_ID', // priority.
166
            'priceBaseType'   => 'Price_Type.PBT_ID',
167
            'priceBaseTypeIn' => 'Price_Type.PBT_ID',
168
        ];
169
        return $this->sanitizeWhereArgsForInputFields(
170
            $where_args,
171
            $arg_mapping,
172
            ['in', 'ticket', 'ticketIn', 'priceType', 'priceTypeIn']
173
        );
174
    }
175
}
176