Predis   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 69
ccs 20
cts 25
cp 0.8
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A load() 0 6 2
A save() 0 9 2
A executeCommand() 0 6 1
1
<?php
2
3
namespace League\Flysystem\Cached\Storage;
4
5
use Predis\Client;
6
7
class Predis extends AbstractCache
8
{
9
    /**
10
     * @var \Predis\Client Predis 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 \Predis\Client $client predis client
28
     * @param string         $key    storage key
29
     * @param int|null       $expire seconds until cache expiration
30
     */
31 12
    public function __construct(Client $client = null, $key = 'flysystem', $expire = null)
32
    {
33 12
        $this->client = $client ?: new Client();
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
        if (($contents = $this->executeCommand('get', [$this->key])) !== null) {
44 3
            $this->setFromStorage($contents);
45 3
        }
46 6
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function save()
52
    {
53 6
        $contents = $this->getForStorage();
54 6
        $this->executeCommand('set', [$this->key, $contents]);
55
56 6
        if ($this->expire !== null) {
57 3
            $this->executeCommand('expire', [$this->key, $this->expire]);
58 3
        }
59 6
    }
60
61
    /**
62
     * Execute a Predis command.
63
     *
64
     * @param string $name
65
     * @param array  $arguments
66
     *
67
     * @return string
68
     */
69 12
    protected function executeCommand($name, array $arguments)
70
    {
71 12
        $command = $this->client->createCommand($name, $arguments);
72
73 12
        return $this->client->executeCommand($command);
74
    }
75
}
76