Passed
Pull Request — master (#5)
by Artem
05:27
created

Cache::getUserParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
namespace Slides\Connector\Auth;
4
5
use Illuminate\Support\Facades\Redis;
6
use Illuminate\Redis\Connections\Connection;
7
8
/**
9
 * Class Cache
10
 *
11
 * @package Slides\Connector\Auth
12
 */
13
class Cache
14
{
15
    /**
16
     * Get a user parameter from cache.
17
     *
18
     * @param int $remoteId The remote user ID.
19
     * @param string $key
20
     * @param mixed $value
21
     *
22
     * @return void
23
     */
24
    public function setUserParam(int $remoteId, string $key, $value)
25
    {
26
        $this->set('user:' . $remoteId, $key, $value);
27
    }
28
29
    /**
30
     * Get a user parameter from cache.
31
     *
32
     * @param int $remoteId The remote user ID.
33
     * @param string $key
34
     *
35
     * @return mixed
36
     */
37
    public function getUserParam(int $remoteId, string $key)
38
    {
39
        return $this->get('user:' . $remoteId, $key);
40
    }
41
42
    /**
43
     * Get all parameters related to the user.
44
     *
45
     * @param int $remoteId The remote user ID.
46
     *
47
     * @return array
48
     */
49
    public function getUserParams(int $remoteId): array
50
    {
51
        return $this->getAll('user:' . $remoteId);
52
    }
53
54
    /**
55
     * Set a parameter value to cache.
56
     *
57
     * @param string $key
58
     * @param string|null $field
59
     * @param mixed $value
60
     *
61
     * @return void
62
     */
63
    public function set(string $key, ?string $field, $value)
64
    {
65
        $value = $this->castValueToString($value);
66
67
        \Illuminate\Support\Facades\Log::debug(
68
            '[Connector] Storing a parameter in cache',
69
            compact('key', 'field', 'value')
70
        );
71
72
        $this->ensure(function(Connection $redis) use ($key, $field, $value) {
73
            $field
74
                ? $redis->hset('connector:' . $key, $field, $value)
75
                : $redis->set('connector:' . $key, $value);
76
        });
77
    }
78
79
    /**
80
     * Get a parameter value from cache.
81
     *
82
     * @param string $key
83
     * @param string $field
84
     *
85
     * @return mixed
86
     */
87
    public function get(string $key, string $field = null)
88
    {
89
        $output = $this->ensure(function(Connection $redis) use ($key, $field) {
90
            return $field
91
                ? $redis->hget('connector:' . $key, $field)
92
                : $redis->get('connector:' . $key);
93
        }, function() {
94
            return null;
95
        });
96
97
        \Illuminate\Support\Facades\Log::debug(
98
            '[Connector] Getting a parameter from cache',
99
            compact('key', 'field', 'output')
100
        );
101
102
        return $this->castValueFromString($output);
103
    }
104
105
    /**
106
     * Get parameters from a hash.
107
     *
108
     * @param string $key
109
     *
110
     * @return mixed
111
     */
112
    public function getAll(string $key)
113
    {
114
        return $this->ensure(function(Connection $redis) use ($key) {
115
            return $redis->hgetall('connector:' . $key);
116
        }, function() {
117
            return [];
118
        });
119
    }
120
121
    /**
122
     * Catch an exception and prevent application from breaking while interacting with Redis.
123
     *
124
     * @param \Closure $callback
125
     * @param \Closure|null $fallback
126
     *
127
     * @return mixed|false
128
     */
129
    protected function ensure(\Closure $callback, \Closure $fallback = null)
130
    {
131
        try {
132
            $output = $callback(Redis::connection('authService'));
133
        }
134
        catch(\Exception $e) {
135
            \Illuminate\Support\Facades\Log::error($e->getMessage());
136
            \Illuminate\Support\Facades\Log::error($e->getTraceAsString());
137
138
            if($fallback instanceof \Closure) {
139
                $output = $fallback($e);
140
            }
141
        }
142
143
        return $output ?? false;
144
    }
145
146
    /**
147
     * Case value to string.
148
     *
149
     * @param mixed $value
150
     *
151
     * @return string
152
     */
153
    protected function castValueToString($value)
154
    {
155
        if(is_bool($value)) {
156
            return $value ? 'true' : 'false';
157
        }
158
159
        return $value;
160
    }
161
162
    /**
163
     * Case value from string.
164
     *
165
     * @param mixed $value
166
     *
167
     * @return mixed
168
     */
169
    protected function castValueFromString($value)
170
    {
171
        if($value === 'true' || $value === 'false') {
172
            return $value === 'true' ? true : false;
173
        }
174
175
        if($value === '') {
176
            return null;
177
        }
178
179
        return $value;
180
    }
181
}