Passed
Push — master ( 4f783c...449b2c )
by Paul
07:14
created

Helper::runClosure()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 4
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\Cache;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Helpers\Url;
9
use GeminiLabs\Vectorface\Whip\Whip;
10
11
class Helper
12
{
13
    /**
14
     * @param array|string $name
15
     * @param string $path
16
     * @return string
17
     */
18 8
    public static function buildClassName($name, $path = '')
19
    {
20 8
        if (is_array($name)) {
21 7
            $name = implode('-', $name);
22
        }
23 8
        $className = Str::camelCase($name);
24 8
        $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\');
25 8
        return !empty($path)
26 8
            ? __NAMESPACE__.'\\'.$path.'\\'.$className
27 8
            : $className;
28
    }
29
30
    /**
31
     * @param string $name
32
     * @param string $prefix
33
     * @return string
34
     */
35 43
    public static function buildMethodName($name, $prefix = '')
36
    {
37 43
        return lcfirst(Str::camelCase($prefix.'-'.$name));
38
    }
39
40
    /**
41
     * @param string $name
42
     * @return string
43
     */
44 1
    public static function buildPropertyName($name)
45
    {
46 1
        return static::buildMethodName($name);
47
    }
48
49
    /**
50
     * @param int|string $version1
51
     * @param int|string $version2
52
     * @param string $operator
53
     * @return bool
54
     */
55 19
    public static function compareVersions($version1, $version2, $operator = '=')
56
    {
57 19
        $version1 = implode('.', array_pad(explode('.', $version1), 3, 0));
58 19
        $version2 = implode('.', array_pad(explode('.', $version2), 3, 0));
59 19
        return version_compare($version1, $version2, $operator);
60
    }
61
62
    /**
63
     * @param string $key
64
     * @return mixed
65
     */
66 8
    public static function filterInput($key, array $request = [])
67
    {
68 8
        if (isset($request[$key])) {
69 7
            return $request[$key];
70
        }
71 8
        $variable = filter_input(INPUT_POST, $key);
72 8
        if (is_null($variable) && isset($_POST[$key])) {
73 8
            $variable = $_POST[$key];
74
        }
75 8
        return $variable;
76
    }
77
78
    /**
79
     * @param string $key
80
     * @return array
81
     */
82 8
    public static function filterInputArray($key)
83
    {
84 8
        $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
85 8
        if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) {
86 8
            $variable = $_POST[$key];
87
        }
88 8
        return Cast::toArray($variable);
89
    }
90
91
    /**
92
     * @return string
93
     */
94 16
    public static function getIpAddress()
95
    {
96 16
        $whitelist = [];
97 16
        $isUsingCloudflare = !empty(filter_input(INPUT_SERVER, 'CF-Connecting-IP'));
98 16
        if (glsr()->filterBool('whip/whitelist/cloudflare', $isUsingCloudflare)) {
99
            $cloudflareIps = glsr(Cache::class)->getCloudflareIps();
100
            $whitelist[Whip::CLOUDFLARE_HEADERS] = [Whip::IPV4 => $cloudflareIps['v4']];
101
            if (defined('AF_INET6')) {
102
                $whitelist[Whip::CLOUDFLARE_HEADERS][Whip::IPV6] = $cloudflareIps['v6'];
103
            }
104
        }
105 16
        $whitelist = glsr()->filterArray('whip/whitelist', $whitelist);
106 16
        $methods = glsr()->filterInt('whip/methods', Whip::ALL_METHODS);
107 16
        $whip = new Whip($methods, $whitelist);
108 16
        glsr()->action('whip', $whip);
109 16
        if (false !== ($clientAddress = $whip->getValidIpAddress())) {
110 16
            return (string) $clientAddress;
111
        }
112
        glsr_log()->error('Unable to detect IP address, please see the FAQ page for a possible solution.');
113
        return 'unknown';
114
    }
115
116
    /**
117
     * @param string $fromUrl
118
     * @param int $fallback
119
     * @return int
120
     */
121 2
    public static function getPageNumber($fromUrl = null, $fallback = 1)
122
    {
123 2
        $pagedQueryVar = glsr()->constant('PAGED_QUERY_VAR');
124 2
        $pageNum = empty($fromUrl)
125 2
            ? filter_input(INPUT_GET, $pagedQueryVar, FILTER_VALIDATE_INT)
126 2
            : filter_var(Url::query($fromUrl, $pagedQueryVar), FILTER_VALIDATE_INT);
127 2
        if (empty($pageNum)) {
128 2
            $pageNum = (int) $fallback;
129
        }
130 2
        return max(1, $pageNum);
131
    }
