PhpRedis::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0116
1
<?php
2
3
namespace League\Flysystem\Cached\Storage;
4
5
use Redis;
6
7
class PhpRedis extends AbstractCache
8
{
9
    /**
10
     * @var Redis PhpRedis Client
11
     */
12
    protected $client;
13
14
    /**
15
     * @var string storage key
16
     */
17
    protected $key;
18
19
    /**
20
     * @var int|null seconds until cache expiration
21
     */
22
    protected $expire;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param Redis|null $client phpredis client
28
     * @param string     $key    storage key
29
     * @param int|null   $expire seconds until cache expiration
30
     */
31 12
    public function __construct(Redis $client = null, $key = 'flysystem', $expire = null)
32
    {
33 12
        $this->client = $client ?: new Redis();
34 12
        $this->key = $key;
35 12
        $this->expire = $expire;
36 12
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 6
    public function load()
42
    {
43 6
        $contents = $this->client->get($this->key);
44
45 6
        if ($contents !== false) {
46 3
            $this->setFromStorage($contents);
47 3
        }
48 6
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    public function save()
54
    {
55 6
        $contents = $this->getForStorage();
56 6
        $this->client->set($this->key, $contents);
57
58 6
        if ($this->expire !== null) {
59 3
            $this->client->expire($this->key, $this->expire);
60 3
        }
61 6
    }
62
}
63