Passed
Push — remove_deprecated_packages ( 999fd5 )
by Enjoys
15:50
created

TokenSubmit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 4
eloc 11
c 4
b 2
f 0
dl 0
loc 32
ccs 11
cts 12
cp 0.9167
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 15 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 90
    public function __construct(private Form $form)
12
    {
13
    }
14
15 90
    private function getToken(): string
16
    {
17 90
        return md5(json_encode($this->form->getOptions()));
18
    }
19
20 90
    public function getElement(): Hidden
21
    {
22 90
        return new Hidden(Form::_TOKEN_SUBMIT_, $this->getToken());
23
    }
24
25
26 90
    public function validate(): bool
27
    {
28
        /** @var array $requestData */
29 90
        $requestData = match ($this->form->getMethod()) {
30 17
            'GET' => $this->form->getRequest()->getQueryParams(),
31 78
            'POST' => $this->form->getRequest()->getParsedBody(),
32
            default => []
33
        };
34
        /** @var string $value */
35 90
        $value = \getValueByIndexPath(
36
            Form::_TOKEN_SUBMIT_,
37
            $requestData
38
        );
39
40 90
        return $value == $this->getToken();
41
    }
42
}
43