FailedValidationSimpleCache::process()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
nc 6
nop 2
dl 0
loc 17
ccs 0
cts 7
cp 0
crap 20
rs 9.9666
c 1
b 0
f 1
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