Completed
Pull Request — master (#12)
by ARCANEDEV
03:40
created

RedisStore::connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanedev\LaravelSettings\Stores;
2
3
use Illuminate\Redis\RedisManager;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     RedisStore
8
 *
9
 * @package  Arcanedev\LaravelSettings\Stores
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class RedisStore extends AbstractStore
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * The redis manager.
21
     *
22
     * @var  \Illuminate\Redis\RedisManager
23
     */
24
    protected $manager;
25
26
    /* -----------------------------------------------------------------
27
     |  Post-Constructor
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * Fire the post options to customize the store.
33
     *
34
     * @param  array  $options
35
     */
36 27
    protected function postOptions(array $options)
37
    {
38 27
        $this->manager = new RedisManager(Arr::pull($options, 'client', 'predis'), $options);
39 27
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Main Methods
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Read the data from the store.
48
     *
49
     * @return array
50
     */
51 27
    protected function read()
52
    {
53 27
        $data = $this->command('get', ['settings']);
54
55 27
        return is_string($data) ? json_decode($data, true) : [];
56
    }
57
58
    /**
59
     * Write the data into the store.
60
     *
61
     * @param  array  $data
62
     */
63 27
    protected function write(array $data)
64
    {
65 27
        $this->command('set', ['settings', json_encode($data)]);
66 27
    }
67
68
    /* -----------------------------------------------------------------
69
     |  Other Methods
70
     | -----------------------------------------------------------------
71
     */
72
73
    /**
74
     * Get a Redis connection by name.
75
     *
76
     * @param  string|null  $name
77
     *
78
     * @return \Illuminate\Redis\Connections\Connection
79
     */
80 27
    protected function connection($name = null)
81
    {
82 27
        return $this->manager->connection($name);
83
    }
84
85
    /**
86
     * Run a command against the Redis database.
87
     *
88
     * @param  string  $method
89
     * @param  array   $parameters
90
     *
91
     * @return mixed
92
     */
93 27
    protected function command($method, array $parameters = [])
94
    {
95 27
        return $this->connection()->command($method, $parameters);
96
    }
97
}
98