Passed
Push — master ( d855a0...34f20f )
by Enjoys
01:02 queued 12s
created

TokenSubmit::getToken()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms;
6
7
use Enjoys\Forms\Elements\Hidden;
8
9
class TokenSubmit
10
{
11 86
    public function __construct(private Form $form)
12
    {
13
    }
14
15 86
    private function getToken(): string
16
    {
17 86
        return md5(json_encode($this->form->getOptions()));
18
    }
19
20 86
    public function getElement(): Hidden
21
    {
22 86
        return new Hidden(Form::_TOKEN_SUBMIT_, $this->getToken());
23
    }
24
25 86
    public function validate(): bool
26
    {
27 86
        $value = \getValueByIndexPath(
28
            Form::_TOKEN_SUBMIT_,
29 86
            match ($this->form->getMethod()) {
30 14
                'GET' => $this->form->getRequest()->getQueryData()->toArray(),
31 77
                'POST' => $this->form->getRequest()->getPostData()->toArray(),
32 86
                default => []
33
            }
34
        );
35
36 86
        return $value == $this->getToken();
37
    }
38
}
39