Passed
Pull Request — 5.x (#26)
by Enjoys
04:01 queued 01:55
created

TokenSubmit::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 8
c 2
b 1
f 0
dl 0
loc 15
ccs 6
cts 7
cp 0.8571
rs 10
cc 1
nc 1
nop 0
crap 1.0028
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