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

FailedValidationSimpleCache   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 41
ccs 0
cts 12
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A process() 0 15 3
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