Test Failed
Push — develop ( 006c9f...4b441a )
by Paul
13:29
created

SanitizeIdUnique   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 8
c 0
b 0
f 0
dl 0
loc 16
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 9 3
A value() 0 3 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Sanitizers;
4
5
use GeminiLabs\SiteReviews\Helpers\Str;
6
7
/**
8
 * This allows lowercase alphannumeric characters, dashes, and underscores.
9
 * A unique value is generated if result is empty.
10
 */
11
class SanitizeIdUnique extends StringSanitizer
12
{
13
    public function run(): string
14
    {
15
        $value = $this->value();
16
        if (empty($value)) {
17
            $value = sanitize_key(base64_encode(random_bytes(12)));
18
            $value = substr($value, 0, 8);
19
            $value = Str::prefix($value, $this->args[0] ?: glsr()->prefix);
20
        }
21
        return $value;
22
    }
23
24
    protected function value(): string
25
    {
26
        return (new SanitizeId($this->value))->run();
27
    }
28
}
29