SimpleCacheDataManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace WebTheory\Saveyour\Data;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\SimpleCache\CacheInterface;
7
use WebTheory\Saveyour\Contracts\Data\FieldDataManagerInterface;
8
9
class SimpleCacheDataManager implements FieldDataManagerInterface
10
{
11
    protected CacheInterface $cache;
12
13
    protected string $key;
14
15
    protected $ttl;
16
17
    public function __construct(CacheInterface $cache, string $key, $ttl = null)
18
    {
19
        $this->cache = $cache;
20
        $this->key = $key;
21
        $this->ttl = $ttl;
22
    }
23
24
    public function getCurrentData(ServerRequestInterface $request)
25
    {
26
        $data = $this->cache->get($this->key);
27
28
        $this->cache->delete($this->key);
29
30
        return $data;
31
    }
32
33
    public function handleSubmittedData(ServerRequestInterface $request, $data): bool
34
    {
35
        $this->cache->set($this->key, $data, $this->ttl);
36
37
        return true;
38
    }
39
}
40