Test Failed
Push — tmp ( 15f615...89cc97 )
by Paul
10:31 queued 04:40
created

QueryBuilder::buildQuery()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 17
ccs 0
cts 17
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Modules\Multilingual;
10
use GeminiLabs\SiteReviews\Modules\Rating;
11
use WP_Query;
12
13
class QueryBuilder
14
{
15
    /**
16
     * Build a WP_Query meta_query/tax_query.
17
     * @return array
18
     */
19
    public function buildQuery(array $keys = [], array $values = [])
20
    {
21
        $queries = [];
22
        foreach ($keys as $key) {
23
            if (!array_key_exists($key, $values)) {
24
                continue;
25
            }
26
            $methodName = Helper::buildMethodName($key, __FUNCTION__);
27
            if (!method_exists($this, $methodName)) {
28
                continue;
29
            }
30
            $query = call_user_func([$this, $methodName], $values[$key]);
31
            if (is_array($query)) {
32
                $queries[] = $query;
33
            }
34
        }
35
        return $queries;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function buildSqlLines(array $values, array $conditions)
42
    {
43
        $string = '';
44
        $values = array_filter($values);
45
        foreach ($conditions as $key => $value) {
46
            if (!isset($values[$key])) {
47
                continue;
48
            }
49
            $values[$key] = implode(',', (array) $values[$key]);
50
            $string.= Str::contains($value, '%s')
51
                ? sprintf($value, strval($values[$key]))
52
                : $value;
53
        }
54
        return $string;
55
    }
56
57
    /**
58
     * Build a SQL 'OR' string from an array.
59
     * @param string|array $values
60
     * @param string $sprintfFormat
61
     * @return string
62
     */
63
    public function buildSqlOr($values, $sprintfFormat)
64
    {
65
        if (!is_array($values)) {
66
            $values = explode(',', $values);
67
        }
68
        $values = array_filter(array_map('trim', (array) $values));
69
        $values = array_map(function ($value) use ($sprintfFormat) {
70
            return sprintf($sprintfFormat, $value);
71
        }, $values);
72
        return implode(' OR ', $values);
73
    }
74
75
    /**
76
     * Get the current page number from the global query.
77
     * @param bool $isEnabled
78
     * @return int
79
     */
80
    public function getPaged($isEnabled = true)
81
    {
82
        return $isEnabled
83
            ? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR'))))
84
            : 1;
85
    }
86
87
    /**
88
     * @param string $value
89
     * @return void|array
90
     */
91
    protected function buildQueryAssignedTo($value)
92
    {
93
        if (!empty($value)) {
94
            $postIds = Arr::convertFromString($value, 'is_numeric');
95
            return [
96
                'compare' => 'IN',
97
                'key' => '_assigned_to',
98
                'value' => glsr(Multilingual::class)->getPostIds($postIds),
99
            ];
100
        }
101
    }
102
103
    /**
104
     * @param array $value
105
     * @return void|array
106
     */
107
    protected function buildQueryCategory($value)
108
    {
109
        if (!empty($value)) {
110
            return [
111
                'field' => 'term_id',
112
                'taxonomy' => Application::TAXONOMY,
113
                'terms' => $value,
114
            ];
115
        }
116
    }
117
118
    /**
119
     * @param string $value
120
     * @return void|array
121
     */
122
    protected function buildQueryEmail($value)
123
    {
124
        if (!empty($value)) {
125
            return [
126
                'key' => '_email',
127
                'value' => $value,
128
            ];
129
        }
130
    }
131
132
    /**
133
     * @param string $value
134
     * @return void|array
135
     */
136
    protected function buildQueryIpAddress($value)
137
    {
138
        if (!empty($value)) {
139
            return [
140
                'key' => '_ip_address',
141
                'value' => $value,
142
            ];
143
        }
144
    }
145
146
    /**
147
     * @param string $value
148
     * @return void|array
149
     */
150
    protected function buildQueryRating($value)
151
    {
152
        if (is_numeric($value)
153
            && in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) {
154
            return [
155
                'compare' => '>=',
156
                'key' => '_rating',
157
                'value' => $value,
158
            ];
159
        }
160
    }
161
162
    /**
163
     * @param string $value
164
     * @return void|array
165
     */
166
    protected function buildQueryType($value)
167
    {
168
        if (!in_array($value, ['', 'all'])) {
169
            return [
170
                'key' => '_review_type',
171
                'value' => $value,
172
            ];
173
        }
174
    }
175
}
176