Passed
Pull Request — 5.x (#27)
by Enjoys
21:33 queued 17:13
created

TokenSubmit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 4
eloc 11
c 4
b 2
f 0
dl 0
loc 32
ccs 17
cts 17
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 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 90
    }
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 90
            'GET' => $this->form->getRequest()->getQueryParams(),
31 90
            'POST' => $this->form->getRequest()->getParsedBody(),
32 90
            default => []
33 90
        };
34
        /** @var string $value */
35 90
        $value = \getValueByIndexPath(
36 90
            Form::_TOKEN_SUBMIT_,
37 90
            $requestData
38 90
        );
39
40 90
        return $value == $this->getToken();
41
    }
42
}
43