Test Failed
Push — main ( 4244ba...f56735 )
by Paul
10:05 queued 12s
created

Request::set()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.9332
cc 4
nc 3
nop 2
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Captcha;
9
use GeminiLabs\SiteReviews\Modules\Encryption;
10
11
class Request extends Arguments
12
{
13
    public function decrypt(string $key): string
14
    {
15 12
        $value = glsr(Encryption::class)->decrypt($this->cast($key, 'string'));
16
        return Cast::toString($value);
17 12
    }
18 12
19 12
    /**
20
     * @param mixed $key
21
     * @param mixed $fallback
22
     *
23
     * @return mixed
24
     */
25
    public function get($key, $fallback = null)
26
    {
27
        $value = Arr::get($this->getArrayCopy(), $key, null);
28 8
        if (is_null($fallback)) {
29
            return $value;
30 8
        }
31 8
        if (!Helper::isEmpty($value)) {
32
            return $value;
33
        }
34
        return Helper::runClosure($fallback);
35 8
    }
36
37
    /**
38
     * @return static
39
     *
40
     * @todo support array values
41 8
     */
42
    public static function inputGet(): Request
43 8
    {
44 8
        $values = [];
45 8
        if ($token = filter_input(INPUT_GET, glsr()->prefix)) {
46
            $token = sanitize_text_field($token);
47 8
            $values = glsr(Encryption::class)->decryptRequest($token);
48 8
        }
49 8
        return new static($values);
50 8
    }
51 8
52
    /**
53 8
     * @return static
54
     */
55
    public static function inputPost(): Request
56
    {
57
        $action = Helper::filterInput('action');
58
        $values = Helper::filterInputArray(glsr()->id);
59
        if (in_array($action, [glsr()->prefix.'admin_action', glsr()->prefix.'public_action'])) {
60
            $values['_ajax_request'] = true;
61
        }
62
        $requestAction = Helper::filterInput('_action', $values);
63
        if (in_array($requestAction, glsr(Captcha::class)->actions())) {
64
            $values['_frcaptcha'] = Helper::filterInput('frc-captcha-solution');
65
            $values['_hcaptcha'] = Helper::filterInput('h-captcha-response');
66
            $values['_procaptcha'] = Helper::filterInput('procaptcha-response');
67
            $values['_recaptcha'] = Helper::filterInput('g-recaptcha-response');
68
            $values['_turnstile'] = Helper::filterInput('cf-turnstile-response');
69
        }
70
        return new static($values);
71
    }
72
73
    /**
74
     * @param mixed $value
75
     */
76
    public function set(string $path, $value): void
77
    {
78
        $storage = Arr::set($this->getArrayCopy(), $path, $value);
79
        $this->exchangeArray($storage);
80
        if (!$this->exists('form_signature') || 'form_signature' === $path) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on GeminiLabs\SiteReviews\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        if (!$this->/** @scrutinizer ignore-call */ exists('form_signature') || 'form_signature' === $path) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            return;
82
        }
83
        $values = $this->decrypt('form_signature');
84
        $values = wp_parse_args(maybe_unserialize($values));
85
        if (array_key_exists($path, $values)) {
86
            $values[$path] = $value;
87
            $storage['form_signature'] = glsr(Encryption::class)->encrypt(maybe_serialize($values));
88
            $this->exchangeArray($storage);
89
        }
90
    }
91
}
92