Passed
Push — develop ( deca8d...2e46de )
by Paul
13:16
created

Honeypot::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Helpers\Cast;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use GeminiLabs\SiteReviews\Modules\Html\Field;
8
9
class Honeypot
10
{
11 1
    public function build(string $formId): string
12
    {
13 1
        $field = new Field([
14 1
            'class' => 'glsr-input glsr-input-text',
15 1
            'label' => esc_html__('Your review', 'site-reviews'),
16 1
            'name' => $this->hash($formId),
17 1
            'type' => 'text',
18 1
        ]);
19 1
        $field->id = "{$field->id}-{$formId}";
20 1
        return $field->builder()->div([
21 1
            'class' => glsr(Style::class)->classes('field'),
22 1
            'style' => 'display:none;',
23 1
            'text' => $field->buildFieldLabel().$field->buildFieldElement(),
24 1
        ]);
25
    }
26
27 18
    public function hash(string $formId): string
28
    {
29 18
        return Str::hash($formId, 8);
30
    }
31
32
    public function verify(string $hash, string $formId): bool
33
    {
34
        return hash_equals($this->hash($formId), $hash);
35
    }
36
}
37