132
133
    /**
134
     * @param mixed $post
135
     * @return int
136
     */
137 15
    public static function getPostId($post)
138
    {
139 15
        if (is_numeric($post) || $post instanceof \WP_Post) {
140 15
            $post = get_post($post);
141
        }
142 15
        if ($post instanceof \WP_Post) {
143 15
            return $post->ID;
144
        }
145
        return 0;
146
    }
147
148
    /**
149
     * @param mixed $value
150
     * @param mixed $fallback
151
     * @return mixed
152
     */
153 47
    public static function ifEmpty($value, $fallback, $strict = false)
154
    {
155 47
        $isEmpty = $strict ? empty($value) : static::isEmpty($value);
156 47
        return $isEmpty ? $fallback : $value;
157
    }
158
159
    /**
160
     * @param bool $condition
161
     * @param mixed $ifTrue
162
     * @param mixed $ifFalse
163
     * @return mixed
164
     */
165 33
    public static function ifTrue($condition, $ifTrue, $ifFalse = null)
166
    {
167 33
        return $condition ? static::runClosure($ifTrue) : static::runClosure($ifFalse);
168
    }
169
170
    /**
171
     * @param mixed $value
172
     * @param string|int $min
173
     * @param string|int $max
174
     * @return bool
175
     */
176 6
    public static function inRange($value, $min, $max)
177
    {
178 6
        $inRange = filter_var($value, FILTER_VALIDATE_INT, ['options' => [
179 6
            'min_range' => intval($min),
180 6
            'max_range' => intval($max),
181
        ]]);
182 6
        return false !== $inRange;
183
    }
184
185
    /**
186
     * @param mixed $value
187
     * @return bool
188
     */
189 53
    public static function isEmpty($value)
190
    {
191 53
        if (is_string($value)) {
192 52
            return trim($value) === '';
193
        }
194 53
        return !is_numeric($value) && !is_bool($value) && empty($value);
195
    }
196
197
    /**
198
     * @param int|string $value
199
     * @param int|string $compareWithValue
200
     * @return bool
201
     */
202 15
    public static function isGreaterThan($value, $compareWithValue)
203
    {
204 15
        return static::compareVersions($value, $compareWithValue, '>');
205
    }
206
207
    /**
208
     * @param int|string $value
209
     * @param int|string $compareWithValue
210
     * @return bool
211
     */
212 1
    public static function isGreaterThanOrEqual($value, $compareWithValue)
213
    {
214 1
        return static::compareVersions($value, $compareWithValue, '>=');
215
    }
216
217
    /**
218
     * @param int|string $value
219
     * @param int|string $compareWithValue
220
     * @return bool
221
     */
222 1
    public static function isLessThan($value, $compareWithValue)
223
    {
224 1
        return static::compareVersions($value, $compareWithValue, '<');
225
    }
226
227
    /**
228
     * @param int|string $value
229
     * @param int|string $compareWithValue
230
     * @return bool
231
     */
232 1
    public static function isLessThanOrEqual($value, $compareWithValue)
233
    {
234 1
        return static::compareVersions($value, $compareWithValue, '<=');
235
    }
236
237
    /**
238
     * @param mixed $value
239
     * @return bool
240
     */
241 43
    public static function isNotEmpty($value)
242
    {
243 43
        return !static::isEmpty($value);
244
    }
245
246
    /**
247
     * @param string $url
248
     * @param int $maxRedirects
249
     * @return int|false
250
     */
251
    public static function remoteStatusCheck($url, $maxRedirects = 0)
252
    {
253
        $headers = get_headers($url, 0, stream_context_create([
254
            'http' => [
255
                'ignore_errors' => 1,
256
                'max_redirects' => $maxRedirects,
257
                'method' => 'HEAD',
258
            ],
259
        ]));
260
        if (false !== $headers) {
261
            return Cast::toInt(substr($headers[0], 9, 3));
262
        }
263
        return false;
264
    }
265
266
    /**
267
     * @param mixed $value
268
     * @return mixed
269
     */
270 33
    public static function runClosure($value)
271
    {
272 33
        if ($value instanceof \Closure || (is_array($value) && is_callable($value))) {
273 31
            return call_user_func($value);
274
        }
275 28
        return $value;
276
    }
277
}
278