Redis::store()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage;
4
5
6
use kalanis\UploadPerPartes\Interfaces;
7
use kalanis\UploadPerPartes\Target\Local\DrivingFile;
8
use kalanis\UploadPerPartes\Traits\TLang;
9
use kalanis\UploadPerPartes\UploadException;
10
use Redis as lib;
11
12
13
/**
14
 * Class Redis
15
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage
16
 * Processing info file in Redis
17
 * @codeCoverageIgnore
18
 */
19
class Redis implements Interfaces\IDrivingFile
20
{
21
    use TLang;
22
23
    protected lib $redis;
24
    protected int $timeout = 0;
25
26
    public function __construct(lib $redis, int $timeout = 3600, ?Interfaces\IUppTranslations $lang = null)
27
    {
28
        // path is not a route but redis key
29
        $this->setUppLang($lang);
30
        $this->redis = $redis;
31
        $this->timeout = $timeout;
32
    }
33
34
    /**
35
     * @param string $key
36
     * @return bool
37
     * @codeCoverageIgnore
38
     */
39
    public function exists(string $key): bool
40
    {
41
        // cannot call exists() - get on non-existing key returns false
42
        return (false !== $this->redis->get($key));
43
    }
44
45
    /**
46
     * @param string $key
47
     * @return string
48
     * @codeCoverageIgnore
49
     */
50
    public function get(string $key): string
51
    {
52
        return strval($this->redis->get($key));
53
    }
54
55
    /**
56
     * @param string $key
57
     * @param string $data
58
     * @throws UploadException
59
     * @return string
60
     * @codeCoverageIgnore
61
     */
62
    public function store(string $key, string $data): string
63
    {
64
        if (false === $this->redis->set($key, $data, $this->timeout)) {
65
            throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key));
66
        }
67
        return $key;
68
    }
69
70
    /**
71
     * @param string $key
72
     * @throws UploadException
73
     * @return bool
74
     * @codeCoverageIgnore
75
     */
76
    public function remove(string $key): bool
77
    {
78
        return boolval($this->redis->del($key));
79
    }
80
81
    public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool
82
    {
83
        if (!$encoder instanceof Interfaces\Storages\ForKV) {
84
            throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder)));
85
        }
86
        return true;
87
    }
88
}
89