Completed
Branch EDTR/master (f9130a)
by
unknown
46:59 queued 38:09
created

AbstractConnectionResolver   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 90
c 0
b 0
f 0
rs 10
wmc 18
lcom 0
cbo 0

3 Methods

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