FormShield   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addPolicy() 0 3 1
A approvesRequest() 0 3 1
A analyzeRequest() 0 14 3
A __construct() 0 4 2
1
<?php
2
3
namespace WebTheory\Saveyour\Auth;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use WebTheory\HttpPolicy\ServerRequestPolicyInterface;
7
use WebTheory\Saveyour\Contracts\Auth\FormShieldInterface;
8
use WebTheory\Saveyour\Contracts\Report\FormShieldReportInterface;
9
use WebTheory\Saveyour\Report\Builder\FormShieldReportBuilder;
10
11
class FormShield implements FormShieldInterface
12
{
13
    /**
14
     * @var array<string,ServerRequestPolicyInterface>
15
     */
16
    protected array $policies = [];
17
18
    /**
19
     * @param array<string,ServerRequestPolicyInterface> $policies
20
     */
21 12
    public function __construct(array $policies)
22
    {
23 12
        foreach ($policies as $name => $policy) {
24 12
            $this->addPolicy($name, $policy);
25
        }
26
    }
27
28 12
    protected function addPolicy(string $name, ServerRequestPolicyInterface $policy)
29
    {
30 12
        $this->policies[$name] = $policy;
31
    }
32
33 6
    public function approvesRequest(ServerRequestInterface $request): bool
34
    {
35 6
        return $this->analyzeRequest($request)->verificationStatus();
36
    }
37
38 12
    public function analyzeRequest(ServerRequestInterface $request): FormShieldReportInterface
39
    {
40 12
        $builder = new FormShieldReportBuilder();
41
42 12
        foreach ($this->policies as $name => $policy) {
43 12
            if (!$policy->approvesRequest($request)) {
44 6
                $status = false;
45 6
                $builder->withRuleViolation($name);
46
            }
47
        }
48
49 12
        $builder->withVerificationStatus($status ?? true);
50
51 12
        return $builder->build();
52
    }
53
}
54