Completed
Pull Request — master (#23)
by
unknown
01:21
created

UpdateMaskedRequestParameters::maskQueryString()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
cc 6
nc 8
nop 2
1
<?php
2
3
namespace Facade\Ignition\Middleware;
4
5
use Facade\FlareClient\Report;
6
7
class UpdateMaskedRequestParameters
8
{
9
    public function handle(Report $report, $next)
10
    {
11
        if ($fields_to_mask = $this->getFieldsToMask()) {
12
            $context = $report->allContext();
13
14
            $this->maskProperties($fields_to_mask, $context);
15
16
            $this->maskQueryString($fields_to_mask, $context['request']['url']);
17
18
            $report->userProvidedContext($context);
19
        }
20
21
        return $next($report);
22
    }
23
24
    public function getFieldsToMask(): array
25
    {
26
        return config('ignition.masked_request_parameters');
27
    }
28
29
    public function maskProperties(array $fields_to_mask, array &$context, &$changed = null)
30
    {
31
        $fields_to_mask = array_combine($fields_to_mask, $fields_to_mask);
32
33
        foreach ($context as $key => &$value) {
34
            if (is_iterable($value)) {
35
                $this->maskProperties($fields_to_mask, $value, $changed);
36
37
                continue;
38
            }
39
40
            if (isset($fields_to_mask[$key])) {
41
                $this->maskProperty($value, $changed);
42
            }
43
        }
44
    }
45
46
    public function maskProperty(&$value, &$changed = null)
47
    {
48
        if (is_iterable($value)) {
49
            foreach ($value as &$_value) {
50
                $this->maskProperty($_value, $changed);
51
            }
52
53
            return;
54
        }
55
56
        $initial_value = $value;
57
58
        switch (gettype($this->cast($value))) {
59
            case 'boolean':
60
                $value = $this->randomiseValueFromArray([true, false]);
61
62
                break;
63
64
            case 'integer':
65
                $value = $this->randomInteger(strlen($value));
66
67
                break;
68
69
            case 'double':
70
                $value = $this->randomFloat(strlen($value));
71
72
                break;
73
74
            case 'string':
75
                if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
76
                    if (class_exists(\Faker\Factory::class)) {
77
                        $value = (\Faker\Factory::create())->safeEmail;
78
79
                        break;
80
                    }
81
82
                    $value = strtolower($this->randomString(30)).'@example.com';
83
84
                    break;
85
                }
86
87
                $value = $this->randomString(strlen($value));
88
89
                break;
90
91
            case 'NULL':
92
            case 'unknown type':
93
                $value = null;
94
95
                break;
96
97
        }
98
99
        if ($changed !== null && $initial_value !== $value) {
100
            $changed = true;
101
        }
102
    }
103
104
    public function maskQueryString(array $fields_to_mask, string &$url)
105
    {
106
        $url_parts = parse_url($url);
107
        if (isset($url_parts['query'])) {
108
            parse_str($url_parts['query'], $query_strings);
109
110
            if ($url_parts['query']) {
111
                $changes = false;
112
                foreach ($fields_to_mask as $field) {
113
                    if (isset($query_strings[$field])) {
114
                        $this->maskProperty($query_strings[$field], $changes);
115
                    }
116
                }
117
118
                if ($changes) {
119
                    $url = $url_parts['scheme'].'://'.$url_parts['host'].$url_parts['path'].'?'.http_build_query($query_strings);
120
                }
121
            }
122
        }
123
    }
124
125
    public function randomiseValueFromArray(array $possible_values)
126
    {
127
        return $possible_values[array_rand($possible_values)];
128
    }
129
130
    public function randomString($length): string
131
    {
132
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
133
134
        $randomString = '';
135
        $charactersLength = strlen($characters);
136
        for ($i = 0; $i < $length; $i++) {
137
            $randomString .= $characters[rand(0, $charactersLength - 1)];
138
        }
139
140
        return $randomString;
141
    }
142
143
    public function randomFloat($length): float
144
    {
145
        $decimal = rand(0, 100) / 100;
146
        $decimal_places = strlen($decimal) - 1;
147
148
        return (float) substr($this->randomInteger($length), 0, -$decimal_places).$decimal;
149
    }
150
151
    public function randomInteger($length): int
152
    {
153
        return rand(1, (int) str_pad('', $length, 9));
154
    }
155
156
    public function cast($value)
157
    {
158
        if (is_numeric($value)) {
159
            if ((int) $value == $value) {
160
                return (int) $value;
161
            }
162
163
            return (float) $value;
164
        }
165
166
        return $value;
167
    }
168
}
169