Passed
Push — 5.x ( 80f665...fe0c03 )
by Enjoys
57s queued 13s
created

TokenSubmit::getToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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