Passed
Push — master ( c5f4fa...dd2f97 )
by butschster
07:03
created

CacheHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Session\Handler;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Psr\SimpleCache\InvalidArgumentException;
9
use Spiral\Cache\CacheStorageProviderInterface;
10
11
final class CacheHandler implements \SessionHandlerInterface
12
{
13
    private readonly CacheInterface $cache;
14
15 7
    public function __construct(
16
        CacheStorageProviderInterface $storageProvider,
17
        private readonly ?string $storage = null,
18
        private readonly int $ttl = 86400,
19
        private readonly string $prefix = 'session:'
20
    ) {
21 7
        $this->cache = $storageProvider->storage($this->storage);
0 ignored issues
show
Bug introduced by
The property cache is declared read-only in Spiral\Session\Handler\CacheHandler.
Loading history...
22
    }
23
24 1
    public function close(): bool
25
    {
26 1
        return true;
27
    }
28
29
    /**
30
     * @throws InvalidArgumentException
31
     */
32 1
    public function destroy(string $id): bool
33
    {
34 1
        $this->cache->delete($this->getKey($id));
35
36 1
        return true;
37
    }
38
39 1
    public function gc(int $max_lifetime): int|false
40
    {
41 1
        return 0;
42
    }
43
44 1
    public function open(string $path, string $name): bool
45
    {
46 1
        return true;
47
    }
48
49
    /**
50
     * @throws InvalidArgumentException
51
     */
52 2
    public function read(string $id): string|false
53
    {
54 2
        $result = $this->cache->get($this->getKey($id));
55
56 2
        return (string) $result;
57
    }
58
59
    /**
60
     * @throws InvalidArgumentException
61
     */
62 1
    public function write(string $id, string $data): bool
63
    {
64 1
        return $this->cache->set($this->getKey($id), $data, $this->ttl);
65
    }
66
67 4
    private function getKey(string $id): string
68
    {
69 4
        return $this->prefix . $id;
70
    }
71
}
72