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

TicketConnectionResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 2.17 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
dl 3
loc 138
rs 10
c 0
b 0
f 0
wmc 13
lcom 3
cbo 3

6 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
A should_execute() 0 8 2
B get_query_args() 3 46 6
A sanitizeInputFields() 0 14 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_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 DatetimeConnectionResolver
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
     * 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 (false === $this->should_execute) {
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 = ['TKT_deleted' => ['IN', [true, false]]];
87
        $query_args   = [];
88
89
        $query_args['limit'] = $this->getLimit();
90
91
        // Avoid multiple entries by join.
92
        $query_args['group_by'] = 'TKT_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['Datetime.DTT_ID']) && is_array($input_fields['Datetime.DTT_ID'])) {
103
                $input_fields['Datetime.DTT_ID'] = ['in', $input_fields['Datetime.DTT_ID']];
104
            }
105
        }
106
107
        /**
108
         * Determine where we're at in the Graph and adjust the query context appropriately.
109
         */
110
        if ($this->source instanceof EE_Datetime) {
111
            $where_params['Datetime.DTT_ID'] = $this->source->ID();
112
        }
113
114
        /**
115
         * Merge the input_fields with the default query_args
116
         */
117
        if (! empty($input_fields)) {
118
            $where_params = array_merge($where_params, $input_fields);
119
        }
120
121
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'TKT_ID');
122
123
        $query_args[] = $where_params;
124
125
        /**
126
         * Return the $query_args
127
         */
128
        return $query_args;
129
    }
130
131
132
    /**
133
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
134
     * friendly keys.
135
     *
136
     * @param array $where_args
137
     * @return array
138
     */
139
    public function sanitizeInputFields(array $where_args)
140
    {
141
        $arg_mapping = [
142
            'datetime'     => 'Datetime.DTT_ID',
143
            'datetimeIn'   => 'Datetime.DTT_ID',
144
            'datetimeIdIn' => 'Datetime.DTT_ID',
145
            'datetimeId'   => 'Datetime.DTT_ID', // priority.
146
        ];
147
        return $this->sanitizeWhereArgsForInputFields(
148
            $where_args,
149
            $arg_mapping,
150
            ['datetime', 'datetimeIn']
151
        );
152
    }
153
}
154