Passed
Push — master ( d2c2d4...c7e2fa )
by Chris
05:36
created

FailedValidationSimpleCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
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
        $status = $this->cache->set($this->key, $violations, $this->ttl);
50
51
        return new FormProcessReport([
52
            'successful' => $status,
53
            'violations' => $violations,
54
        ]);
55
    }
56
}
57