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

DatetimeConnectionResolver   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 168
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 18
loc 168
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_loader_name() 0 4 1
A get_query() 0 4 1
A get_ids() 0 6 2
F get_query_args() 18 94 15
A sanitizeInputFields() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use EE_Error;
6
use EEM_Datetime;
7
use EE_Event;
8
use EE_Ticket;
9
use EE_Checkin;
10
use EventEspresso\core\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidInterfaceException;
12
use InvalidArgumentException;
13
use WPGraphQL\Model\Post;
14
15
/**
16
 * Class DatetimeConnectionResolver
17
 *
18
 */
19
class DatetimeConnectionResolver extends AbstractConnectionResolver
20
{
21
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
22
    public function get_loader_name()
23
    {
24
        return 'espresso_datetime';
25
    }
26
27
    /**
28
     * @return EEM_Datetime
29
     * @throws EE_Error
30
     * @throws InvalidArgumentException
31
     * @throws InvalidDataTypeException
32
     * @throws InvalidInterfaceException
33
     */
34
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
35
    public function get_query()
36
    {
37
        return EEM_Datetime::instance();
38
    }
39
40
    /**
41
     * Return an array of item IDs from the query
42
     *
43
     * @return array
44
     */
45
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
46
    public function get_ids()
47
    {
48
        $results = $this->query->get_col($this->query_args);
49
50
        return ! empty($results) ? $results : [];
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 InvalidDataTypeException
62
     * @throws InvalidInterfaceException
63
     */
64
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
65
    public function get_query_args()
66
    {
67
        $where_params = ['DTT_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'] = 'DTT_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['EVT_ID']) && is_array($input_fields['EVT_ID'])) {
86
                $input_fields['EVT_ID'] = ['in', $input_fields['EVT_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
        }
92
93
        /**
94
         * Determine where we're at in the Graph and adjust the query context appropriately.
95
         *
96
         * For example, if we're querying for datetime as a field of event query, this will automatically
97
         * set the query to pull datetimes that belong to that event.
98
         * We can set more cases for other source types.
99
         */
100
        if (is_object($this->source)) {
101
            switch (true) {
102
                // It's surely an event
103
                case $this->source instanceof Post:
104
                    $where_params['EVT_ID'] = $this->source->ID;
105
                    break;
106
                case $this->source instanceof EE_Event:
107
                    $where_params['EVT_ID'] = $this->source->ID();
108
                    break;
109
                case $this->source instanceof EE_Ticket:
110
                    $where_params['Ticket.TKT_ID'] = $this->source->ID();
111
                    break;
112
                case $this->source instanceof EE_Checkin:
113
                    $where_params['Checkin.CHK_ID'] = $this->source->ID();
114
                    break;
115
            }
116
        }
117
118
        /**
119
         * Merge the input_fields with the default query_args
120
         */
121
        if (! empty($input_fields)) {
122
            $where_params = array_merge($where_params, $input_fields);
123
        }
124
125
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'DTT_ID');
126
127 View Code Duplication
        if (! empty($this->args['where']['upcoming'])) {
128
            $where_params['DTT_EVT_start'] = array(
129
                '>',
130
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start')
131
            );
132
        }
133
134
        if (! empty($this->args['where']['active'])) {
135
            $where_params['DTT_EVT_start'] = array(
136
                '<',
137
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start')
138
            );
139
            $where_params['DTT_EVT_end'] = array(
140
                '>',
141
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')
142
            );
143
        }
144
145 View Code Duplication
        if (! empty($this->args['where']['expired'])) {
146
            $where_params['DTT_EVT_end'] = array(
147
                '<',
148
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')
149
            );
150
        }
151
152
        $query_args[] = $where_params;
153
154
        /**
155
         * Return the $query_args
156
         */
157
        return $query_args;
158
    }
159
160
161
    /**
162
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
163
     * friendly keys.
164
     *
165
     * @param array $where_args
166
     * @return array
167
     */
168
    public function sanitizeInputFields(array $where_args)
169
    {
170
        $arg_mapping = [
171
            'event'      => 'EVT_ID',
172
            'eventIn'    => 'EVT_ID',
173
            'eventId'    => 'EVT_ID',
174
            'eventIdIn'  => 'EVT_ID',
175
            'ticket'     => 'Ticket.TKT_ID',
176
            'ticketIn'   => 'Ticket.TKT_ID',
177
            'ticketId'   => 'Ticket.TKT_ID',
178
            'ticketIdIn' => 'Ticket.TKT_ID',
179
        ];
180
        return $this->sanitizeWhereArgsForInputFields(
181
            $where_args,
182
            $arg_mapping,
183
            ['event', 'eventIn', 'ticket', 'ticketIn']
184
        );
185
    }
186
}
187