Completed
Branch EDTR/master (bb716e)
by
unknown
57:55 queued 49:04
created

sanitizeWhereArgsForInputFields()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 32
nop 3
dl 0
loc 35
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\connection_resolvers;
4
5
use WPGraphQL\Data\Connection\AbstractConnectionResolver as WPGraphQLConnectionResolver;
6
use GraphQLRelay\Relay;
7
8
/**
9
 * Class AbstractConnectionResolver
10
 * Shared logic for ConnectionResolvers
11
 *
12
 * @package EventEspresso\core\services\graphql\connection_resolvers
13
 * @author  Manzoor Ahmad Wani
14
 * @since   $VID:$
15
 */
16
abstract class AbstractConnectionResolver extends WPGraphQLConnectionResolver
17
{
18
19
    /**
20
     * Set limit the highest value of first and last, with a (filterable) max of 100
21
     *
22
     * @return array
23
     */
24
    protected function getLimit()
25
    {
26
        $this->args['first'] = ! empty($this->args['first']) ? absint($this->args['first']) : 0;
27
        $this->args['last'] = ! empty($this->args['last']) ? absint($this->args['last']) : 0;
28
29
        $limit = min(
30
            max($this->args['first'], $this->args['last'], 10),
31
            $this->query_amount
32
        );
33
        $limit++;
34
        return $limit;
35
    }
36
37
38
    /**
39
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
40
     * friendly keys.
41
     *
42
     * @param array  $query_args
43
     * @param array  $where_params
44
     * @param string $primary_key
45
     * @return array
46
     */
47
    protected function mapOrderbyInputArgs(array $query_args, array $where_params, $primary_key)
48
    {
49
        // ID of the current offset
50
        $offset = $this->get_offset();
51
        /**
52
         * Map the orderby inputArgs to the WP_Query
53
         */
54
        if (! empty($this->args['where']['orderby']) && is_array($this->args['where']['orderby'])) {
55
            $query_args['order_by'] = [];
56
            foreach ($this->args['where']['orderby'] as $orderby_input) {
57
                $query_args['order_by'][ $orderby_input['field'] ] = $orderby_input['order'];
58
            }
59
        } elseif ($offset) {
60
            $compare = $this->args['last'] ? '<' : '>';
61
            $where_params[ $primary_key ] = [$compare, $offset];
62
        }
63
        return [$query_args, $where_params];
64
    }
65
66
67
    /**
68
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model
69
     * friendly keys.
70
     *
71
     * @param array $where_args
72
     * @param array $arg_mapping
73
     * @param array $id_fields   The fields to convert from global IDs to DB IDs.
74
     * @return array
75
     */
76
    protected function sanitizeWhereArgsForInputFields(
77
        array $where_args,
78
        array $arg_mapping,
79
        array $id_fields
80
    ) {
81
        $query_args = [];
82
83
        foreach ($where_args as $arg => $value) {
84
            if (! array_key_exists($arg, $arg_mapping)) {
85
                continue;
86
            }
87
88
            if (is_array($value) && ! empty($value)) {
89
                $value = array_map(
90
                    static function ($value) {
91
                        if (is_string($value)) {
92
                            $value = sanitize_text_field($value);
93
                        }
94
                        return $value;
95
                    },
96
                    $value
97
                );
98
            } elseif (is_string($value)) {
99
                $value = sanitize_text_field($value);
100
            }
101
            $query_args[ $arg_mapping[ $arg ] ] = in_array($arg, $id_fields)
102
            ? $this->convertGlobalId($value)
103
            : $value;
104
        }
105
106
        /**
107
         * Return the Query Args
108
         */
109
        return ! empty($query_args) && is_array($query_args) ? $query_args : [];
110
    }
111
112
113
    /**
114
     * Converts global ID to DB ID.
115
     *
116
     * @param string|string[] $ID
117
     * @return mixed
118
     */
119
    protected function convertGlobalId($ID)
120
    {
121
        if (is_array($ID)) {
122
            return array_map([$this, 'convertGlobalId'], $ID);
123
        }
124
        $parts = Relay::fromGlobalId($ID);
125
        return ! empty($parts['id']) ? $parts['id'] : null;
126
    }
127
}
128