Completed
Branch EDTR/master (dbc914)
by
unknown
09:15 queued 40s
created

AttendeeConnectionResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 5.97 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 134
rs 10
c 0
b 0
f 0
wmc 12
lcom 3
cbo 2

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() 8 41 5
A sanitizeInputFields() 0 15 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_Attendee;
7
use EventEspresso\core\exceptions\InvalidDataTypeException;
8
use EventEspresso\core\exceptions\InvalidInterfaceException;
9
use InvalidArgumentException;
10
use ReflectionException;
11
12
/**
13
 * Class DatetimeConnectionResolver
14
 */
15
class AttendeeConnectionResolver extends AbstractConnectionResolver
16
{
17
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
18
    public function get_loader_name()
19
    {
20
        return 'espresso_attendee';
21
    }
22
23
    /**
24
     * @return EEM_Attendee
25
     * @throws EE_Error
26
     * @throws InvalidArgumentException
27
     * @throws InvalidDataTypeException
28
     * @throws InvalidInterfaceException
29
     */
30
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
31
    public function get_query()
32
    {
33
        return EEM_Attendee::instance();
34
    }
35
36
37
    /**
38
     * Return an array of item IDs from the query
39
     *
40
     * @return array
41
     */
42
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
43
    public function get_ids()
44
    {
45
        $results = $this->query->get_col($this->query_args);
46
47
        return ! empty($results) ? $results : [];
48
    }
49
50
51
    /**
52
     * Determine whether the Query should execute. If it's determined that the query should
53
     * not be run based on context such as, but not limited to, who the user is, where in the
54
     * ResolveTree the Query is, the relation to the node the Query is connected to, etc
55
     * Return false to prevent the query from executing.
56
     *
57
     * @return bool
58
     */
59
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
60
    public function should_execute()
61
    {
62
        if (false === $this->should_execute) {
63
            return false;
64
        }
65
66
        return $this->should_execute;
67
    }
68
69
70
    /**
71
     * Here, we map the args from the input, then we make sure that we're only querying
72
     * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers
73
     * handle batch resolution of the posts.
74
     *
75
     * @return array
76
     * @throws EE_Error
77
     * @throws InvalidArgumentException
78
     * @throws ReflectionException
79
     * @throws InvalidDataTypeException
80
     * @throws InvalidInterfaceException
81
     */
82
    // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
83
    public function get_query_args()
84
    {
85
        $where_params = [];
86
        $query_args   = [];
87
88
        $query_args['limit'] = $this->getLimit();
89
90
        // Avoid multiple entries by join.
91
        $query_args['group_by'] = 'ATT_ID';
92
93
        $query_args['default_where_conditions'] = 'minimum';
94
95
        /**
96
         * Collect the input_fields and sanitize them to prepare them for sending to the Query
97
         */
98
        $input_fields = [];
99 View Code Duplication
        if (! empty($this->args['where'])) {
100
            $input_fields = $this->sanitizeInputFields($this->args['where']);
101
102
            // Use the proper operator.
103
            if (! empty($input_fields['Registration.Ticket.TKT_ID']) && is_array($input_fields['Registration.Ticket.TKT_ID'])) {
104
                $input_fields['Registration.Ticket.TKT_ID'] = ['in', $input_fields['Registration.Ticket.TKT_ID']];
105
            }
106
        }
107
108
        /**
109
         * Merge the input_fields with the default query_args
110
         */
111
        if (! empty($input_fields)) {
112
            $where_params = array_merge($where_params, $input_fields);
113
        }
114
115
        list($query_args, $where_params) = $this->mapOrderbyInputArgs($query_args, $where_params, 'ATT_ID');
116
117
        $query_args[] = $where_params;
118
119
        /**
120
         * Return the $query_args
121
         */
122
        return $query_args;
123
    }
124
125
126
    /**
127
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
128
     * friendly keys.
129
     *
130
     * @param array $where_args
131
     * @return array
132
     */
133
    public function sanitizeInputFields(array $where_args)
134
    {
135
        $arg_mapping = [
136
            'regTicket'     => 'Registration.Ticket.TKT_ID',
137
            'regTicketIn'   => 'Registration.Ticket.TKT_ID',
138
            'regTicketIdIn' => 'Registration.Ticket.TKT_ID',
139
            'regTicketId'   => 'Registration.Ticket.TKT_ID', // priority.
140
            'regStatus'     => 'Registration.Status.STS_ID',
141
        ];
142
        return $this->sanitizeWhereArgsForInputFields(
143
            $where_args,
144
            $arg_mapping,
145
            ['regTicket', 'regTicketIn']
146
        );
147
    }
148
}
149