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

TokenSubmit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
eloc 10
c 3
b 1
f 0
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getElement() 0 3 1
A __construct() 0 2 1
A getToken() 0 3 1
A validate() 0 12 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