Passed
Push — 5.x ( 65a76f...3a1e20 )
by Enjoys
59s queued 13s
created

Submit   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 37
ccs 13
cts 14
cp 0.9286
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getToken() 0 3 1
A check() 0 3 1
A validate() 0 14 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Interfaces\Ruleable;
9
use Enjoys\Forms\Rules;
10
11
class Submit extends Rules implements RuleInterface
12
{
13 79
    public function __construct(string $token)
14
    {
15 79
        parent::__construct(null, $token);
16
    }
17
18 79
    private function getToken()
19
    {
20 79
        return $this->getParams()[0];
21
    }
22
23
    /**
24
     * @psalm-suppress PossiblyNullReference
25
     * @param Ruleable&Element $element
26
     * @return bool
27
     */
28 79
    public function validate(Ruleable $element): bool
29
    {
30
31 79
        $method = $this->getRequest()->getRequest()->getMethod();
32
33 79
        $requestData = match (strtolower($method)) {
34 76
            'get' => $this->getRequest()->getQueryData()->toArray(),
35 3
            'post' => $this->getRequest()->getPostData()->toArray(),
36
            default => []
37
        };
38
39 79
        $value = \getValueByIndexPath($element->getName(), $requestData);
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Enjoys\Forms\Interfaces\Ruleable. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Interfaces\Ruleable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $requestData);
Loading history...
40
41 79
        return $this->check($value);
42
    }
43
44
45 79
    private function check($value): bool
46
    {
47 79
        return $value == $this->getToken();
48
    }
49
}
50