Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

TokenSubmit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
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 30
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 13 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 87
    public function __construct(private Form $form)
12
    {
13
    }
14
15 87
    private function getToken(): string
16
    {
17 87
        return md5(json_encode($this->form->getOptions()));
18
    }
19
20 87
    public function getElement(): Hidden
21
    {
22 87
        return new Hidden(Form::_TOKEN_SUBMIT_, $this->getToken());
23
    }
24
25
26 87
    public function validate(): bool
27
    {
28
        /** @var string $value */
29 87
        $value = \getValueByIndexPath(
30
            Form::_TOKEN_SUBMIT_,
31 87
            match ($this->form->getMethod()) {
32 14
                'GET' => $this->form->getRequest()->getQueryData()->toArray(),
33 78
                'POST' => $this->form->getRequest()->getPostData()->toArray(),
34 87
                default => []
35
            }
36
        );
37
38 87
        return $value == $this->getToken();
39
    }
40
}
41