FailedValidationSimpleCache   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
dl 0
loc 43
ccs 0
cts 12
cp 0
rs 10
c 1
b 0
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A process() 0 17 4
1
<?php
2
3
namespace WebTheory\Saveyour\Processor;
4
5
use DateInterval;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\SimpleCache\CacheInterface;
8
use WebTheory\Saveyour\Contracts\Processor\FormDataProcessorInterface;
9
use WebTheory\Saveyour\Contracts\Report\FormProcessReportInterface;
10
use WebTheory\Saveyour\Processor\Abstracts\AbstractFormDataProcessor;
11
use WebTheory\Saveyour\Report\FormProcessReport;
12
13
class FailedValidationSimpleCache extends AbstractFormDataProcessor implements FormDataProcessorInterface
14
{
15
    protected CacheInterface $cache;
16
17
    protected string $key;
18
19
    /**
20
     * @var null|int|DateInterval
21
     */
22
    protected $ttl;
23
24
    /**
25
     * @param string $name
26
     * @param CacheInterface $cache
27
     * @param string $key
28
     * @param null|int|DateInterval $ttl
29
     */
30
    public function __construct(string $name, CacheInterface $cache, string $key, $ttl = null)
31
    {
32
        parent::__construct($name, null);
33
34
        $this->cache = $cache;
35
        $this->key = $key;
36
        $this->ttl = $ttl;
37
    }
38
39
    public function process(ServerRequestInterface $request, array $results): ?FormProcessReportInterface
40
    {
41
        $violations = [];
42
43
        foreach ($results as $field => $result) {
44
            if (false === $result->validationStatus()) {
45
                $violations[$field] = $result->ruleViolations();
46
            }
47
        }
48
49
        if ($violations) {
50
            $status = $this->cache->set($this->key, $violations, $this->ttl);
51
        }
52
53
        return new FormProcessReport([
54
            'successful' => $status ?? null,
55
            'violations' => $violations,
56
        ]);
57
    }
58
}
59